Understanding argc and argv in C Programming

understand argc and argv n.w
1 / 11
Embed
Share

Learn about the argc and argv parameters in C programming with examples and explanations provided by Yung-Hsiang Lu from Purdue University. This tutorial covers the basics of using argc and argv in C programs, including how to access command-line arguments and their importance in program execution.

  • C Programming
  • Command Line
  • Arguments
  • Yung-Hsiang Lu
  • Purdue

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Understand argc and argv Yung-Hsiang Lu Purdue University 1

  2. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } 2

  3. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } an integer as indexes 3

  4. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } %d means the value of an integer This line prints argc's value 4

  5. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } argc is the number of arguments it is at least one 5

  6. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } the first argument is the program's name 6

  7. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } ind is 0, 1, 2, ... argc - 1 7

  8. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } print the index and the value of the argument 8

  9. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } print an integer 9

  10. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } print a string 10

  11. #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) { printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } ind is 0, 1, 2, ..., argc - 1 not not 1, 2, ..., argc 11

Related


More Related Content