13
DecKeywords in C: List of Keywords
Keywords in C: An Overview
Are you looking to develop your knowledge in coding? Working with strings, integers, arrays, and more can be a daunting task for anyone, regardless of their experience level. But don't worry! With this C Tutorial on keywords in C programming language, you will learn the basics as well as the advanced features that can help enhance your understanding. In addition to learning about keywords in C programming, you can consider taking a C certification course.
What are Keywords in C?
Keywords are reserved words that convey a special meaning and purpose within the context of the language. They are used in the syntax and cannot be used as a variable name.
Example: int
a
Here int
is a keyword that conveys, a
is a variable that can store the value of integer data type.
Types of Keywords in C
auto | break | case | char | const | continue | default | do |
double | else | enum | extern | float | for | goto | if |
int | long | register | return | short | signed | sizeof | static |
struct | switch | typedef | union | unsigned | void | volatile | while |
auto
This keyword is used for automatic storage class, variables are created and initialized locally within blocks.
Example
auto int num = 5;
In the given example in the C Editor, an integer variable num
is initialized to 5.
Read More - Top 50 C Interview Questions and Answers
break
Used to terminate a loop or switch statement prematurely.
Example
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
} }
In the provided example, the loop will stop when the value of i
becomes 5.
case
Used in the switch statement to define different cases to be matched.
Example
switch (option) {
case 1:
// code for case 1
break;
case 2:
// code for case 2
break;
default:
// code for default case
}
char
Data type for storing a single character.
Example
char letter = 'A';
In the example, the character, “A
” is assigned to the variable letter
.
const
Used to define constants that cannot be modified after initialization.
Example
const int MAX_VALUE = 100;
In the given example, MAX_VALUE is a constant with a value of 100.
continue
Used to skip the rest of the loop body and proceed to the next iteration.
Example
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
// code here will be skipped for i = 2
}
In the example, when i=2, the iteration is skipped and the next iteration with i=3 begins.
default
Used in the switch statement as the default
case when no other cases match.
Example
switch (option) {
case 1:
// code for case 1
break;
default:
// code for default case
}
do
Used to start a do-while loop, which guarantees the loop body executes at least once.
Example
do {
// loop body
} while (condition);
double
Data type for storing double-precision floating-point numbers.
Example
double value = 3.14159;
In the example, the variable value is assigned the value 3.14159.
else
Used with if statements to define an alternative block of code to execute when the condition is false
.
Example
if (x > 0) {
// code when x is positive
} else {
// code when x is non-positive
}
In this example, if the value of x
is positive, a positive message is printed; otherwise, a non-positive message is printed using an if-else
statement.
enum
Used to define an enumerated type consisting of named integer constants.
Example
enum Weekdays {
MON, TUE, WED, THU, FRI, SAT, SUN
};
In the example, a set of weekdays is defined using an enum.
extern
Declares a variable or function as existing externally, often used when variables are defined in one file and used in another.
Example
extern int count;
In this example, an external variable named count
is declared.
float
Data type for storing single-precision floating-point numbers.
Example
float value = 2.71828f;
In the example, the variable value is assigned the value 2.71828f.
for
Used to start a for loop, which repeats a block of code for a specified number of iterations.
Example
for (int i = 0; i < 10; i++) {
// code to repeat
}
In this example, the loop
iterates 10 times, printing the iteration index.
goto
Used to transfer control to a labeled statement in the same function.
Example
goto label;
// ...
label:
// statement to jump to
In this example, depending on whether x is positive or not, the program jumps to corresponding labels and prints messages using the goto statement.
if
Used to conditionally execute a block of code based on a specified condition.
Example
if (x > 0) {
// code to execute when x is positive
}
In this example, it checks if x is positive and prints a message accordingly.
int
Data type for storing integer values.
Example
int num = 42;
In this example, an integer variable num is declared with a value and printed.
long
Data type for storing larger integer values.
Example
long largeNum = 1234567890L;
In this example, a long integer variable largeNum is declared with a value and printed.
register
Suggests to the compiler that a variable should be stored in a CPU register for faster access.
Example
register int counter = 0;
return
Used to exit a function and return a value to the caller.
Example
int add(int a, int b) {
return a + b;
}
In this example, a function add()
is defined that returns the sum of two integers.
short
Data type for storing smaller integer values.
Example
short smallNum = 32767;
In this example, a short integer variable smallNum is declared with a value and printed.
signed
Specifies that a variable can hold both positive and negative values.
Example
signed int temperature = -10;
sizeof
Returns the size, in bytes, of a data type or variable.
Example
int size = sizeof(int);
In this example, it calculates and prints the size of the int data type.
static
Declares a variable with static storage duration, retaining its value between function calls.
Example
static int counter = 0;
struct
Used to define a user-defined data type that groups variables of different types under a single name.
Example
struct Point {
int x;
int y;
};
In this example, a structure type Point is defined, an instance is created, and values are assigned.
switch
Used to evaluate a variable or expression against a set of possible values and execute the corresponding case.
Example
switch (option) {
case 1:
// code for case 1
break;
case 2:
// code for case 2
break;
default:
// code for default case
}
In this example in the C Compiler, the program executes code blocks based on the value of the option variable using a switch statement.
typedef
Used to create an alias for an existing data type, improving code readability.
Example
typedef unsigned long ulong;
In this example, it creates an alias ulong for the unsigned long data type and uses it.
union
Similar to a struct, a union allows storing different data types in the same memory location.
Example:
union Data {
int iValue;
float fValue;
};
unsigned
Specifies that a variable can hold only positive values.
Example
unsigned int count = 10;
In this example, an unsigned integer variable count is declared and its value is printed.
void
Represents the absence of a value or return type in functions.
Example
void printMessage() {
printf("Hello, world!\n");
}
In this example, a function printMessage() is defined that prints a message.
volatile
Indicates that a variable can be changed by external entities, preventing certain compiler optimizations.
Example
volatile int sensorValue = 0;
while
Used to start a while loop, which repeats a block of code as long as a condition is true.
Example
while (x < 100) {
// code to repeat while x < 100
}
In this example, the loop repeats while x is less than 100, printing x in each iteration.
Summary
The C keywords are essential language constructs with established meanings. Understanding these keywords is critical for error-free coding. Further, tokens include different aspects of C++ code like keywords, identifiers, constants, strings, special symbols, & operators, making them the core of C programs. For detailed theoretical and practical understanding, join our C Certification Course.