C++ Syntax, Data Types and Reserved words.

To start coding with C++ it is mandatory to learn and know the syntax of C++ and some important notations otherwise you will not understand what is happening with elements and whats the flow of working. 


In this session we will learn the following.

C++ Data Types

C++ Variables

C++ Comments

C++ Statements

C++ Constants and Identifiers

C++ Reserved Words

Lets have an overview of all these points.

Variables:

Variables are the containers that holds data basically. In computer we need to memory space to store data or something just as in real world. In order to reserve that memory we store our data in containers that we call as variables. There values can be changed throughout the program. We may assign any name to variable except the reserved words that we will discuss later in this session.

Data Types:

In C++, data types are used to specify the type of data that a variable can store. C++ provides several built-in data types to handle different types of data. Here are some commonly used data types in C++:

  • Integer Types:

  1. int: Used to store integer values.
  2. short: Short integer.
  3. long: Long integer.
  4. long long: Long long integer.
  5. unsigned int, unsigned short, unsigned long, unsigned long long: Unsigned integer types (can't store negative values).
Example:
int age = 25;
unsigned int count = 100;

  • Floating-Point Types:

  1. float: Single-precision floating-point number.
  2. double: Double-precision floating-point number.
  3. long double: Extended precision floating-point number.
Example:
float pi = 3.14;
double gravity = 9.81;

  • Character Types:

  1. char: Used to store a single character.
  2. char16_t, char32_t: 16-bit and 32-bit character types for Unicode characters.
  3. wchar_t: Wide character type.
Example:
char grade = 'A';
  • Boolean Type:
bool: Represents boolean values true and false.
Example:
bool isStudent = true;
  • Void Type:
void: Indicates the absence of a type.
Example:
void function() {
    // This function doesn't return any value
}
  • Derived Types:
  1. Arrays: Contiguous collection of elements of the same type.
  2. Pointers: Variables that store memory addresses.
  3. References: Alias to an existing variable.
  4. Structures: User-defined composite data type that groups related data members.
  5. Enums: User-defined data type used for defining named constants.

Comments:

In C++, comments are used to add notes or explanations within the source code that are ignored by the compiler. Ofcourse not mandatory but comments are crucial for enhancing code readability, documenting code, and providing context for future developers or yourself so it is good practice to add comments maintainable and understandable code. There are two main types of comments in C++:
  • Single-Line Comments:
Single-line comments start with // and continue until the end of the line. They are used to comment out a single line of code or to add short explanations.
  • Multi-Line Comments:
Multi-line comments, also known as block comments, begin with /* and end with */. They can span multiple lines and are used to comment out large blocks of code or to add longer explanations.

Statements:

In any code statement is a complete unit of execution that carries out the action to be performed. There are different kinds of statement in C++ that we will try to cover in detail in our next sessions. Some of them are:

1. Expression Statements
2. Declaration Statements
3. Compound Statements (Blocks)
4. Selection Statements

  •     if Statement
  •     if-else Statement
  •     switch Statement
5. Iteration Statements (Loops)
  •     while Loop
  •     do-while Loop
  •     for Loop
6. Jump Statements
  •     break Statement
  •     continue Statement
  •     return Statement
Remember that every statement in c++ must end with a semicolon otherwise it will be counted as syntax error and your program execution will be stopped.

Constants and Identifiers:

Constants in C++ are values that do not change during the execution of a program. They are used to represent fixed values such as numbers, characters, or strings. Constants can be of various types, including integer constants, floating-point constants, character constants, and string literals. In C++, constants are declared using the const keyword or using preprocessor directives like #define etc. For example const double PI = 3.14159;

#define MESSAGE "Hello, world!"

While Identifiers in C++ are names given to various programming elements such as variables, functions, classes, and labels. Identifiers are used to uniquely identify these elements within the program.

 There are certain rules that must be followed while defining identifiers:

  • They can include letters (both uppercase and lowercase), digits, and underscores.
  • The first character must be a letter or an underscore.
  • Identifiers are case-sensitive.
  • They cannot be C++ keywords.
Some examples are:

int class; // variable identifier

void calculateHeight(); // function identifier

class MyBooks {}; // class identifier

const int MAX_VALUE = 320; // constant identifier

Reserved Words:

In every programming language reserve words are pre-defined identifiers that have special meaning and purposes within that language. We cannot use these words in our programs. Some of the Reserve words of C++ are:

When writing code in C++, it's important to avoid using these reserved words as identifiers for variables, functions, classes, or any other programming elements, as doing so will result in a compilation error.

Post a Comment

0 Comments