Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Arrays in String:
- Use a [] to tell that it's an array, like: string s[] means "s" is an array of strings.
- Arrays can be any types, like int a[] or float b[]
- We can use the string array in "main" function, so that we can give "main" function 2 inputs.
- Instead of "int main(void)", we can say:
- int main(int argc, string argv[]) // "argc" is the total number of words user typed, and "argv[]" are the array of all the words that user typed
- so we can add some input after the program name, and then there will be some output:
- int main(int argc, string argv[])
- {
- if (argc == 2) // if there are 2 words in input
- {
- printf("hello, %s\n", argv[1]); // print out the second inputed word
- }
- else
- {
- printf("hello world.\n");
- }
- }
- So after the compile: make argv0
- if we type ./argv0
- The output will be:
- hello, world
- if we type ./argv0 David
- The output will be:
- hello, David
Add Comment
Please, Sign In to add comment