• Register
100 points
7 4

There are some words which are already defined in C++ language known as keywords. The collection of restrained words and identifiers which are pre-defined by the compiler are keywords. For example “double”, “else”, “short”, “return” and many more.

If you use a C++ keyword as an identifier, your program will:

Since keywords are pre-defined with their specific meaning in C++ language, we cannot use them as identifiers in our program. If we use a C++ keyword as an identifier in our program then our program will not compile. All the keywords are predefined by the compiler so we cannot manipulate them since it will confuse the compiler. So using keywords as identifier is restricted in C++ to prevent unexpected behaviour in our program.

Program

#include<iostream>
using namespace std;
int main()
{
    char return;
    cout << "Enter value in this keyword : ";
    cin >> return;
    cout << "I hope it will not compile and run.";
    return 0;
}

Error

keyword.cpp: In function 'int main()':
keyword.cpp:5:10: error: expected unqualified-id before 'return'
     char return;
          ^
keyword.cpp:7:12: error: expected primary-expression before 'return'
     cin >> return;
            ^

 

 In this program we used return as an identifier which is a keyword in C++ so our program will not compile. When we try to compile our program, the compiler will show an error saying that it is not valid.

List of Standard keyword predefined in C++

alignas
alignof
and b
and_eq b
asm a
auto
bitand b
bitor b
bool
break
case
catch
char
char8_t c
char16_t
char32_t
class
compl b
concept c
const
const_cast
consteval c
constexpr

constinit c
continue
co_await c
co_return c
co_yield c
decltype
default
delete
do
double
dynamic_cast
else
enum
explicit
export c
extern
false
float
for
friend
goto
if
inline

int
long
mutable
namespace
new
noexcept
not b
not_eq b
nullptr
operator
or b
or_eq b
private
protected
public
register reinterpret_cast
requires c
return
short
signed
sizeof
static
static_assert

static_cast
struct
switch
template
this
thread_local
throw
true
try
typedef
typeid
typename
union
unsigned
using declaration
using directive
virtual
void
volatile
wchar_t
while
xor b
xor_eq b

100 points
7 4