Hello world code in cpp

Содержание
  1. Hello World Hello World Hello World
  2. Source File hello.cpp … : File hello.cpp … : hello.cpp
  3. Compiling hello.cpp … : Compiling
  4. Terminology
  5. Compiler Flags
  6. Don’t Use using namespace std; ! using namespace std; Don’t!
  7. Hello World Program in C++ with Code Explanation
  8. Your First Program: C++ “Hello World!” Explanation
  9. Explanation of C++ Hello World Program Code
  10. Summary
  11. Writing First C++ Program – Hello World Example
  12. General Architecture of C++ Program
  13. C++ Hello World Program
  14. C++
  15. The complexity of the above method
  16. Working of Hello World C++ Program
  17. 1. // C++ program to display “Hello World”
  18. 2. #include
  19. 3. using namespace std
  20. 4. int main()
  21. 6. std::cout This line tells the compiler to display the message “Hello World” on the screen. This line is called a statement in C++. Every statement is meant to perform some task. A semi-colon ‘;’ is used to end a statement. The semi-colon character at the end of the statement is used to indicate that the statement is ending there. The std::cout is used to identify the standard character output device which is usually the desktop screen. Everything followed by the character “More on Input/Output. 7. return 0 This is also a statement. This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function. 8. Indentation As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program such as Hello World, it does not hold much relevance, but as the programs become more complex, it makes the code more readable, and less error-prone. Therefore, you must always use indentations and comments to make the code more readable. Must read the FAQ on the style of writing programs. Important Points Always include the necessary header files for the smooth execution of functions. For example, must be included to use std::cin and std::cout. The execution of code begins from the main() function. It is a good practice to use Indentation and comments in programs for easy understanding. cout is used to print statements and cin is used to take inputs. This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or have more information about the topic discussed above. Источник
  22. 7. return 0
  23. 8. Indentation
  24. Important Points
Читайте также:  Java считать сумму чисел

Hello World Hello World Hello World

Source File hello.cpp … : File hello.cpp … : hello.cpp

#include #include
  • this line will be replaced with the content of the file iostream
  • iostream is a header file in the compiler directory that provides input and output functionalities
  • #include » path/to/filename » ⇒ inserts the content of a file
  • #include< filename >⇒ same, but searches for file in all include directories
  • happens before compilation ⇒ compiler does only see the already preprocessed file
// our first program Comments

Comments are ignored by the compiler.

int main () main()
  • defines a function called «main»
  • every program starts by executing the main function
  • int is the only allowed return type for the main function ( int refers to an integer (whole) number)
  • () is an empty parameter list
  • blocks of statements are enclosed in curly braces
  • statements are terminated by a semi-colon ;
std::cout
  • this statement writes text to the console
  • std is the namespace of the standard library
  • cout (short for «character out») refers to the standard (console) output
  • «Hello World\n» is a string literal – a sequence of characters
  • \n is a special new line/line break character
  • the program terminates after executing the main function
  • it will automatically return 0 (indicating success) if no return statement is given
  • return codes other than 0 are interpreted as an error by the operating system executing the program

Compiling hello.cpp … : Compiling

C++ is a compiled language
  • source code can’t be run directly
  • code is written to an abstract machine model (more on that later)
  • compiler translates source code into binary machine code understood by the CPU
  • program that can be run = binary executable file containing machine code
$ g++ hello.cpp -o sayhello $ ./sayhello Hello World!
compile & link run executable "sayhello" program output

Terminology

  • Compiler Error = program not compilable, compiler will stop
  • Compiler Warning = program compilable, compiler will continue, but there is a problematic piece of code that might lead to runtime bugs
  • static = fixed at compile time (baked into the executable file, not changeable at runtime)
  • dynamic = changeable at runtime (possibly by user input)

Compiler Flags

Recommended compiler flags for your first programs

g++ -std=c++20 -Wall -Wextra -Wpedantic -Wshadow input.cpp -o output

It’s 2023 – set your compiler to C++20 (or at least to C++17 if you have to use an older compiler).

Don’t Use using namespace std; ! using namespace std; Don’t!

Many code examples / tutorials show something like this:

using namespace std; int main ()

… to avoid qualifying cout with std:: .

But:

using a namespace drags all symbols from that namespace into the global namespace. This can lead to name conflicts and ambiguities and in some cases even bugs that will only manifest at runtime and are very hard to detect.

Polluting the global namespace with all symbols from other namespaces is a serious liability in any production code base and you should avoid this anti-pattern from the very start.

Источник

Hello World Program in C++ with Code Explanation

The “Hello World” program is the first but most vital step towards learning any programming language and it is certainly the simplest program you will learn with each programming language. All you need to do is display the message “Hello World” on the output screen.

Let us now look at C++ Hello World Code:

Step 1) On configuration page. Select create the cache now option.

Hello World Program

You should see a screen something like this

Hello World Program

In some computers and operating systems, it asks whether to include all the libraries. If the option is selected, it will install all the libraries.

Step 2) Create a new source file.
Once the program opens, you need to create a new source file, so you can start writing your first C++ program. To do this select File > New > Source File. The path is shown in the figure below.

Hello World Program

This will open an area where you be able to type out your code.

Step 3) Now you can write the C++ code. After that you can write the C++ code as shown in the image below:

Hello World Program

#include using namespace std; int main()

Step 4) Compile your code. In this step, Click on Execute->compile & run

Hello World Program

Step 5) Save the file. After saving you should see a black screen outputting “Hello World.”

Hello World Program

Hello World Program

Your First Program: C++ “Hello World!” Explanation

C++ is a compiled language. The source code is compiled into object files. Object files are then combined by a linker creating an executable program.

A production C++ consists of many source code files (usually called source files).

  • Curly braces, <>, express grouping in C++. Here, they indicate the start and end of the function body.
  • Every C++ program has exactly one global function named main (). The program starts by executing that function. An int value is returned by main (), which it passes to the system.’ If no value is returned, then the system will receive a value 0 thus indicating successful completion. A nonzero value from the main () function indicates failure.

Program:C++ Hello World! Explanation

Explanation of C++ Hello World Program Code

Code line 1: The first line is #include . It instructs the compiler to include the standard stream I/O library. Without this header inclusion the expression would not compile.

Code line 4: int main(). This is the main function of the program. Functions are denoted by the parentheses(). Before the main function is “int”. This means that the main function will return an integer to the function or process that called it.

Don’t worry about this, for the time being, simply note that the program must return an integer before the end. The curly braces, < and >, contain the code within a function. The program terminates, at the end of the main function denoted by >

(Note: A string literal is a sequence of characters surrounded by double quotes. endl inserts a newline character on the same line)

Code line 7: return 0; This is the last command in the main function, the return statement. Its purpose is only to return a value to the function or process that is called the main function. Don’t worry about this other than the fact that it is required by the “int” in front of the main function definition. It should return a zero from the main function meaning the program ran successfully and exited.

Note: Cout is a stream which outputs to the stream specified. It is by default the standard output stream. Cout is very common in programs as the ultimate motive in every program is to give some output. endl; represents the end of statements in C++. The semicolon in C++ separates different statements and must be put at the end of statements in C++.

Summary

  • The “Hello World” program is the first step towards learning any programming language.
  • After installing a C++ compiler and a Text Editor of your choice, you can go ahead and execute your first basic C++ program.
  • The first line is #include . It instructs the compiler to include the standard stream I/O library.
  • : int main(). This is the main function of the program.
  • The operator
  • Return 0; is the last command in the main function which is the return statement.
  • : Cout is a stream that outputs the stream specified.

Источник

Writing First C++ Program – Hello World Example

C++ is a widely used Object Oriented Programming language and is relatively easy to understand. Learning C++ programming can be simplified into:

  • Writing your program in a text editor and saving it with the correct extension(.CPP , .C, .CP).
  • Compiling your program using a compiler or online IDE.
  • Understanding the basic terminologies.

General Architecture of C++ Program

  • Documentation.
  • Preprocessor Statements.
  • Global Declarations.
  • The main() function.
  • Local Declarations.
  • Program Statements and Expressions.
  • User-Defined Functions.

C++ Hello World Program

The “Hello World” program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. This is the basic program that is used to demonstrate how the coding process works. All you have to do is display the message “Hello World” on the screen.

Below is the C++ program for Hello World:

C++

The complexity of the above method

Time Complexity: O(1), As we are performing only constant time operations.

Auxiliary Space: O(1), As constant extra space is used.

Working of Hello World C++ Program

Let us now understand every line and the terminologies of the above program:

1. // C++ program to display “Hello World”

This line is a comment line. A comment is used to display additional information about the program. A comment does not contain any programming logic. When a comment is encountered by a compiler, the compiler simply skips that line of code. Any line beginning with ‘//’ without quotes OR in between /*…*/ in C++ is a comment. Click to know More about Comments.

2. #include

In C++, all lines that start with the pound (#) sign are called directives and are processed by a preprocessor which is a program invoked by the compiler. The #include directive tells the compiler to include a file and #include . It tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions. Click to Know More on Preprocessors.

3. using namespace std

This is used to import the entity of the std namespace into the current namespace of the program. The statement using namespace std is generally considered a bad practice. When we import a namespace we are essentially pulling all type definitions into the current scope. The std namespace is huge. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type. Click to know More about using namespace std.

4. int main()

This line is used to declare a function named “main” which returns data of integer type. A function is a group of statements that are designed to perform a specific task. Execution of every C++ program begins with the main() function, no matter where the function is located in the program. So, every C++ program must have a main() function. Click to know More about the main() function.

5.

The opening braces ‘’ indicates the ending of the main function. Everything between these two comprises the body of the main function.

6. std::cout

This line tells the compiler to display the message “Hello World” on the screen. This line is called a statement in C++. Every statement is meant to perform some task. A semi-colon ‘;’ is used to end a statement. The semi-colon character at the end of the statement is used to indicate that the statement is ending there. The std::cout is used to identify the standard character output device which is usually the desktop screen. Everything followed by the character “More on Input/Output.

7. return 0

This is also a statement. This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.

8. Indentation

As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program such as Hello World, it does not hold much relevance, but as the programs become more complex, it makes the code more readable, and less error-prone. Therefore, you must always use indentations and comments to make the code more readable. Must read the FAQ on the style of writing programs.

Important Points

  1. Always include the necessary header files for the smooth execution of functions. For example, must be included to use std::cin and std::cout.
  2. The execution of code begins from the main() function.
  3. It is a good practice to use Indentation and comments in programs for easy understanding.
  4. cout is used to print statements and cin is used to take inputs.

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or have more information about the topic discussed above.

Источник

Оцените статью