Welcome to our comprehensive guide on C++ Keywords! This tutorial is designed to help both beginners and intermediate learners understand the fundamental building blocks of the C++ programming language. Let's dive in!
In programming, keywords are reserved words that have special meanings and are used to define various programming constructs. In C++, these keywords help us create variables, control flow, and define functions, among other things.
Here's a list of C++ keywords that you'll encounter frequently:
autobreakcasecatchcharclassconstconstexprcontinuedefaultdeletedodoubledynamic_castelseenumexplicitexportextensibleexternfalsefloatforfriendgotoifinlineintlongnamespacenewnoexceptnotnulloperatorprivateprotectedpublicregisterreinterpret_castreturnshortsignedsizeofstaticstatic_caststatic_assertstructswitchtemplatethisthread_localthrowtruetrytypedeftypeidtypenameunionunordered_mapunordered_setunsignedusingvirtualvoidvolatilewchar_twhilexorLet's take a look at some examples to understand how these keywords are used in practice.
int and char š”#include <iostream>
int main() {
int age = 25; // An integer variable
char name = 'A'; // A character variable
std::cout << "My age is: " << age << std::endl;
std::cout << "My name is: " << name << std::endl;
return 0;
}In the example above, int and char are C++ keywords used to declare integer and character variables, respectively.
if, else, and switch š”#include <iostream>
int main() {
int age = 25;
if (age > 18) {
std::cout << "You are an adult.";
} else {
std::cout << "You are a minor.";
}
// Using switch
int day = 3;
switch (day) {
case 1:
std::cout << "Today is Monday.";
break;
case 2:
std::cout << "Today is Tuesday.";
break;
case 3:
std::cout << "Today is Wednesday.";
break;
// Add more cases as needed
default:
std::cout << "Invalid day.";
}
return 0;
}In the second example, we use if, else, and switch to control the flow of our program.
What is the purpose of the `int` keyword in C++?
Happy coding! š