Writing First C/C++ Program : A tutorial of C/C++ Programming
::Big Index :: C/C++ Programming Tutorials::
C/C++ Tutorial : Basic of programming
C/C++ programming basic is very important for learning C/C++ language. In this page, basic of C/C++ programming tutorial is presented in an explained way. It is very useful for the beginners of C/C++ learners. Basically, it is a basic tutorial of a C/C++ programming. The basic programming concepts are explained thoroughly with the help of one simple program "Hello World" program. Here, at first, I simply discussed C and C++ programming definition. Then the program basic is explained.
Requirements for writing a program
- Computer
- Programmer
- Compiler
- Basic knowledge of programming
What is C programming?
C is :
- a programming language
- a structured language
- a general purpose programming language
- developed basically for developing system programming
- also used for application programming
- developed by Dennis Ritchie at Bell Lab many
- languages are greatly influenced by C (for example: C++)
- low level language
What is C++ programming?
C++ is :
- object-oriented programming language
- C++ is a super-set of C
- supports almost all features of C
- simplifies memory management
- supports several features-
- encapsulation
- inheritance
- polymorphism
- created by Bjarne Stroustrup
Writing the Program : Learn by Example
Programs are developed or implemented by programmer. For successful implementation of program or programs, three steps are required:
- Step 01 : Writing the program
- Step 02 : Compiling i.e. checking program's syntax to find errors and solving
- Step 03 : Running the program
Step 01 : Writing the program
Compilers are required for writing any C or C++ program. Most common compilers are Borland C++, Microsoft C++, GNU C++ etc. The compile choosing is fully depends on you.
In this article, the program will be implemented by using "Turbo C++" compiler. Here, a simple program will be coded. We call it "Hello World" program. The program code is given below:
"Hello World" Program
/********************************/ /* A simple Hello World program */ /********************************/
#include<iostream.h> #include<conio.h> /* declertion of main function */
int main(){ cout << "Hello World !!"; cout << endl; cin.get(); return 0; } /* Program ends here */
This is the actual needed code. But to understand the program, more details is explained below:
Explained version of "Hello World" Program
/********************************/ /* A simple Hello World program */ /********************************/ #include<iostream.h> #include<conio.h> // declearation of header directives /* declertion of main function */
int main(){ // main function starts by a "{" bracket cout << "Hello World !!"; // the above line prints the text to the output cout << endl; // inserts a new line break cin.get(); // waits to take a hit form the user to end return 0; // returns value 0 } // main function ends by a "}" bracket /* Program ends here */
Explanation of Headers
The program begins with a "#include" . It indicates a preprocessor directive. By writing such it tells the compile to insert code form the header "iostream" to the program. It's also necessary to access several library functions. So, you have to include several required header files based on your program.
Explanation of Comments
In programming, comments are used to describe the program's work or the use of any function etc. Comments are ignored by compiler. Comments can be implemented by the following ways:
- /* text comment */: Compiler ignores Al's between /* and */. This means text comment.
- /** documentation comment*/: Also ignored by compiler and used to as a documentation comment
- // text: Compiler ignores all from // to the end of the line.
Explanation of "main()" Function
Every C/C++ program must contains one "main()" function.
"int main()" implies the compiler that there exists a function named as "main" and it is integer type. It may also be void or float or double type. If you use int than you have to return the value.
"main()" function always stayed curly braces ( { and } ). They indicate the beginning and ending of the program block.
In the "main()" function block, there is "cout" object and after it "<<" symbol. "<<" is called "insertion operators". It indicates the output. After the "<<" symbol there are some text which are between two double inverted commas (" Hello World !! "). The text which are in between the double inverted commas are seen as the output when the program runs.
Every expression in C++ program ends with a semicolon (;).
Explanation of "cout"
"\n" refers a single character and means newline. If you use it, then the cursor will go to the next line. You can also use "endl" which also does the same task.
Explanation of "cin.get()"
The function call "cin.get()" reads the inputs and waits until the user hits the enter key of keyboard.
Step 02 : Compiling the program
After writing the program, it's time to run your simple program. But before that you have to check whether the program contains any error or not. If it has any error, you need to solve the error or errors.
For compiling your program, you have to press "Alt-F9". It then compiles and shows whether any error exists or not. If you find any error then try to solve them.
Explanation of "return 0"
Finally, "return 0" which returns O. It's use is mandatory if we use integer or float etc. type for declaration of "main()" function. To avoid this line, you can use "main()" function as void type.
Step 03 : Running the program
Once you compiled your program successfully, you have done good. Now, it's time for running the program. Use "Ctrl-F9" to run the program. You will see the output "Hello World !!" on the screen.
Output of "Hello World" Program
Build Your Basic Stronger :: Questions
Here, some questions are given for enhancing your learning.
1. What are the differences between C and C++?
2. Which function must exists in any C/C++ program?
3. C++ programming supports which type of features?
4. Name some of the common compilers.
5. How to compile a C++ program?
6. How to run a C++ program?
7. Does a comment have any importance to the compiler?
Build Your Basic Stronger :: Answers
1.
- C++ supports OOP (Object Oriented Programming) concept. But C does not.
- C++ allows to create classes but C does by structures.
- C++ applications are slower than C programs during run-time.
- C++ supports polymorphism, inheritance, encapsulation. But C does not support.
2. "main()" function
3.
- encapsulation
- inheritance
- polymorphism
4.
- Borland C++
- Microsoft C++
- GNU C++
5. By "Alt+F9"
6. By "Ctrl+F9"
7. No, the compiler ignores all comments
Give your vote
Is this article helpful for you to build a good basic for programming in C/C++?
Learn more on "Programming Basics"
- Basics and Concepts of Programming
For programming languages, several terms are used. Some important terms i.e.