简体   繁体   中英

Problem with command Line argument in Turbo C compiler

Can anyone tell me how i can give command line argument (int argc and char*argv[]) in turbo C compiler??

Thnx

  • Launch a command prompt
  • run your executable. if it is abc.exe do: abc.exe argument1 argument2 argument3. . . argumentn abc.exe argument1 argument2 argument3. . . argumentn

In the code argv[0] will contain abc.exe , argv[1] will contain argument1 and so on. argc value would be the number of strings in argv

Sample

#include <stdio.h>

int main (int argc, char *argv[])
{
  int i=0;
  printf ("\nargc = %d", argc);
  for (i=0; i<argc; i++)
  {
    printf ("\nargv[%d] = %s", i, argv[i]);
  }
  printf ("\n");
  return 0;
}

run with:

demo.exe hello man this is a test

Output:

argc = 7
argv[0] = demo.exe
argv[1] = hello
argv[2] = man
argv[3] = this
argv[4] = is
argv[5] = a
argv[6] = test

PS: Please stop using TurboC (3.1)

Just declare your main's prototype as int main(int argc, char *argv[]) and you'll be fine. argc and argv are passed by the operating system (whichever you're using);)

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