0
0
C++programming~10 mins

main function and program entry in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the main function that returns 0.

C++
int [1]() {
    return 0;
}
Drag options to blanks, or click blank then click option'
Abegin
Bstart
Cmain
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name other than 'main' for the program entry point.
Forgetting to define the main function.
2fill in blank
medium

Complete the code to print "Hello, World!" inside the main function.

C++
#include <iostream>

int main() {
    std::[1] << "Hello, World!" << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Acout
Bprint
Cwrite
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of cout in C++.
Forgetting to use std:: prefix or using namespace std;.
3fill in blank
hard

Fix the error in the main function declaration to make it valid.

C++
int [1](int argc, char* argv[]) {
    return 0;
}
Drag options to blanks, or click blank then click option'
Astart
Bprogram
Centry
Dmain
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name instead of main.
Incorrect function signature for the program entry point.
4fill in blank
hard

Fill both blanks to complete the main function that prints the first command line argument.

C++
#include <iostream>

int [1](int argc, char* argv[]) {
    if (argc > 1) {
        std::cout << argv[[2]] << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Amain
B0
C1
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using argv[0] which is the program name, not the first argument.
Using a wrong function name for the entry point.
5fill in blank
hard

Fill all three blanks to complete a main function that prints all command line arguments except the program name.

C++
#include <iostream>

int [1](int argc, char* argv[]) {
    for (int i = [2]; i < argc; ++i) {
        std::cout << argv[[3]] << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Amain
B0
Ci
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 which prints the program name.
Using a fixed index inside the loop instead of the loop variable.