Skills101
← All practice quizzes

Quiz: Command-Line Arguments in C

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Pass information to a C program when you start it. Learn what argc and argv hold, how to convert arguments to numbers safely, handle options like -n 5, and return exit codes.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    A program is run as ./calc 5 + 7. What is argc?

  2. Question 2

    For the same command ./calc 5 + 7, what is argv[2]?

  3. Question 3

    What is always stored in argv[argc]?

  4. Question 4

    What does this print?

    #include <stdio.h>
    int run(int argc, char *argv[]) {
        int total = 0;
        for (int i = 1; i < argc; i++)
            total += argv[i][0] - '0';
        return total;
    }
    int main(void) {
        char *v[] = {"./t", "3", "4", "5", NULL};
        printf("%d", run(4, v));
        return 0;
    }
    
  5. Question 5

    Why is strtol better than atoi for reading a number from argv?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →