
Using argc and argv in C Programming
Discover how to utilize the argc and argv parameters in C programming to handle command-line arguments efficiently. Learn how to convert command-line input into integers, error handling, and more with illustrative code examples and explanations by Yung-Hsiang Lu from Purdue University.
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
How to Use argc and argv Yung-Hsiang Lu Purdue University 1
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } 2
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } check the value use argv[1] 3
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } an error message 4
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } the program stops with an error 5
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } convert argv[1] to an integer store the integer in val 6
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } add 10 to val 7
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } print the string argv[1] and the integer val 8
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } the program successfully prints the value 9
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { if (argc < 2) { printf("Need a number\n"); return EXIT_FAILURE; } int val = strtol(argv[1], NULL, 10); val += 10; printf("argv[1] = %s\n", argv[1]); printf("val = %d\n", val); return EXIT_SUCCESS; } 10