0% found this document useful (0 votes)
9 views16 pages

Keywords With Sample Case Use

C keys as words
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

Keywords With Sample Case Use

C keys as words
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

-KEYWORDS WITH SAMPLE CASE USE

1. asm

asm allows you to insert assembly language instructions within C++ code. This
is rarely used nowadays.

================================================
#include <iostream>
int main() {
int result;
asm("movl $42, %0" : "=r"(result)); // Move 42 into result using
assembly
std::cout << "Result: " << result << std::endl;
return 0;
}

2. auto

auto automatically deduces the type of a variable at compile time.

================================================
#include <iostream>
int main() {
auto x = 5.5; // x is deduced as double
auto y = 10; // y is deduced as int
std::cout << "x: " << x << ", y: " << y << std::endl;
return 0;
}

3. volatile

volatile tells the compiler that a variable's value may change unexpectedly
(e.g., in multithreading or hardware-interrupt scenarios).

================================================
volatile int flag = 1;
void someFunc() {
while (flag) { /* Keep checking the flag */ }
// the compiler won’t optimize out this loop
}

4. while

while is used for looping until a condition becomes false.

================================================
int main() {
int i = 0;
while (i < 5) {
std::cout << i++ << std::endl;
}
return 0;
}

5. bool

Represents a boolean value: either true or false.

================================================
#include <iostream>
int main() {
bool isActive = true;
if (isActive) std::cout << "Active" << std::endl;
return 0;
}

6. break

break is used to exit a loop or switch statement prematurely.

================================================
#include <iostream>
int main() {
for (int i = 0; i < 10; ++i) {
if (i == 5) break; // Exit when i == 5
std::cout << i << " ";
}
return 0;
}

7. case

Used in switch statements to define cases.

================================================
#include <iostream>
int main() {
int x = 2;
switch (x) {
case 1: std::cout << "One"; break;
case 2: std::cout << "Two"; break;
default: std::cout << "Other"; break;
}
return 0;
}
8. catch

Used with try and throw for exception handling.

================================================
#include <iostream>
int main() {
try {
throw std::runtime_error("An error occurred");
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}

9. char

Represents a character type.

================================================
#include <iostream>
int main() {
char letter = 'A';
std::cout << letter << std::endl;
return 0;
}

10. class

Defines a user-defined data type (class).

================================================
class Person {
public:
std::string name;
int age;
Person(std::string n, int a) : name(n), age(a) {}
void display() { std::cout << name << " is " << age << " years
old." << std::endl; }
};

11. const

Specifies that a variable's value cannot be changed.

================================================
const int PI = 3.14159;
12. const_cast

Used to cast away the constness of variables.

================================================
#include <iostream>
void print(const int* ptr) {
int* modifiable = const_cast<int*>(ptr);
*modifiable = 100; // Modify even though it was const
}
int main() {
int x = 10;
print(&x);
std::cout << x; // Prints 100
return 0;
}

13. continue

Skips the current iteration of the loop.

================================================
#include <iostream>
int main() {
for (int i = 0; i < 5; ++i) {
if (i == 2) continue; // Skip 2
std::cout << i << std::endl;
}
return 0;
}

14. default

The default case in a switch statement.

================================================
#include <iostream>
int main() {
int x = 3;
switch (x) {
case 1: std::cout << "One"; break;
default: std::cout << "Default case"; break;
}
return 0;
}

15. delete

Deallocates memory that was dynamically allocated with new.


================================================
int* ptr = new int(10);
delete ptr;

16. do

do-while loop ensures the code is executed at least once.

================================================
#include <iostream>
int main() {
int i = 0;
do {
std::cout << i << std::endl;
i++;
} while (i < 5);
return 0;
}

17. double

Represents a floating-point number with double precision.

================================================
double pi = 3.14159;

18. dynamic_cast

Used to safely convert pointers/references in an inheritance hierarchy.

================================================
class Base { virtual void func() {} };
class Derived : public Base {};
Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr); // Safe
downcast

19. else

Follows if for alternate execution paths.

================================================
#include <iostream>
int main() {
int x = 10;
if (x < 5) std::cout << "Less than 5";
else std::cout << "5 or more";
return 0;
}

20. enum

Defines a set of named integer constants.

================================================
enum Color { Red, Green, Blue };
Color c = Red;

21. except

An MS-specific keyword used in Structured Exception Handling (SEH).

================================================
__try {
// SEH code block
} __except (EXCEPTION_EXECUTE_HANDLER) {
// Handle exception
}

22. explicit

Prevents implicit conversions.

================================================
class A {
public:
explicit A(int x) {}
};
A obj1 = 10; // Error: no implicit conversion
A obj2(10); // OK

23. extern

Declares a variable that is defined in another translation unit.

================================================
extern int x;

24. false

Represents a boolean false value.

================================================
bool flag = false;

25. finally

Used in exception handling (not standard in C++ but in C++/CLI or similar


variants).

================================================
try {
// Code
} finally {
// Cleanup code
}

26. float

Represents a floating-point number with single precision.

================================================
float temperature = 36.6f;

27. for

A loop construct that allows initialization, condition, and iteration expression.

================================================
for (int i = 0; i < 10; ++i) {
std::cout << i << std::endl;
}

28. friend

Grants another class or function access to private/protected members.

================================================
class B;
class A {
int secret;
friend class B;
};
class B {
A a;
void reveal() { std::cout << a.secret; }
};

29. goto
Jumps to a labeled statement within the same function.

================================================
int main() {
int x = 0;
start:
std::cout << x << std::endl;
x++;
if (x < 5) goto start; // Jump to label
return 0;
}

30. if

Conditional statement that executes a block of code if a condition is true.

================================================
#include <iostream>
int main() {
int a = 10;
if (a > 5) {
std::cout << "a is greater than 5" << std::endl;
}
return 0;
}

31. inline

Suggests that the function be inlined, reducing function call overhead (though
the compiler ultimately decides).

================================================
inline int square(int x) {
return x * x;
}
int main() {
std::cout << square(5) << std::endl;
return 0;
}

32. int

Defines an integer variable.

================================================
int age = 25;

33. long
Defines an extended-size integer.

================================================
long population = 7800000000;

34. mutable

Allows a class member to be modified even if the containing object is const.

================================================
class Test {
public:
mutable int x;
void modify() const { x = 100; } // Allowed since x is mutable
};

35. namespace

Used to define a scope for identifiers, helping to avoid name conflicts.

================================================
namespace First {
void func() { std::cout << "In First Namespace" << std::endl; }
}
namespace Second {
void func() { std::cout << "In Second Namespace" << std::endl; }
}
int main() {
First::func();
Second::func();
return 0;
}

36. new

Allocates memory dynamically from the heap.

================================================
int* ptr = new int(100);
delete ptr;

37. operator

Defines or overloads an operator.

================================================
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
Complex operator+(const Complex& rhs) {
return Complex(real + rhs.real, imag + rhs.imag);
}
};

38. private

Specifies that members are accessible only within the class itself.

================================================
class A {
private:
int secret;
};

39. protected

Specifies that members are accessible within the class and derived classes.

================================================
class A {
protected:
int secret;
};
class B : public A {
void reveal() { std::cout << secret; }
};

40. public

Specifies that members are accessible outside the class.

================================================
class A {
public:
int number;
};

41. register

Requests that the variable be stored in a CPU register (this is largely ignored by
modern compilers).
================================================
register int counter = 0;

42. reinterpret_cast

Used for dangerous or low-level pointer type conversions.

================================================
int x = 42;
void* ptr = &x;
int* intPtr = reinterpret_cast<int*>(ptr);

43. return

Used to return a value from a function.

================================================
int square(int x) {
return x * x;
}

44. short

Defines a short integer.

================================================
short height = 150;

45. signed

Specifies that a variable can hold both positive and negative values (explicitly
marked).

================================================
signed int balance = -100;

46. sizeof

Returns the size, in bytes, of a type or object.

================================================
std::cout << "Size of int: " << sizeof(int) << std::endl;

47. static
Defines a variable that retains its value across function calls or a member that is
shared among all objects of the class.

================================================
class A {
public:
static int count;
A() { ++count; }
};
int A::count = 0;

48. static_cast

Safely converts between related types.

================================================
float f = 3.14;
int i = static_cast<int>(f); // Convert float to int

49. struct

Defines a structure, which is similar to a class but with public members by


default.

================================================
struct Point {
int x, y;
};

50. switch

Controls flow based on the value of an expression.

================================================
int main() {
int day = 2;
switch (day) {
case 1: std::cout << "Monday"; break;
case 2: std::cout << "Tuesday"; break;
default: std::cout << "Other";
}
return 0;
}

51. template

Used for generic programming.


================================================
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
std::cout << add(5, 10) << std::endl; // Works for integers
std::cout << add(5.5, 3.1) << std::endl; // Works for floats
return 0;
}

52. this

Refers to the current object instance within a class.

================================================
class A {
int x;
public:
A(int x) : x(x) {}
void show() { std::cout << "x = " << this->x << std::endl; }
};

53. throw

Throws an exception.

================================================
#include <iostream>
void test(int value) {
if (value == 0) throw std::invalid_argument("Value cannot be
zero");
}
int main() {
try {
test(0);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}

54. true

Represents the boolean true value.

================================================
bool success = true;
55. try

Defines a block of code where exceptions are handled.

================================================
try {
// Code that may throw
} catch (std::exception& e) {
std::cout << e.what();
}

56. type_info

Used for runtime type information (RTTI) in C++.

================================================
#include <iostream>
#include <typeinfo>
int main() {
int x = 10;
std::cout << typeid(x).name() << std::endl; // Outputs type
information of x
return 0;
}

57. typedef

Defines a new name (alias) for a type.

================================================
typedef unsigned long ulong;
ulong population = 7800000000;

58. typeid

Returns the type information at runtime.

================================================
#include <iostream>
#include <typeinfo>
int main() {
double d = 5.0;
std::cout << typeid(d).name() << std::endl; // Outputs "double"
return 0;
}

59. typename
Used in template definitions to specify a placeholder for a type.

================================================
template <typename T>
class MyClass {
T data;
public:
MyClass(T d) : data(d) {}
};

60. union

Defines a data structure where all members share the same memory location.

================================================
union Data {
int i;
float f;
};
Data data;
data.i = 10;
std::cout << data.i;

61. unsigned

Specifies that a variable can only hold non-negative values.

================================================
unsigned int score = 100;

62. using

Creates an alias for a type or brings a namespace into scope.

================================================
using namespace std; // Bring everything from std into scope
using myInt = int; // Alias for int

63. virtual

Allows derived classes to override a method.


================================================
class Base {
public:
virtual void show() { std::cout << "Base class" << std::endl; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived class" <<
std::endl; }
};

64. void

Specifies that a function does not return a value.

================================================
void printMessage() {
std::cout << "Hello World" << std::endl;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy