Lab 1 Q1 - Hello World Example

Hello World example programs

This question comes from slide 3 of lecture 1

Exercise 1: Compile and run the following C++ programs using Dev C++ and understand what is the difference between them.
Exercise 2: Understand which one of the following programs and can take arguments and how you can pass arguments to it.
Exercise 3: Understand how to use command line arguments which are passed to your program (using both Dev C++ and the command line).
Exercise 4: Understand why the system("pause") line is there at the end of main(). What happens if you remove it?

Hello World program with arguments (slide 3 of lecture 1)

/* HelloWorldWithArgs.cpp*/
#include <iostream>
using namespace std;
 
int main(int argc, char *argv[]) {
    int num;
    cin >> num;
    cout << "Hello World " << num
         << endl;
    system("pause");
}

Hello World program without arguments

/* HelloWorld.cpp*/
#include <iostream>
using namespace std;
 
int main() {
    int num;
    cin >> num;
    cout << "Hello World " << num
         << endl;
    system("pause");
}

The first program accepts arguments because you can see the main() has the parameters to accept the command line arguments which are given like this

int main(int argc, char *argv[])
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License