Bird
Raised Fist0
Cprogramming~20 mins

Syntax of command line arguments - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Command Line Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C program with command line arguments?

Consider this C program that prints the number of command line arguments and the first argument:

#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("%d %s\n", argc, argv[1]);
    return 0;
}

If you run this program as ./program Hello, what will be the output?

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("%d %s\n", argc, argv[1]);
    return 0;
}
A1 Hello
B2 Hello
C2 ./program
D1 ./program
Attempts:
2 left
💡 Hint

Remember, argc counts the program name as the first argument.

❓ Predict Output
intermediate
2:00remaining
What does this program print when no arguments are given?

Look at this C program:

#include <stdio.h>
int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("No arguments\n");
    } else {
        printf("Argument: %s\n", argv[1]);
    }
    return 0;
}

What will it print if you run it as ./program with no extra arguments?

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("No arguments\n");
    } else {
        printf("Argument: %s\n", argv[1]);
    }
    return 0;
}
ASegmentation fault
BArgument: ./program
CArgument: (null)
DNo arguments
Attempts:
2 left
💡 Hint

Check the value of argc when no extra arguments are passed.

❓ Predict Output
advanced
2:00remaining
What error occurs when accessing argv out of bounds?

Consider this program:

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

If you run this program as ./program one, what happens?

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("%s\n", argv[2]);
    return 0;
}
APrints garbage or causes segmentation fault
BPrints program name
CPrints "one"
DCompilation error
Attempts:
2 left
💡 Hint

Think about how many arguments are passed and what argv[2] points to.

🧠 Conceptual
advanced
2:00remaining
How are command line arguments passed to a C program?

Which statement best describes how command line arguments are passed to a C program?

AArguments are passed as an integer count and an array of strings to <code>main()</code>.
BArguments are passed as environment variables only.
CArguments are passed as a single string to <code>main()</code>.
DArguments are passed through global variables automatically.
Attempts:
2 left
💡 Hint

Recall the signature of main() that receives arguments.

❓ Predict Output
expert
2:00remaining
What is the output of this program with multiple arguments?

Analyze this program:

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

What will be printed if you run ./program apple banana?

C
#include <stdio.h>
int main(int argc, char *argv[]) {
    for (int i = 0; i < argc; i++) {
        printf("%d:%s ", i, argv[i]);
    }
    printf("\n");
    return 0;
}
A0:apple 1:banana 2:./program
B0:./program 1:banana 2:apple
C0:./program 1:apple 2:banana
D1:./program 2:apple 3:banana
Attempts:
2 left
💡 Hint

Remember the order of arguments in argv.

Practice

(1/5)
1.

What does the argc parameter represent in a C program's main function?

easy
A. The number of command line arguments including the program name
B. The array of command line argument strings
C. The first command line argument
D. The program's exit status

Solution

  1. Step 1: Understand the role of argc

    argc stands for 'argument count' and tells how many arguments are passed to the program including the program name itself.
  2. Step 2: Differentiate argc from argv

    argv is the array holding the actual argument strings, not the count.
  3. Final Answer:

    The number of command line arguments including the program name -> Option A
  4. Quick Check:

    argc = argument count [OK]
Hint: Remember: argc counts all arguments including program name [OK]
Common Mistakes:
  • Confusing argc with argv
  • Thinking argc excludes the program name
  • Assuming argc is the argument array
2.

Which of the following is the correct syntax for the main function to accept command line arguments in C?

easy
A. int main(char argc, char *argv[])
B. void main()
C. int main(int argc, char *argv[])
D. int main(int argv, char *argc[])

Solution

  1. Step 1: Recall the standard signature for main with arguments

    The correct signature uses int argc and char *argv[] to receive argument count and argument strings.
  2. Step 2: Identify incorrect options

    void main() lacks parameters, C uses wrong type for argc, D swaps argc and argv incorrectly.
  3. Final Answer:

    int main(int argc, char *argv[]) -> Option C
  4. Quick Check:

    Main signature with argc and argv = int main(int argc, char *argv[]) [OK]
Hint: argc is int, argv is array of char pointers [OK]
Common Mistakes:
  • Using void main instead of int main
  • Swapping argc and argv types
  • Using char type for argc
3.

What will be the output of the following C program if run as ./program hello world?

int main(int argc, char *argv[]) {
    printf("%d %s %s", argc, argv[1], argv[2]);
    return 0;
}
medium
A. 3 hello world
B. 2 hello world
C. 3 ./program hello
D. Error: argv[2] does not exist

Solution

  1. Step 1: Count the arguments passed

    Running ./program hello world passes three arguments: program name, "hello", and "world". So, argc = 3.
  2. Step 2: Identify argv values

    argv[0] = "./program", argv[1] = "hello", argv[2] = "world".
  3. Final Answer:

    3 hello world -> Option A
  4. Quick Check:

    argc=3, argv[1]=hello, argv[2]=world [OK]
Hint: argv[0] is program name; argc counts all args [OK]
Common Mistakes:
  • Assuming argc excludes program name
  • Confusing argv indexes
  • Thinking argv[2] is out of range
4.

Identify the error in this C program snippet that uses command line arguments:

int main(int argc, char *argv[]) {
    printf("%s", argv[argc]);
    return 0;
}
medium
A. argc should be a char type
B. Accessing argv[argc] causes out-of-bounds error
C. argv should be int array
D. Missing return statement

Solution

  1. Step 1: Understand argv indexing

    argv indexes go from 0 to argc-1. Accessing argv[argc] is out of bounds and unsafe.
  2. Step 2: Check other options

    argc must be int, argv is array of char pointers, and return statement is present.
  3. Final Answer:

    Accessing argv[argc] causes out-of-bounds error -> Option B
  4. Quick Check:

    argv valid indexes: 0 to argc-1 [OK]
Hint: argv max index is argc-1, not argc [OK]
Common Mistakes:
  • Using argv[argc] instead of argv[argc-1]
  • Changing argc type incorrectly
  • Ignoring array bounds
5.

How would you write a C program that prints all command line arguments except the program name, each on a new line?

hard
A. Print argc without looping
B. Print argv[0] only
C. Use a loop from 0 to argc and print argv[i]
D. Use a loop from 1 to argc-1 and print argv[i]

Solution

  1. Step 1: Understand argument indexing

    argv[0] is the program name, so to print only arguments, start from index 1.
  2. Step 2: Loop through arguments

    Loop from i=1 to i<argc to print each argument on a new line.
  3. Final Answer:

    Use a loop from 1 to argc-1 and print argv[i] -> Option D
  4. Quick Check:

    Skip argv[0], loop i=1 to argc-1 [OK]
Hint: Start loop at 1 to skip program name [OK]
Common Mistakes:
  • Starting loop at 0 printing program name
  • Looping beyond argc-1 causing errors
  • Not using a loop to print all arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard