ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on November 2, 2008

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
working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)