Tute

C++ Program Structure?

C++_Program_Structure

C++ Program

Before starting C++ language, you must need to learn how to write, compile and run the first C++ program.

Write a first C++ program

First open the C++ console and write the following code.  

  • #include
  • #include
  • void main() {  
  •  clrscr();  
  • cout << "Welcome to C++ Programming.";   
  • getch();  
  • }  
  1. #include includes the standard input output library functions.
  2. It provides cin and cout methods for reading from input and writing to output respectively.
  3. #include  includes the console input output library functions.
  4. The getch() function is defined in conio.h header file.
  5. void main() The main() function is the entry point of every program in C++ language.
  6. The void keyword specifies that it returns no value.
  7. cout << "Welcome to C++ Programming." is used to print the data "Welcome to C++ Programming." on the console.
  8. getch() The getch() function asks for a single character. and holds the screen Until you press any key.

 

Cpp Program 1


How to compile and run the C++ program

There are 2 ways to compile and run the C++ program.

By menu

  • click on the compile menu then compile sub menu to compile the c++ program.
  • Then click on the run menu then run sub menu to run the c++ program.

By shortcut

  • Or, press ctrl+f9 keys compile and run the program directly.
  • output on user screen.

Cpp Program 2

  • You can view the user screen any time by pressing the alt+f5 keys.
  • press Esc to return to the turbo c++ console.

In Other Word

C++ Program Structure

a simple program code that would print the welcome to rltute.

#include
#include
using namespace std;

// main() is where program execution begins.

int main(){

cout << "welcome to rltute"; // prints welcome to rltute

return 0; }

there are various parts of the above program −

  1. The C++ language defines several headers files, which contain information that is necessary or useful to your program.

  2. The header  is needed.

  3. The line using namespace std; tells the compiler to use the std namespace.

  4. Namespaces are a relatively recent addition to C++.

  5. The next line // main() is where program execution begins. is a single-line comment available in C++.

  6. Single-line comments begin with // and stop at the end of the line.

  7. The line int main() is the main function where program execution begins.

  8. The next line cout << "welcome to rltute"; causes the message "welcome to rltute" to be displayed on the screen.

  9. The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.

Compile and Execute C++ Program

How to save the file, compile and run the program. 

  1. Open a text editor and add the code as above.

  2. Save the file as: hello.cpp

  3. Open a command prompt and go to the directory where you saved the file.

  4. Type g++ hello.cpp and press enter to compile your code.

  5. If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.

  6. Now, type a.out to run your program.

  7. You will be able to see welcome to rltute printed on the window.

welcome to rltute

Related Topics