C Keywords
C Keywords
function.
if else do while
1. auto:
Explanation: The auto keyword is used to declare automatic variables. These are local
variables which are automatically created and destroyed when the program flow enters
and exits the block in which they are defined. In modern C, auto is the default storage
class for local variables, so it is rarely used explicitly.
Explanation: The char keyword is used to declare variables that can hold a single
character. Characters in C are stored as integers, using the ASCII value of the character.
Explanation: The const keyword is used to declare variables whose value cannot be
changed after initialization. It can be applied to any data type.
4. float
Explanation: The float keyword is used to declare variables of type float, a single-
precision floating-point data type (typically 32 bits).
5. double:
Explanation: The double keyword is used to declare variables of type double for a
floating-point data type that offers more precision (typically 64 bits) compared to float
(32 bits).
Explanation: The int keyword is used to declare variables of type int, which represents
integers. The size of an int typically depends on the system but is often 32 bits.
7. long
Explanation: The long keyword is used to declare variables of type long, a data type that
can hold larger integer values compared to int. It can also be used with double for double-
precision floating-point variables.
8. struct
Explanation: The struct keyword is used to define a structure, a user-defined data type
that allows grouping variables of different types under a single name.
9. switch
Explanation: The switch keyword is used to create a switch statement, which tests a
variable against a series of constant values and executes the corresponding code block for
the matching value.
10. case:
Explanation: The case keyword is used within a switch statement to define individual
conditions (cases) that can match the value of the variable being evaluated. Each case is
followed by a constant expression and a colon.
11. break:
Explanation: The break statement is used to exit from the nearest enclosing loop or
switch statement. When break is encountered inside a loop, the loop is immediately
terminated, and control resumes at the next statement following the loop.
12. default
Explanation: The default keyword is used in a switch statement to specify the block of
code that should execute if no case matches the switch expression.
13. continue
Explanation: The continue statement skips the remaining code in the current iteration of
the loop and proceeds to the next iteration. It's often used with if statements to skip
certain conditions.
14. short
Explanation: The short keyword is used to declare variables of type short int, a data type
that typically uses less memory (16 bits) than int. It is useful when memory usage is a
concern.
15. signed
Explanation: The signed keyword is used to declare variables that can store both
positive and negative numbers. It is the default for int data types.
16. register
Explanation: The register keyword suggests that the compiler store the variable in a
CPU register instead of RAM for faster access. However, it's merely a suggestion, and the
compiler may ignore it.
17. if
18. else:
Explanation: The else keyword is used in conjunction with if to execute a block of code
if the if condition is false. It is used to handle alternative conditions.
Example: int main()
{
int x = 5;
if (x > 0) {
printf("Positive");
}
else
{
printf("Negative");
}
return 0;
}
// Output: Positive
19. do:
20. while
Explanation: The while keyword is used to create a loop that executes as long as a
specified condition is true. It is a pre-tested loop, meaning the condition is checked before
executing the loop's code.
21. for
Explanation: The for keyword is used to create a loop that repeats a block of code a
specified number of times. It consists of an initialization, a condition, and an
increment/decrement expression.
22. goto
Explanation: The goto keyword transfers control to a labeled statement within the same
function. It is generally discouraged because it can make the code difficult to follow and
debug.
23. void
Explanation: The void keyword is used to indicate that a function does not return a
value. It is also used to declare pointers to an unknown data type.
24. enum:
Explanation: The enum keyword is used to define an enumerated type, which is a set of
named integer constants. Each name in the enum corresponds to an integer value, starting
at 0 by default and incrementing by 1 for each subsequent name.
Example: enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
int main()
{
enum week today = Wed;
printf("%d", today); // Outputs 3 (index of Wed)
return 0;
}
// Output: 3
25. extern
Example: File1.c
#include <stdio.h>
int value = 10; // Define the variable
void displayvalue() // Function to display the variable
{
printf("Value from file1.c: %d\n", value);
}
File2.c
#include <stdio.h>
int main() {
// Use the external function
displayvalue();
return 0;
}
26. sizeof
Explanation: The sizeof keyword is used to determine the size (in bytes) of a data type
or a variable. It is often used for memory allocation.
27. typedef
Explanation: The typedef keyword is used to create a new name (alias) for an existing
data type. It is commonly used for simplifying complex type declarations.
28. union
Explanation: The union keyword is used to define a union, a user-defined data type that
allows storing different data types in the same memory location. Only one member can
hold a value at a time.
// data.i = 10
// data.f = 220.5
// data.str = C Programming
29. unsigned
Explanation: The unsigned keyword is used to declare variables that can only store non-
negative numbers. It increases the maximum range of the variable but removes the ability
to store negative values.
30. volatile
Explanation: The volatile keyword is used to indicate that a variable's value may change
unexpectedly, such as from hardware or another thread. It prevents the compiler from
optimizing code that reads or writes the variable.
31. return
Explanation: The return keyword terminates the execution of a function and returns
control to the calling function. Optionally, it can return a value to the calling function.
32. static
Explanation: The static keyword has several uses. When applied to a variable within a
function, it retains its value between function calls. When used with global variables, it
restricts their scope to the file in which they are declared. In a function, it limits the
visibility to the current file.