Class 10 Exam  >  Class 10 Notes  >  C++ Programming for Beginners  >  C++ Syntax & Output

C++ Syntax & Output | C++ Programming for Beginners - Class 10 PDF Download

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 | C++ Programming for Beginners - Class 10 is a part of the Class 10 Course C++ Programming for Beginners.
All you need of Class 10 at this link: Class 10
15 videos|20 docs|13 tests

Top Courses for Class 10

15 videos|20 docs|13 tests
Download as PDF
Explore Courses for Class 10 exam

Top Courses for Class 10

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

C++ Syntax & Output | C++ Programming for Beginners - Class 10

,

past year papers

,

study material

,

Important questions

,

Previous Year Questions with Solutions

,

Semester Notes

,

Sample Paper

,

C++ Syntax & Output | C++ Programming for Beginners - Class 10

,

Summary

,

ppt

,

shortcuts and tricks

,

MCQs

,

mock tests for examination

,

Viva Questions

,

practice quizzes

,

Free

,

Exam

,

video lectures

,

Extra Questions

,

pdf

,

C++ Syntax & Output | C++ Programming for Beginners - Class 10

,

Objective type Questions

;