0
0
Cprogramming~20 mins

Syntax of command line arguments - Practice Problems & Coding Challenges

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