fbpx

Introduction to Data Types and Type Specifiers in C++ Programming

C++

INTRODUCTION

C++ is one of the most powerful and versatile programming languages, widely used in software development, game development, and system programming. To truly master this language, it’s essential to start with its basics, as they lay the foundation for writing efficient, readable, and maintainable code.

In this blog, we’ll explore key fundamental concepts such as comments, tokens, keywords, and operators, which form the backbone of C++ programming. Comments, for instance, play a crucial role in improving code readability and providing descriptions or context for complex logic. Whether it’s single-line or multi-line comments, their purpose remains the same: to document your code and make it easier to understand—even years later.

Basics in c++:

  • Comments does not paritipate in the execution.
  • It clearly explains about the readability ,description of the program.purpose of the program
  • The primary purpose of comments is to provide additional context or details about specific lines of code.
  • Programmers frequently use comments to document their work and improve code maintainability.

Why Comments are used in C++?               

It makes a program more readable

It gives description of the code.

After a long time u r recap the code means the code means the comments says the program what is supposed to do.

  • Types of comments :

Single line(comments) : single line does not pariticipate in the execution. The compiler is to ignore the  that single line after this comment.

#include <iostream>

using namespace std;

int main()

{

                // compiler

                cout << “iies!”;

                return 0;

}

Multiline comments:

Multiple lines of comments:ignores a multi line comments by the compiler

 

// C++ Program to demonstrate Multi line comment

#include <iostream>

using namespace std;

 

int main()

{

                /* Multi line comment which

                will be ignored by the compiler

                */

                cout << “Hello iies!”;

                return 0;

}

  • Tokens:

Every single element in the c program is said to be an token.

Tokens

  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Special Symbols
  • Operators

Keywords:

1.Reserved  words

2.Predefined words.

3.we cannot redefine keyword  for  example( int  int 4);

Standard

Total Keywords

C++98/C++03

63

C++11

74 (11 new keywords added)

C++14

75 (1 enhancement to constexpr)

C++17

75

C++20

82 (7 new keywords added)

C++23

84 (2 new keywords added)

  1. Inherited from C:
    • 32 keywords (e.g., int, return, if, while).
  2. Added in C++:
    • Additional keywords for object-oriented programming, templates, and advanced features (e.g., class, virtual, template, namespace).
  3. Modern Features:
    • Introduced in newer standards for concepts, concurrency, and coroutines (e.g., constexpr, concept, co_await, requires)

asm

double

new

switch

auto

else

operator

template

break

enum

private

this

case

extern

protected

throw

catch

float

public

try

char

For

register

typedef

class

friend

return

union

const

goto

short

unsigned

continue

if

signed

virtual

default

inline

sizeof

void

delete

Int

static

volatile 

do

long

struct

while 

Storage Class Specifiers

1. auto:

auto x = 10; // Type deduced as int

    • In earlier C++, it specified automatic storage duration (default for local variables).

2. register:

      • Suggests that the variable should be stored in a CPU register for faster access (modern compilers often ignore this).

3. static:

      • Used to define:
        • Variables with static storage duration.
        • Members belong to the class, not an instance.

4. extern:

        • Declares a variable or function that is defined in another file or scope.

Type Specifiers

5. char, int, float, double, short, long, signed, unsigned:

    • Specify data types.
    • Example: unsigned int defines a non-negative integer type.

6. void:

      • Specifies a function that does not return a value.

Control Flow

7. if, else:

    • Conditional statements for decision-making.

8. switch, case, default:

      • Used for multi-way branching.

switch (x) {

    case 1: /* Code */; break;

    default: /* Code */;

}

9. for, while, do:

    • Looping constructs:
      • for: For a specified number of iterations.
      • while: Iterates as long as a condition is true.
      • do-while: loop runs at least once.

10. break, continue:

        • Control loop flow:
          • break: Exit the loop immediately.
          • continue: Skip the current iteration.

11. goto:

            • Transfers control to a labeled statement (rarely used due to readability concerns).

Object-Oriented Keywords

12. class:

    • used to define user-defined types (blueprints for objects).

class myclass {    public:        int x;};

13. public, private, protected:

    • access specifiers:
      • public: accessible from anywhere.
      • private: accessible only within the class.
      • protected: accessible within the class and derived classes.

14. virtual:

    • specifies a function for runtime polymorphism.

15. this:

    • a pointer to the current object.

16. friend:

    • grants another class or function access to private/protected members.

17. template:

    • enables generic programming by defining functions/classes that work with any data type.

 template <typename t>

18. operator:

    • overloads operators for user-defined types.

19. typedef:

    • creates an alias for an existing type.

 typedef unsigned int uint;

20. union:

    • defines a data structure where all members share the same memory space.

Exception handling:

21. try, catch, throw:

    • handle runtime errors (exceptions).

 try {    // code that may throw} catch (exception& e) {    // handle exception

22. return:

    • specifies the return value of a function.

23. inline:

    • suggests the compiler replace function calls with the function’s body to reduce overhead.

24. sizeof:

    • returns the size of a data type or object in bytes.

int x = sizeof(int); // typically 4

25. volatile:

    • indicates a variable can be changed unexpectedly (e.g., hardware registers, multi-threading).

26. const:

    • declares variables whose values cannot be modified after initialization.

27. asm:

    • allows embedding assembly language instructions directly in c++ code (rarely used).

Constants:

Constants  in c  it cannot change the value once it is defined.

// C program to illustrate constant variable definition

#include <stdio.h>

 

int main()

{

 

    const int integer_const = 34;

 

    const char character_const = ‘B’;

 

    const float float_const = 12.22;

 

    printf(“Printing value of Integer Constant: %d\n”,

           integer_const);

    printf(“Printing value of Character Constant: %c\n”,

           character_const);

    printf(“Printing value of Float Constant: %f”,

           float_const);

 

    return 0;

}

Strings in c++:

——————-

// C++ Program to demonstrate strings

#include <iostream>

using namespace std;

 

int main()

{

    char s[] = “”;

    cout << s << endl;

    return 0;

}

 

// C++ program to create std::string objects

#include <iostream>

using namespace std;

 

int main()

{

 

    string str(“iies”);

    cout << str;

    return 0;

}

 

#include <iostream>

using namespace std;

 

int main()

{

    string str(5, ‘g’);

    cout << str;

    return 0;

}

Using C-style strings

// C++ Program to demonstrate C-style string declaration

#include <iostream>

using namespace std;

 

int main()

{

 

    char s1[] = { ‘g’, ‘f’, ‘g’, ‘\0’ };

    char s2[4] = { ‘g’, ‘f’, ‘g’, ‘\0’ };

    char s3[4] = “gfg”;

    char s4[] = “gfg”;

 

    cout << “s1 = ” << s1 << endl;

    cout << “s2 = ” << s2 << endl;

    cout << “s3 = ” << s3 << endl;

    cout << “s4 = ” << s4 << endl;

 

    return 0;

}

Other Operators:

Semicolon (;): It is used to terminate the statement.

Square brackets []: They are used to store array elements.

Curly Braces {}: They are used to define blocks of code.

Scope resolution (::): Scope resolution operator is used to access members of namespaces, classes, etc.

Dot (.): Dot operator also called member access operator used to access class and struct members.

Assignment operator ‘=’: This operator is used to assign values to variables.

Double-quote (“): It is used to enclose string literals.

Single-quote (‘): It is used to enclose character literals.

Operators:

  • Unary operators
  • Binary operators
  • Ternary operators

.Operators