Bird
Raised Fist0
Cprogramming~20 mins

Accessing arguments - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Argument Access 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?

Consider the following C program that prints command line arguments:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%d\n", argc);
    return 0;
}

If you run this program as ./program arg1 arg2, what will it print?

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%d\n", argc);
    return 0;
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

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

❓ Predict Output
intermediate
2:00remaining
What does this program print?

Look at this C program that prints the first command line argument:

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        printf("%s\n", argv[1]);
    } else {
        printf("No argument\n");
    }
    return 0;
}

What will it print if run as ./program Hello?

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        printf("%s\n", argv[1]);
    } else {
        printf("No argument\n");
    }
    return 0;
}
AHello
BNo argument
C./program
Dargv[1]
Attempts:
2 left
💡 Hint

Remember argv[0] is the program name, argv[1] is the first argument.

❓ Predict Output
advanced
2:00remaining
What is the output of this program?

Analyze this C program that prints all command line arguments:

#include <stdio.h>

int main(int argc, char *argv[]) {
    for (int i = 0; i < argc; i++) {
        printf("%s ", argv[i]);
    }
    printf("\n");
    return 0;
}

What will it print if run as ./prog one two?

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    for (int i = 0; i < argc; i++) {
        printf("%s ", argv[i]);
    }
    printf("\n");
    return 0;
}
Aone two
B./prog one two
C./prog
Dprog one two
Attempts:
2 left
💡 Hint

Remember argv[0] is the program name including the path used to run it.

❓ Predict Output
advanced
2:00remaining
What error does this program cause?

Consider this C program:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%s\n", argv[argc]);
    return 0;
}

What happens when you run it?

C
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%s\n", argv[argc]);
    return 0;
}
ASegmentation fault (accessing invalid memory)
BPrints the last argument
CPrints the program name
DCompilation error
Attempts:
2 left
💡 Hint

Remember that argv array indices go from 0 to argc - 1.

🧠 Conceptual
expert
2:00remaining
How to access the third command line argument safely?

You want to print the third command line argument in a C program. Which code snippet safely does this?

Aprintf("%s\n", argv[2]);
Bif (argc &gt; 2) printf("%s\n", argv[2]); else printf("No third argument\n");
Cif (argc &gt; 3) printf("%s\n", argv[3]); else printf("No third argument\n");
Dif (argc &gt;= 4) printf("%s\n", argv[3]); else printf("No third argument\n");
Attempts:
2 left
💡 Hint

Remember that argv[0] is the program name, so the first argument is argv[1].

Practice

(1/5)
1.

What do argc and argv represent in a C program's main function?

easy
A. argc is the count of arguments; argv is an array of argument strings.
B. argc is the first argument; argv is the last argument.
C. argc is the program name; argv is the argument count.
D. argc and argv are used to return values from main.

Solution

  1. Step 1: Understand argc meaning

    argc holds the number of command-line arguments passed, including the program name.
  2. Step 2: Understand argv meaning

    argv is an array of strings holding each argument, starting with the program name at index 0.
  3. Final Answer:

    argc is the count of arguments; argv is an array of argument strings. -> Option A
  4. Quick Check:

    argc = count, argv = array [OK]
Hint: Remember: argc counts, argv holds strings [OK]
Common Mistakes:
  • Confusing argc as the first argument value
  • Thinking argv is a single string, not an array
  • Assuming argc excludes the program name
2.

Which of the following is the correct way to declare the main function to access command-line arguments in C?

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

Solution

  1. Step 1: Check return type and parameters

    The standard way is int main(int argc, char *argv[]) where argc is int and argv is array of char pointers.
  2. Step 2: Identify incorrect declarations

    Options A, B, and C have wrong types or missing return type.
  3. Final Answer:

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

    Correct main signature uses int and char* array [OK]
Hint: Main must return int and use char* argv[] [OK]
Common Mistakes:
  • Using void as return type for main
  • Declaring argv as char instead of char* array
  • Omitting return type in main declaration
3.

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;
}
medium
A. 2 apple banana
B. 3 ./prog apple
C. 3 apple banana
D. 2 ./prog apple

Solution

  1. Step 1: Count arguments with argc

    Running as ./prog apple banana passes 3 arguments: program name, apple, banana. So argc = 3.
  2. Step 2: Identify argv values

    argv[0] is program name ./prog, argv[1] is apple.
  3. Final Answer:

    3 ./prog apple -> Option B
  4. Quick Check:

    argc=3, argv[0]=./prog, argv[1]=apple [OK]
Hint: argv[0] is program name; argc counts all args [OK]
Common Mistakes:
  • Assuming argv[0] is first user argument
  • Counting argc as number of user arguments only
  • Mixing argv indexes and argc count
4.

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;
}
medium
A. No error; prints second argument correctly.
B. Syntax error: argv should be argv[1].
C. Possible runtime error if less than 3 arguments are passed.
D. Missing return statement in main.

Solution

  1. Step 1: Check argv index usage

    Accessing argv[2] means the third argument (program name is argv[0]).
  2. Step 2: Consider argument count safety

    If fewer than 3 arguments are passed, argv[2] is invalid and causes runtime error.
  3. Final Answer:

    Possible runtime error if less than 3 arguments are passed. -> Option C
  4. Quick Check:

    Accessing argv beyond argc causes runtime error [OK]
Hint: Check argc before accessing argv index [OK]
Common Mistakes:
  • Ignoring argc check before argv access
  • Confusing argv index with argument number
  • Assuming argv[2] always exists
5.

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;
}
hard
A. if (argc > 1) printf("%s\n", argv[1]); else printf("No argument\n");
B. printf("%s\n", argv[1]);
C. if (argc == 1) printf("%s\n", argv[1]); else printf("No argument\n");
D. if (argc < 1) printf("No argument\n"); else printf("%s\n", argv[0]);

Solution

  1. Step 1: Check argument count safely

    To print first user argument, ensure argc > 1 because argv[0] is program name.
  2. Step 2: Print argument or fallback message

    If argc > 1, print argv[1]; else print "No argument".
  3. Final Answer:

    if (argc > 1) printf("%s\n", argv[1]); else printf("No argument\n"); -> Option A
  4. Quick Check:

    Check argc > 1 before argv[1] access [OK]
Hint: Always check argc before using argv index [OK]
Common Mistakes:
  • Not checking argc before accessing argv[1]
  • Printing argv[0] instead of user argument
  • Using wrong condition like argc == 1
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