1

I'm not sure exactly if the title made it clear, but I want to save the text typed in the same line while executing a program in C.

For example, if I type: ./myprogram samplestring

I want to save samplestring as a variable in the C program. Not sure if this is possible or not, and sorry if I'm not being clear, I'm a bit confused.

4
  • 2
    Are you looking for argv[1]? Commented Mar 1, 2016 at 18:57
  • 1
    crasseux.com/books/ctutorial/argc-and-argv.html will help. A better title for your question would be "how to get the command line arguments supplied to my C program". Commented Mar 1, 2016 at 19:02
  • I think his main issue was that he didn't know that this were 'command line arguments' ! Hence the difficulty to search for it, to find duplicates and to have a clear title. Commented Mar 1, 2016 at 19:39
  • Yes, I was looking for argv[1], and @mah, thanks for the article. Very helpful. Commented Mar 1, 2016 at 20:32

1 Answer 1

2

From here:

#include <stdio.h> int main (int argc, char *argv[]) { int count; printf ("This program was called with \"%s\".\n",argv[0]); if (argc > 1) { for (count = 1; count < argc; count++) { printf("argv[%d] = %s\n", count, argv[count]); } } else { printf("The command had no other arguments.\n"); } return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.