Experiment 2 Creating, Compiling and Executing C/C++ Programs Using GCC/G++ Compilers
Experiment 2 Creating, Compiling and Executing C/C++ Programs Using GCC/G++ Compilers
Cpp/c file(s) Preprocessed file(s) Assembly File(s) Generation Object file(s) Generation
Final Executable
.Every c/cpp file has its own preprocessed file, assembly file, and object file
1. For running only the preprocessor, we use -E option.
2. For running the compilation process till assembly file generation, we use –S option.
3. For running the compilation process till object file creation, we use –c option.
4. If no option is specified, the whole compilation process till the generation of executable will
run.
A file generated using any option can be used to create the final executable. For example, let’s
suppose that we have two source files: math.cpp and main.cpp, and we create object files:
The object files created using above two commands can be used to generate the final executable.
Command line arguments are a way to pass data to the program. Command line arguments are
passed to the main function. Suppose we want to pass two integer numbers to main function of
an executable program called a.out. On the terminal write the following line:
./a.out 1 22
./a.out is the usual method of running an executable via the terminal. Here 1 and 22 are the
numbers that we have passed as command line argument to the program. These arguments are
passed to the main function. In order for the main function to be able to accept the arguments, we
have to change the signature of main function as follows:
argc is the counter. It tells how many arguments have been passed.
argc in this case will not be equal to 2, but it will be equal to 3. This is because the name ./a.out
is also passed as command line argument. At index 0 of arg, we have ./a.out; at index 1, we have
1; and at index 2, we have 22. Here 1 and 22 are in the form of character string, we have to
convert them to integers by using a function atoi. Suppose we want to add the passed numbers
and print the sum on the screen:
In-Lab Questions:
Question 1: Write a C or C++ program that accepts a file name as command line argument and
prints the file’s contents on console. If the file does not exist, print some error on the screen.
Question 2: Write a C or C++ program that accepts a list of integers as command line
arguments sorts the integers and print the sorted integers on the screen.
Post-Lab Questions:
Problem 1: Write a C/C++ program that takes some integers as command line parameters, store
them in an array and prints the sum and average of that array. Also note that you have to run the
program for all possible error checks.
Problem 3: Write a C/C++ program that takes some integers in the form of series as command
line parameters; store them in array than compute the missing element from that series and
output that missing element to file.
Problem 4: Write a C/C++ program that reads file in which there are integers related to series
and store them in array than compute the missing element from that series and output that
missing element to file.