Himanshu

Negi

Developer & Internet Marketer


Welcome to my blog.

Sed sed leo sit amet elit sagittis fermentum. Nulla posuere commodo erat. Nam ac nunc ipsum, id espara tincidunt orci. Quisque adipiscing molestie dolor, ut congue sem porta et. Sed sapien urna, auctor et accumsan ut, accumsan sit amet dui. Nunc tristique interdum magna, dictum volutpat sem. Nam ac nunc ipsum, id espara tincidunt orci. Sed sapien urna, auctor et accumsan ut.

How to Write Your First C++ Program?

April 29, 2017wp-himu0 Comments

I am glad to see you here. I know you are new to C++ and you have next to no experience with programming languages but trust me you’ll soon love it. I will make you understand each and every aspect of programming esp. C++. Are you ready to write your first C++ program?

Before you write your first C++ program make sure you have a working C++ compiler in your system or you can use an online compiler for now. A compiler converts a programming language into binary language (also called machine language), that computers understand easily but not humans.

The first program we’re going to write is the simplest C++ program, known as ‘Hello world program’.

[codesyntax lang=”cpp” title=”My first C++ Program” bookmarkname=”My first C++ Program”]

#include<iostream.h>

void main() 
{ 
     cout<<”Hello, world!”;
}

[/codesyntax]

This program doesn’t do much but display a simple message (i.e. Hello, world!) on the screen (console). If you’ll compile this program right now, you’ll not see the message because computer displays messages and exits the program so quickly that humans can’t notice anything happened.

To see the “hello, world!” message on screen we can easy hold that program before it exits but before that let’s learn the meaning of above important lines.

Understand Your First C++ Programming Code:

#include<> is used to include header files in our program, with the help of this code we can add any number of header files.

iostream.h is a header file and contains the meaning of ‘cout’. Header files are added to a program so that compiler can understand the words that we are going to use in our program. Header files are like dictionary for a compiler where it can learn more about a particular predefined keyword/code. Each header files contain meanings of some particular words.

Every C++ program will have at least a function. A function is the group of line of codes that performs a particular task. A program without a main() can never run. Main() is also a function. A function can do anything depending upon what code programmer writes in it.

The ‘void’ before the main function shows that the function is of void type, for now you doesn’t need to know about that (return types), I will explain it later on when there is a need.

Brackets {} shows the scope of function, they help to bind these lines of code under a single unit (in case called, main function).

Cout (read as c-out) means that the program need to display a message on console (screen) that is written inside the double quotes. << are known as output operators. C in cout stands for console.

A semi-colon (;) is used to terminate a C++ programming statement, just like we have a full-stop in English.

Now you have learned the meaning of all used C++ keywords. Let’s try to do something to hold the program screen. A little code to is enough, which will hold the program until you press any key.

[codesyntax lang=”cpp” title=”Hello world C++ program” bookmarkname=”Hello world C++ program”]

// Hello world program in C++

#include<iostream.h>
#include<conio.h>

void main() 
{ 
    cout<<”Hello, world!”; 
    getch(); 
}

[/codesyntax]

conio.h is another header files that contain the meaning of getch() that holds the screen until you press any keyboard key.

Double slash (//) is used for commenting purpose. It can be insert anywhere and the whole line after it becomes a comment. Compiler ignores it and never compiler it because it’s meant for humans only.

Tips for writing your first C++ program:

  • This is your first program in C++ so don’t get scared.
  • By default, C++ compiler saves all files in .c format but you have to make it .cpp (helloworld.cpp is fine)
  • All the programming is done in lower case.
  • You can insert anything between double quotes except a “ itself. Use a back-slash (\) before “ to insert it.

Exercise: Try to display you name on the screen.


Leave a Reply
www.edatastyle.com www.introducingsomething.com