List of Keywords in C++ and their functions
Keyword: A C++ programming language, keywords are predefined, reserved words that have special meanings to the compiler. C++ Keywords cannot be used as identifiers (i.e., names of variables, functions, etc.) in your code.
Token: In the C++ programming language, a token is the smallest unit of a program that is meaningful to the compiler. Tokens can be individual words (such as keywords, identifiers, and constants), punctuation marks, or special symbols.
Here are some examples of tokens in C++:
Keywords: int, for, while
Identifiers: x, sum, my_function
Constants: 5, 3.14, 'a'
Punctuation marks: {, }, (, ), ;
Special symbols: +, -, *, /, %
The C++ compiler breaks a program down into tokens and then processes them according to the rules of the language. This process is known as lexical analysis or tokenization. The resulting tokens are then passed to the compiler's parser, which determines the structure and meaning of the program based on the relationships between the tokens.
Best C++ Keywords All List Explanation |
keyword: In programming languages, a keyword is a word that is reserved by the language for a specific purpose and cannot be used as an identifier (i.e., a name for a variable, function, etc.). Keywords are typically used to represent programming constructs or language features that cannot be expressed using ordinary identifiers.
Keywords are an important part of the syntax of a programming language, and they are often used to signal the beginning or end of a particular construct or block of code. For example, in C-style languages like C++, keywords such as for, while, and if are used to define loops and conditional statements. In object-oriented languages like Java and C#, keywords such as class, interface, and extends are used to define types and inheritance relationships.
Different programming languages have different sets of keywords, and the exact set of keywords for a particular language may change over time as the language evolves. Some languages also allow users to define their own keywords or reserved words, although this is usually not recommended as it can lead to confusion and compatibility issues.
Identifiers: In programming languages, an identifier is a name given to a variable, function, or other programming construct. Identifiers are used to refer to these constructs in the code, and they can be used to give names to things like variables, functions, classes, and objects.
The rules for naming identifiers vary from language to language, but most languages have some basic rules in common. For example:
1. Identifiers can typically contain letters, digits, and underscores, but they cannot begin with a digit.
2. Identifiers are usually case-sensitive, so x and X are considered to be different identifiers.
3. Most languages have a set of reserved words or keywords that cannot be used as identifiers.
Constants: In programming languages, a constant is a value that cannot be changed during the execution of a program. Constants are typically used to represent fixed values that are used frequently in a program, such as mathematical constants, physical constants, or string literals.
In most programming languages, constants are defined using a keyword or directive, and they are usually assigned a value when they are defined. For example, in C++, you can define a constant using the const keyword.
const double PI = 3.14159; const int MAX_ITERATIONS = 100; const char* MESSAGE = "Hello, World!"; |
How Many Keywords in C++ Wikipedia
auto | break | case |
char | continue | default |
do | double | else |
enum | extern | float |
for | goto | if |
inline | int | long |
register | return | short |
signed | sizeof | static |
struct | switch | typedef |
union | unsigned | void |
volatile | while | friend |
virtual | public | private |
this | class | template |
try | delete | protected |
Note that this list includes some keywords that are specific to C++ and not present in the C programming language. There are also a few additional keywords that are only available in certain C++ versions or in certain contexts.
C++ Keywords List
C++ auto Keyword
auto keyword: In C++, the auto keyword is used to specify that the type of variable should be deduced from its initializer. This can be useful when the type of the variable is complex or not easily written out, or when the type is subject to change, and you want to avoid having to update the declaration every time it changes.
auto x = 5; // x is an int auto y = 3.14; // y is a double auto z = "hello"; // z is a char* |
In each of these cases, the type of the variable is deduced from the type of the initializer on the right-hand side of the assignment. x is an int because 5 is an int, y is a double because 3.14 is a double, and z is a char* because "hello" is a string literal of type char[6] that decays to a char*.
break Statement in C++ Example
break keyword: In C++ and many other programming languages, the break keyword is used to exit a loop or switch statement prematurely. When a break statement is encountered inside a loop or switch, control is immediately transferred to the statement following the loop or switch.
for (int i = 0; i < 10; i++) { if (i == 5) { break; } cout << i << endl; // prints 0 1 2 3 4 } |
switch (x) { case 0: cout << "x is zero" << endl; break; case 1: cout << "x is one" << endl; break; default: cout << "x is something else" << endl; } |
In this example, the value of x is compared to the values specified in each case clause. If x is equal to 0, the block of code following the case 0: clause will be executed. If x is equal to 1, the block of code following the case 1: clause will be executed. If x is not equal to either 0 or 1, the block of code following the default: clause will be executed.
The case keyword is typically followed by a constant expression or a variable of a type that can be compared using the == operator. If the value of the expression matches the value of the case, the block of code following the case will be executed.
char keyword: In C++, the char keyword is used to declare variables of type char, which is a built-in data type that represents a single character.
A char variable can store any character from the ASCII character set, which includes the letters of the alphabet, digits, punctuation marks, and special characters. A char variable is typically stored in a single byte of memory and can hold values from -128 to 127 (if signed) or 0 to 255 (if unsigned).
char ch = 'A'; |
continue Keyword in Cpp
continue keyword: In C++ and many other programming languages, the continue keyword is used to skip the remainder of the current iteration of a loop and move on to the next iteration.
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } cout << i << endl; } |
In this example, the continue statement is encountered when i is an even number, causing the loop to skip the remainder of the current iteration and move on to the next one. The loop will iterate 10 times and print the values 1, 3, 5, 7, and 9, but it will not print the even numbers.
class keyword: class keyword is used to declare a class, which is a user-defined data type that can contain data members (variables) and member functions (methods). A class is a template for creating objects, and an object is an instance of a class.
The class keyword is used to define a class in C++, and the class definition specifies the data members and member functions that the class will contain. Class definitions are typically placed in a separate header file, which can be included in other source files to use the class.
class Example { private: int x; int y; public: Example(int x, int y) : x(x), y(y) {} int getX() const { return x; } int getY() const { return y; } void setX(int x) { this->x = x; } void setY(int y) { this->y = y; } }; |
default access specifier in C++
default keyword: In C++ and many other programming languages, the default keyword is used in a switch statement to specify a block of code to be executed when the value of the switch expression does not match any of the case labels.
switch (x) { case 0: cout << "x is zero" << endl; break; case 1: cout << "x is one" << endl; break; default: cout << "x is something else" << endl; } |
In this example, the default clause is used to specify a block of code to be executed when the value of x does not match either 0 or 1. If x is equal to 0, the block of code following the case 0: clause will be executed. If x is equal to 1, the block of code following the case 1: clause will be executed. If x is not equal to either 0 or 1, the block of code following the default: clause will be executed.
do keyword: In C++ and many other programming languages, the do keyword is used to start a do-while loop. A do-while loop is a type of loop that executes a block of code at least once and then continues to repeat the block as long as a certain condition is true.
int i = 0; do { cout << i << endl; i++; } while (i < 10); |
double x = 3.14159; double y = 1.0 / 3.0; |
if (x > 0) { // code block to execute if x is greater than 0 } else { // code block to execute if x is not greater than 0 } |
enum Color { RED, GREEN, BLUE }; int main() { Color c = RED; if (c == RED) { cout << "The color is red." << endl; } return 0; } |
C++ extern Keyword
// data.h extern int x; // data.cpp int x = 42; |
float x = 3.14159; float y = 1.0 / 3.0; |
for (int i = 0; i < 10; i++) { // code block to execute 10 times } |
for (int i = 0; i < 10; i++) { if (i == 5) { goto skip; } cout << i << endl; } skip: cout << "Skipped the number 5." << endl; |
inline Keyword C++
inline int max(int x, int y) { return (x > y) ? x : y; } int main() { int a = 10; int b = 20; int c = max(a, b); cout << c << endl; // Output: 20 return 0; } |
int x = 42; int y = -1; int z = 0; |
long x = 1234567890; long y = -9876543210; long z = 0; |
C++ register Keyword
register int x = 42; register int y = 10; int main() { int z = x + y; cout << z << endl; return 0; } **********OUTPUT********** 52 |
int add(int x, int y) { int z = x + y; return z; } int main() { int a = 10; int b = 20; int c = add(a, b); cout << c << endl; return 0; } **********OUTPUT********** 30 |
short x = 12345; short y = -9876; short z = 0; |
signed int x = 42; signed int y = -1; signed int z = 0; |
sizeof(int) = 4 bytes sizeof(double) = 8 bytes sizeof(x) = 4 bytes |
class Example { public: static int x; static int y; }; int Example::x = 10; int Example::y = 20; int main() { Example e1; Example e2; cout << e1.x << endl; cout << e2.y << endl; return 0; } **********OUTPUT********** 10 20 |
struct Student { string name; int age; float grade; }; int main() { Student s1; s1.name = "Ravi"; s1.age = 18; s1.grade = 3.8; Student s2; s2.name = "Bob"; s2.age = 19; s2.grade = 3.5; cout << s1.name << " is " << s1.age << " years old and has a grade of " << s1.grade << endl; cout << s2.name << " is " << s2.age << " years old and has a grade of " << s2.grade << endl; return 0; } |
int x = 2; switch (x) { case 1: cout << "x is 1" << endl; break; case 2: cout << "x is 2" << endl; break; case 3: cout << "x is 3" << endl; break; default: cout << "x is not 1, 2, or 3" << endl; break; } |
typedef int Integer; Integer x = 10; Integer y = 20; cout << x + y << endl; **********OUTPUT********** 30 |
union Data { int i; float f; char c; }; int main() { Data d; d.i = 10; cout << d.i << endl; // Output: 10 d.f = 3.14; cout << d.f << endl; // Output: 3.14 d.c = 'A'; cout << d.c << endl; // Output: A return 0; } |
unsigned int x = 42; unsigned int y = 0; unsigned int z = UINT_MAX; |
void printMessage() { cout << "Hello, world!" << endl; } void main() { printMessage(); return 0; } **********OUTPUT********** Hello, world! |
volatile int x; void readValue() { x = readFromHardware(); } int main() { readValue(); cout << x << endl; return 0; } |
int x = 0; while (x < 10) { cout << x << endl; x++; } |
class Example { private: int x; int y; public: Example(int x, int y) : x(x), y(y) {} friend int add(const Example& e); }; int add(const Example& e) { return e.x + e.y; } int main() { Example e(10, 20); cout << add(e) << endl; return 0; } **********OUTPUT********** 30 |
class Shape { public: virtual double area() const = 0; // Pure virtual function }; class Circle : public Shape { private: double radius; public: Circle(double radius) : radius(radius) {} double area() const override { // Overrides the virtual function in the base class return 3.14 * radius * radius; } }; int main() { Circle c(10); cout << c.area() << endl; // Output: 314 return 0; } **********OUTPUT********** 314 |
class Example { public: int x; // Public member int y; // Public member private: int z; // Private member }; int main() { Example e; e.x = 10; // Valid e.y = 20; // Valid e.z = 30; // Invalid (private member) return 0; } |
this Keyword in C++
class Example { private: int x; int y; public: Example(int x, int y) : x(x), y(y) {} void setX(int x) { this->x = x; // Assigns value to the x member of the object } void print() { cout << "x = " << x << ", y = " << y << endl; } }; int main() { Example e(10, 20); e.setX(5); e.print(); // Output: "x = 5, y = 20" return 0; } **********OUTPUT********** x=5 y=20 |
#include <iostream> #include <stdexcept> int divide(int x, int y) { if (y == 0) { throw std::invalid_argument("divide by zero"); } return x / y; } int main() { try { int result = divide(10, 0); std::cout << result << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; } |
0 Comments