简体   繁体   中英

How to send a variable from batch file to a C program?

How to use a variable from a batch file and send the value to a variable in a C file/program?

I absolutely do not know how to do this, and I don't know if it is even possible.

I am thinking about something like this:

Batch file:

@echo off
set name= %1
::somehow set a variable in myfile.c to "name"'s value
gcc myfile.c -o readf
.\readf

C file:

#include <stdio.h>
int main() {
    char(or any other) name[] = /* the value of "name" in the batch file */
    printf(name);

    return 0;
}

As pointed out by @kopecs, it seem that you want to define a macro in the command line while compiling your code. This would be useful if you want to define some constant that will be included in the binary for future execution, but not if you simply want to get argument from the command line.

Modify your code the following way to use the preprocessor

#include <stdio.h>

#ifndef VARIABLE
// In case you want to define a default value for your VARIABLE
#define VARIABLE "default value"
#endif

int main() {
    char(or any other) name[] = VARIABLE // VARIABLE is a preprocessing macro that will be defined by the compiler
    printf(name);

    return 0;
}

Then invoke the compiler with the following preprocessor option

 @echo off
set name=%1
::somehow set a variable in myfile.c to "name"'s value
gcc myfile.c -o readf -D "VARIABLE=%name%" -Wall -Wextra -pedantic
.\readf

Documentation quote for the argument

-D name=definition

The contents of definition are tokenized and processed as if they appeared during translation phase three in a '#define' directive. In particular, the definition is truncated by embedded newline characters.

If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell's quoting syntax to protect characters such as spaces that have a meaning in the shell syntax.

If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you should quote the option. With sh and csh, -D'name(args…)=definition' works.

-D and -U options are processed in the order they are given on the command line. All -imacros file and -include file options are processed after all -D and -U options.

However, I would recommend using command line argument for your program, that would be more flexible and avoid to recompile it each time you want to change it (unless you want to redistribute the binary with the hardcoded string).

#include <stdio.h>
int main(int argc, char* argv[]) {
    // add some sanity checking against argc before getting the value in argv
    char(or any other) name[] = argv[1];
    printf(name);

    return 0;
}

and after compiling your code the with

gcc myfile.c -o readf -Wall -Wextra -pedantic

Just invoke with

.\readf name_value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM