C++ Basic Syntax

0
24

C++ program is a collection of objects which communicates each other via methods. It is a collection of statements which gets executed in a sequence, hence it is called a sequential programming language. Each statement is combination of various legal symbols. These symbols are smallest individual unit of a program. These are called as C++ tokens and classified into

  • Keywords – re-existing, reserved words, each holding its own position and power and has a specific function associated with it. These can not be used as variable name.
  • Identifiers – a name that identifies (that is, labels the identity of) a variable, arrays, functions, data structures, unique object and a unique class of objects.
  • Constants/Literals – referred to as fixed values that cannot change their value during the entire program run as soon as we define them
  • Strings – an array of characters as well as an individual data type.
  • Operators – tools or symbols which are used to perform a specific operation on data. Operations are performed on operands.
  • Special Symbols – [] () {} , # * ~ . are special symbols, which help you manipulate or perform data operations. Each special symbol has a specific meaning to the C++ compiler.

Rules for C++ Identifiers

There are certain rules to be followed by the user while naming identifiers, otherwise, you would get a compilation error. These rules are:

  1. First character: The first character of the identifier in C++ should positively begin with either an alphabet or an underscore. It means that it strictly cannot begin with a number.
  2. No special characters: C++ does not encourage the use of special characters while naming an identifier. It is evident that we cannot use special characters like the exclamatory mark or the “@” symbol.
  3. No keywords: Using keywords as identifiers in C++ is strictly forbidden, as they are reserved words that hold a special meaning to the C++ compiler. If used purposely, you would get a compilation error.
  4. No white spaces: Leaving a gap between identifiers is discouraged. White spaces incorporate blank spaces, newline, carriage return, and horizontal tab.
  5. Word limit: The use of an arbitrarily long sequence of identifier names is restrained. The name of the identifier must not exceed 31 characters, otherwise, it would be insignificant.
  6. Case sensitive: In C++, uppercase and lowercase characters connote different meanings.

List Of Special Symbols


Special Character
Trivial NameFunction
[ ]Square bracketsThe opening and closing brackets of an array symbolize single and multidimensional subscripts.
()Simple bracketsThe opening and closing brackets represent function declaration and calls, used in print statements.
{ }Curly bracesThe opening and closing curly brackets to denote the start and end of a particular fragment of code which may be functions, loops or conditional statements
,CommaWe use commas to separate more than one statements, like in the declaration of different variable names
#Hash / Pound / PreprocessorThe hash symbol represents a preprocessor directive used for denoting the use of a header file
*AsteriskWe use the asterisk symbol in various respects such as to declare pointers, used as an operand for multiplication
~TildeWe use the tilde symbol as a destructor to free memory
.Period / dotThe use the dot operator to access a member of a structure

Comments

Explanatory statements, included in the C++ code, help anyone reading the source code.

Like any other language C++ supports comments and classifies comments into single-line and multi-line comments. All characters available inside any comment are ignored by C++ compiler.

C++ comments start with /* and end with */. For example −

/* This is a comment */

/* C++ comments can also
   * span multiple lines
*/

A comment can also start with // extending to the end of the line.

#include <iostream>
using namespace std;

main() {
   cout << "Hello World"; // prints Hello World
   
   return 0;
}

When the above code is compiled, it will ignore // prints Hello World and final executable will produce the following result −

Within a /* and */ comment, // characters have no special meaning. Within a // comment, /* and */ have no special meaning. Thus, you can “nest” one kind of comment within the other kind. For example −

Hello World
/* Comment out printing of Hello World:

cout << "Hello World"; // prints Hello World

*/
Previous articleC++ Environment Setup
Next articleC++ Data Types
Vishal Bhandari is an admin, editor and writer of this portfolio. He writes about himself, his interests, hobbies, activities. He manages many articles about tech, finance, history and geopolitics on his portfolio.

LEAVE A REPLY

Please enter your comment!
Please enter your name here