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.
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.
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;
}
Every single element in the c program is said to be an token.
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) |
asm | double | new | |
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 | signed | virtual | |
default | inline | sizeof | void |
delete | Int | static | volatile |
do | long | struct | while |
1. auto:
auto x = 10; // Type deduced as int
2. register:
3. static:
4. extern:
5. char, int, float, double, short, long, signed, unsigned:
6. void:
7. if, else:
8. switch, case, default:
switch (x) {
case 1: /* Code */; break;
default: /* Code */;
}
9. for, while, do:
10. break, continue:
11. goto:
12. class:
class myclass { public: int x;};
13. public, private, protected:
14. virtual:
15. this:
16. friend:
17. template:
template <typename t>
18. operator:
19. typedef:
typedef unsigned int uint;
20. union:
21. try, catch, throw:
try { // code that may throw} catch (exception& e) { // handle exception
22. return:
23. inline:
24. sizeof:
int x = sizeof(int); // typically 4
25. volatile:
26. const:
27. asm:
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;
}
——————-
// 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;
}
// 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;
}
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.
.
Indian Institute of Embedded Systems – IIES