Writing First C/C++ Program : A tutorial of C/C++ Programming

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-
  1. encapsulation
  2. inheritance
  3. 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

"Hello World" - Program Output
"Hello World" - Program Output

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++?

See results without voting

Comments 8 comments

shreekrishna 7 years ago

is there doest not exist any compiler ,

which runs on white screen ,

when i am in 7th grade of school , we taught us to build program , but then i dont know on which compiler the program runs,

they run on a white screen & we can build the different shapes , i dont know which language we are using that time.

did you know anything about that ,

i never run any program after 5 years of that.


rancidTaste profile image

rancidTaste 7 years ago Author

To run any program, you must need a compiler. There are many compilers and the color of any compiler can also be customizable. For C/C++ programming, many people use Mircosoft Visual C++. This compile is coding environment is white but when you run, it executes in black screen.


nicomp profile image

nicomp 7 years ago from Ohio, USA

GCC is another useful C++ compiler; it's also free.


Eric Graudins profile image

Eric Graudins 7 years ago from Australia

Crikey, I used to have brain cells that knew that stuff!

difficult topic for a hub, but you've covered it well.

Cheers, Eric G.


masanghi profile image

masanghi 6 years ago

Good hub! On Windows it makes sense to use Visual C++, so I wrote a Hub about setting up a basic project in it, linking to this.


joana 5 years ago

it's really helpful for a student who is 1st tym in programming.

thanks a lot


Java Programs profile image

Java Programs 4 years ago from India

One of the best hub about C and C++ programming .... Thanks for such hub ...... great .... keep the good work up ....


klanguedoc profile image

klanguedoc 4 years ago from Canada

Excellent hub c/c++. I really enjoyed it. You covered the subject well. Although VC++ is still the dominate C++ environment, others are returning and will probably overtake VC++ in the future just like Borland C++ used to be king.

I am talking about QT environment, which is more or less free (http://qt.nokia.com/products/)

and also from Apple. The Xcode IDE also provides native support for C/C++ development, but for the Mac/iOS plateforms.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    More by this Author


    Click to Rate This Article
    working