execute C program from .bat file

1

What I want to know is if you can run a C program and give the program entries through the same .bat file for example a program that adds 2 numbers what I do in the code below is compile it and generate a. exe that is called add then in the following line executes it and the time to execute it I want that when asking the numbers the program to add automaticallycamente enter the 10 then the 20 and already the program says how much is the sum ... can be done somehow?  in advance, thank you very much

gcc sumar.c -o sumar

sumar

10

20

pause
    
asked by Yept 23.10.2017 в 08:54
source

1 answer

1

If the program expects to receive the values through the standard input, what you can do is redirect said entry to a file.

The batch would look like this:

sumar < entrada.txt

If you want to provide the values directly from the batch you have to modify your program to read the input parameters:

batch:

sumar 10 20

C:

int main(int argc, char** argv)
{
  int a, b;
  if( argc == 3 )
  {
    a = strtol(argv[1],NULL,10);
    b = strtol(argv[2],NULL,10);
  }
  else
  {
    printf("Introduce dos números:\n");
    scanf("%d %d",&a,&b);
  }

  printf("El resultado es %d\n",a+b);
}
    
answered by 23.10.2017 / 09:11
source