0
0
Cprogramming~20 mins

Accessing arguments - Practice Problems & Coding Challenges

Choose your learning style9 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].