We use arguments to give extra information to a program when it starts. Accessing arguments lets the program read these inputs and use them.
Accessing arguments
int main(int argc, char *argv[]) { // code here }
argc is the count of arguments including the program name.
argv is an array of strings holding each argument.
int main(int argc, char *argv[]) { // argc is number of arguments // argv[0] is program name // argv[1] is first argument }
#include <stdio.h> int main(int argc, char *argv[]) { if (argc > 1) { printf("First argument: %s\n", argv[1]); } return 0; }
This program prints how many arguments were given and lists each one. The first argument is always the program name.
#include <stdio.h> int main(int argc, char *argv[]) { printf("Number of arguments: %d\n", argc); for (int i = 0; i < argc; i++) { printf("Argument %d: %s\n", i, argv[i]); } return 0; }
Arguments are always strings; you may need to convert them to numbers if needed.
Remember that argv[0] is the program's name or path.
Always check argc before accessing argv to avoid errors.
Arguments let you give input to a program when it starts.
Use argc and argv in main to access them.
Check the number of arguments before using them to keep your program safe.
Practice
What do argc and argv represent in a C program's main function?
Solution
Step 1: Understand
argcmeaningargcholds the number of command-line arguments passed, including the program name.Step 2: Understand
argvmeaningargvis an array of strings holding each argument, starting with the program name at index 0.Final Answer:
argcis the count of arguments;argvis an array of argument strings. -> Option AQuick Check:
argc= count,argv= array [OK]
- Confusing argc as the first argument value
- Thinking argv is a single string, not an array
- Assuming argc excludes the program name
Which of the following is the correct way to declare the main function to access command-line arguments in C?
Solution
Step 1: Check return type and parameters
The standard way isint main(int argc, char *argv[])where argc is int and argv is array of char pointers.Step 2: Identify incorrect declarations
Options A, B, and C have wrong types or missing return type.Final Answer:
int main(int argc, char *argv[]) -> Option DQuick Check:
Correct main signature uses int and char* array [OK]
- Using void as return type for main
- Declaring argv as char instead of char* array
- Omitting return type in main declaration
What will be the output of this program if run as ./prog apple banana?
int main(int argc, char *argv[]) {
printf("%d %s %s\n", argc, argv[0], argv[1]);
return 0;
}Solution
Step 1: Count arguments with argc
Running as./prog apple bananapasses 3 arguments: program name, apple, banana. So argc = 3.Step 2: Identify argv values
argv[0]is program name./prog,argv[1]isapple.Final Answer:
3 ./prog apple -> Option BQuick Check:
argc=3, argv[0]=./prog, argv[1]=apple [OK]
- Assuming argv[0] is first user argument
- Counting argc as number of user arguments only
- Mixing argv indexes and argc count
Identify the error in this program snippet that tries to print the second argument:
int main(int argc, char *argv[]) {
printf("%s\n", argv[2]);
return 0;
}Solution
Step 1: Check argv index usage
Accessingargv[2]means the third argument (program name is argv[0]).Step 2: Consider argument count safety
If fewer than 3 arguments are passed,argv[2]is invalid and causes runtime error.Final Answer:
Possible runtime error if less than 3 arguments are passed. -> Option CQuick Check:
Accessing argv beyond argc causes runtime error [OK]
- Ignoring argc check before argv access
- Confusing argv index with argument number
- Assuming argv[2] always exists
Write a program snippet that safely prints the first user argument if provided, or prints No argument otherwise.
int main(int argc, char *argv[]) {
// Your code here
return 0;
}Solution
Step 1: Check argument count safely
To print first user argument, ensureargc > 1becauseargv[0]is program name.Step 2: Print argument or fallback message
Ifargc > 1, printargv[1]; else print "No argument".Final Answer:
if (argc > 1) printf("%s\n", argv[1]); else printf("No argument\n"); -> Option AQuick Check:
Check argc > 1 before argv[1] access [OK]
- Not checking argc before accessing argv[1]
- Printing argv[0] instead of user argument
- Using wrong condition like argc == 1
Ready to prove it?
Pass the test and this pattern moves unknown
