Sunteți pe pagina 1din 4

C/C++ programming through Linux-11I

- Kshitij Arade

Hi! Today I am going to continue with C++ because I saw the students are struggling to run the C++ programs using GCC compiler and yet they are not succeed to do that. In this SparkForum tutorial we are going to develop simple programs to demonstrate how to use GCC compiler to run C++ programs so lets go for itYou already have studied the C++ in college so I am not going to explain what does C++ means and why it is called object oriented language. There are such things that you have to learn by your own. I am just providing the programming information and I always dealing with errors while programming the C++ code. Now open an editor and type in the program given below save it as Demo1.cpp in the Home folder. Open a terminal window and type to compile the program.

~:$ g++ -o demo Demo1.cpp You should see that Demo1.cpp is there and a file called demo which is the compiled C++ program you have just written.

/***************************************************************** C ++ Programming in Linux Demo1.cpp *****************************************************************/ #include<iostream> using namespace std; int main() { int n; cout << "\n\n Hi! From Kshitij Enter any Value n="; cin >> n; cout << "\n You Entered the value of n= " << n <<\n\n; } Now type following command to execute, or run the program. :~$

./demo

Hi! From Kshitij Enter any Value n= 20 You Entered the value of n= 20

Anatomy of the program:


We have used the following #include directive in our program#include <iostream> not iostream.h

It contains the declarations of cin and cout and the operator << and >>. some old versions of C++ compilers uses the iostream.h header file instead of iostream alone but it not allowed in

new version of gcc compiler. (Turbo C/C++ software still uses iostream.h as header file. The oldest technology . ). This change is introduced by ANSI C++. Some implementations use iostream.hpp; yet others iostream.hxx. You can use any according to your version of compiler. Namespace is a new concept introduced by ANSI C++ which defines a scope for identifiers used in our program (in our case int n) which is denoted byusing namespace std; Remaining program is as it is. it is self explanatory. We just taking input value for variable n from user and same value is displayed as output. cout is replacement for printf() function as output operator and cin is replacement for scanf() function as input operator. We are not using %d, %f etc format specifiers in C++ program. Let's rewrite the program as follows to see what this entire means before we go further. /**************************************************************** C++ Programming in Linux Demo2.cpp *****************************************************************/ #include<iostream> using namespace std; int main() { float n1, n2, sum = 0.0, avg = 0.0; cout << "\n Enter the values of N1 and N2 here--->"; cout << "\nN1 = "; cin >> n1; cout << "\nN2 = "; cin >> n2;

sum = n1 + n2; avg = sum / 2; cout<<"\n Sum of "<< n1 <<" + "<< n2 << " = " <<sum; cout<<"\n Average of "<< sum <<" /2 " << " = " <<avg; }

Save the file as Demo2.cpp in the same folder. Open a terminal window and type: ~:$ g++ -o demo2 Demo2.cpp to compile the program into a form that can be executed. Now type to execute, or run the program. ~:$ . /demo2 Enter the values of N1 and N2 here---> N1 = 10.2 N2 = 5.0 Sum of 10.2 + 5.0 = 15.2 Average of 15.2 /2 = 7.6 I think it is not necessary to explain this code block; here we are taking two numbers as a input to our program, we calculate the sum of these two numbers and then calculate the average of these two using mathematical functions. In next Tutorial we will see more complications of C/ C++ programs. Thank you for reading

S-ar putea să vă placă și