Guest User

Untitled

a guest
Jun 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. Arrays in String:
  2.  
  3. Use a [] to tell that it's an array, like: string s[] means "s" is an array of strings.
  4.  
  5. Arrays can be any types, like int a[] or float b[]
  6.  
  7. We can use the string array in "main" function, so that we can give "main" function 2 inputs.
  8.  
  9. Instead of "int main(void)", we can say:
  10.  
  11. 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
  12.  
  13. so we can add some input after the program name, and then there will be some output:
  14.  
  15. int main(int argc, string argv[])
  16.  
  17. {
  18.  
  19. if (argc == 2) // if there are 2 words in input
  20.  
  21. {
  22.  
  23. printf("hello, %s\n", argv[1]); // print out the second inputed word
  24.  
  25. }
  26.  
  27. else
  28.  
  29. {
  30.  
  31. printf("hello world.\n");
  32.  
  33. }
  34. }
  35.  
  36. So after the compile: make argv0
  37.  
  38. if we type ./argv0
  39.  
  40. The output will be:
  41.  
  42. hello, world
  43.  
  44. if we type ./argv0 David
  45.  
  46. The output will be:
  47.  
  48. hello, David
Add Comment
Please, Sign In to add comment