0

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

Thnx

Stuti
  • 1,620
  • 1
  • 16
  • 33

2 Answers2

6
  • Launch a command prompt
  • run your executable. if it is abc.exe do : 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

P.S.: Please stop using TurboC (3.1)

phoxis
  • 60,131
  • 14
  • 81
  • 117
  • 2
    Why do you print backwards (`"\nstuff"` versus `"stuff\n"`)? :D – pmg Jun 06 '11 at 18:24
  • 2
    habit, `"stuff\n"` feels like that there is no soil under the feet of the string. `"\nstuff"` feels like there is a solid base (the `\n`) behind the string protecting it. Its actually mental. – phoxis Jun 06 '11 at 18:39
0

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) ;)

BlackBear
  • 22,411
  • 10
  • 48
  • 86