Download presentation
Presentation is loading. Please wait.
1
Yung-Hsiang Lu Purdue University
How to Use argc and argv Yung-Hsiang Lu Purdue University
2
#include <stdio. h> #include <stdlib
#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;
3
check the value use argv[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; check the value use argv[1]
4
#include <stdio. h> #include <stdlib
#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
5
the program stops with an error
#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
6
convert argv[1] to an integer store the integer in val
#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
7
#include <stdio. h> #include <stdlib
#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
8
print the string argv[1] and the integer val
#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
9
the program successfully prints the value
#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
10
#include <stdio. h> #include <stdlib
#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;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.