Class 8  >  C++ Programming for Beginners  >  C++ Syntax & Output

C++ Syntax & Output - Notes | Study C++ Programming for Beginners - Class 8

1 Crore+ students have signed up on EduRev. Have you?

C++ Syntax

Let's break up the following code to understand it better:

Example

#include <iostream>

using namespace std;


int main() {

  cout << "Hello World!";

  return 0;

}

Example explained

  • Line 1: #include <iostream> is a header file library that lets us work with input and output objects, such as cout (used in line 5). Header files add functionality to C++ programs.
  • Line 2: using namespace std means that we can use names for objects and variables from the standard library.

Don't worry if you don't understand how #include <iostream> and using namespace std works. Just think of it as something that (almost) always appears in your program.

  • Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
  • Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function. Any code inside its curly brackets {} will be executed.
  • Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text. In our example it will output "Hello World".

Note: Every C++ statement ends with a semicolon ;.

Note: The body of int main() could also been written as:

int main () { cout << "Hello World! "; return 0; }

Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.

  • Line 6: return 0 ends the main function.
  • Line 7: Do not forget to add the closing curly bracket } to actually end the main function.

Omitting Namespace

You might see some C++ programs that runs without the standard namespace library. The using namespace std line can be omitted and replaced with the std keyword, followed by the :: operator for some objects:

Example

#include <iostream>


int main() {

  std::cout << "Hello World!";

  return 0;

}

It is up to you if you want to include the standard namespace library or not.

C++ Output (Print Text)

The cout object, together with the << operator, is used to output values/print text:
Example

#include <iostream>

using namespace std;


int main() {

  cout << "Hello World!";

  return 0;

}

You can add as many cout objects as you want. However, note that it does not insert a new line at the end of the output:
Example

#include <iostream>

using namespace std;


int main() {

  cout << "Hello World!";

  cout << "I am learning C++";

  return 0;

}

C++ New Lines

To insert a new line, you can use the \n character:

Example

#include <iostream>

using namespace std;


int main() {

  cout << "Hello World! \n";

  cout << "I am learning C++";

  return 0;

}

Tip: Two \n characters after each other will create a blank line:

Example

#include <iostream>

using namespace std;


int main() {

  cout << "Hello World! \n\n";

  cout << "I am learning C++";

  return 0;

}

Another way to insert a new line, is with the endl manipulator:

Example

#include <iostream>

using namespace std;


int main() {

  cout << "Hello World!" << endl;

  cout << "I am learning C++";

  return 0;

}

The document C++ Syntax & Output - Notes | Study C++ Programming for Beginners - Class 8 is a part of the Class 8 Course C++ Programming for Beginners.
All you need of Class 8 at this link: Class 8
15 videos|20 docs|13 tests
Download as PDF

Download free EduRev App

Track your progress, build streaks, highlight & save important lessons and more!

Related Searches

Sample Paper

,

past year papers

,

C++ Syntax & Output - Notes | Study C++ Programming for Beginners - Class 8

,

mock tests for examination

,

Extra Questions

,

Previous Year Questions with Solutions

,

C++ Syntax & Output - Notes | Study C++ Programming for Beginners - Class 8

,

Exam

,

Viva Questions

,

Objective type Questions

,

ppt

,

practice quizzes

,

MCQs

,

C++ Syntax & Output - Notes | Study C++ Programming for Beginners - Class 8

,

Important questions

,

Free

,

Semester Notes

,

Summary

,

study material

,

pdf

,

video lectures

,

shortcuts and tricks

;