Lesson 3

After writing your first program in C/C++ let’s know some basic syntax of C/C++. Some of the things mentioned here were already discussed in previous lesson. Remember as basic syntax of C/C++ are same so this lesson will cover both language. Example for both C/C++ will be given you just have to considerate on language your learning.

Semicolon

In C/C++, semicolon (also called statement terminator) represents end of a statement.  Not only in C/C++ in any language semicolon is used to terminate a statement. Remember there is a difference between end of line and end of statement as we’ve discussed a single line C program can’t be a statement a statement can consist of multiple lines. There are some languages which don’t use semicolon but they won’t allow multiple statement in a single line. End of a statement is semicolon and end of a line is whitespace (Enter).

C Code Example


printf(“Hello World!”); return 0;

Above example shows multiple statement on a single line.


printf(“Hello,
World”);

Above example shows a single statement of C program on two lines.

C++ Code Example


cout << “Hello, World!”; return 0;

Above example shows multiple statement on a single line.


cout << “Hello, “
<< “World”;

Above example shows a single statement of C++ program on two lines.

Comments

Comments are used to give additional useful information to the programmer about a particular line or a particular function. Comments are ignored by the compilers; they are only used to give information to programmers. Comments are like helping text in your C/C++ program they help you to understand code in future or even help other programmers which are reading your code. Using comment is good practice. You can’t have comments within comments or even in string in usual comments are written before statement or after statement.

In C/C++, there are two types of comments.

  1. Single Line Comments; they are expressed by “//”. Single line comment can only comment a single line (And from line I mean whitespace line not a statement).
  2. Multiple Line Comments; these comments start with /* and ends at */ everything written between them will consider as comment. An opening comment must be close otherwise compiler will give syntax error.

C Code Example


printf(“Hello, World!”); //This is Single line comment

/* This is
Multiple Line 
Comment */

printf(“Hello, World!”);

C++ Code Example


cout “Hello, World!”; //This is Single line comment

/* This is
Multiple Line 
Comment */

cout “Hello, World!”;

Other use of comments is, suppose your testing your code and there is a statement that you think causing an error and you want to check it instead of deleting that line or then again writing it you can simple comment it like


//return 0;

Now this return 0; will consider as comment and will not be compile by compiler. Similarly, you can use multiline comments for multiple statements.

Keywords

C/C++ program consist different keywords. Keyword have standard, predefined meaning in C/C++. Compiler of C/C++ knows what a keyword means or what’s it functionality. A basic C/C++ program is made up of keywords like main, for, int, if etc. keywords are all in lower case. There are 32 keywords available in C and 87 keywords in C++. Keywords are also known as “Reserve Words”.