Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

C Keywords

The document provides an overview of the 32 keywords in the C programming language, detailing their specific functions and usage. Each keyword is accompanied by an explanation and example code demonstrating its application. Keywords include 'auto', 'char', 'const', 'float', 'double', 'int', 'long', 'struct', and others, highlighting their roles in variable declaration, control flow, and data structure definitions.

Uploaded by

K S Rajasekhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Keywords

The document provides an overview of the 32 keywords in the C programming language, detailing their specific functions and usage. Each keyword is accompanied by an explanation and example code demonstrating its application. Keywords include 'auto', 'char', 'const', 'float', 'double', 'int', 'long', 'struct', and others, highlighting their roles in variable declaration, control flow, and data structure definitions.

Uploaded by

K S Rajasekhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Keywords in C Language: C programming language has 32 keywords, each with a specific

function.

auto char const float

double int long struct

switch case break default

continue short signed register

if else do while

for goto void enum

extern sizeof typedef union

unsigned volatile return static

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.

Example: int main()


{
auto int x = 10; // 'auto' is optional, same as 'int x = 10;'
printf("%d", x);
return 0;
}
2. char:

 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.

Example: int main()


{
char letter = 'A';
printf("%c", letter);
return 0;
}
// Output: A
3. const:

 Explanation: The const keyword is used to declare variables whose value cannot be
changed after initialization. It can be applied to any data type.

Example: int main()


{
const int x = 5;
x = 10; // Error: cannot modify a constant variable
printf("%d", x);
return 0;
}
// Output: 5

4. float

 Explanation: The float keyword is used to declare variables of type float, a single-
precision floating-point data type (typically 32 bits).

Example: int main()


{
float pi = 3.14f;
printf("%f", pi);
return 0;
}
// Output: 3.140000

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).

Example: int main()


{
double pi = 3.14159;
printf("%lf", pi);
return 0;
}
// Output: 3.141590
6. int

 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.

Example: int main() {


int number = 10;
printf("%d", number);
return 0;
}
// Output: 10

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.

Example: int main()


{
long int num = 123456789L;
printf("%ld", num);
return 0;
}
// Output: 123456789

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.

Example: struct Point // Define a structure named 'Point'


{
int x; // x-coordinate
int y; // y-coordinate
};
int main()
{
struct Point p1; // Declare a variable of type 'struct Point'
// Assign values to the members of the structure
p1.x = 10;
p1.y = 20;
// Print the values of the structure members
printf("x = %d, y = %d", p1.x, p1.y);
return 0;
}
// Output: x = 10, y = 20

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.

Example: int main()


{
int num = 2;
switch (num) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Other");
}
return 0;
}
// Output: Two

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.

Example: int main() {


int num = 2;
switch (num) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Other");
}
return 0;
}
// Output: Two

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.

Example: int main()


{
for (int i = 0; i < 5; i++) {
if (i == 3) break;
printf("%d ", i);
}
return 0;
}
// Output: 0 1 2

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.

Example: int main()


{
int num = 3;
switch (num) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Other");
}
return 0;
}
// Output: Other

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.

Example: int main()


{
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
printf("%d ", i);
}
return 0;
}
// Output: 0 1 3 4

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.

Example: int main()


{
short int num = 32767;
printf("%d", num);
return 0;
}
// Output: 32767

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.

Example: int main()


{
signed int num = -10;
printf("%d", num);
return 0;
}
// Output: -10

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.

Example: int main()


{
register int x = 5;
printf("%d", x);
return 0;
}
// Output: 5

17. if

 Explanation: The if keyword is used to execute a block of code if a specified condition


is true. It is the primary decision-making structure in C.

Example: int main()


{
int x = 5;
if (x > 0)
{
printf("Positive");
}
return 0;
}
// Output: Positive

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:

 Explanation: The do keyword is used in conjunction with while to create a do-while


loop, which is similar to a while loop, except that the block of code is executed at least
once before the condition is tested.

Example: int main()


{
int i = 0;
do
{
printf("%d ", i);
i++;
}
while (i < 3);
return 0;
}
// Output: 0 1 2

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.

Example: int main()


{
int i = 0;
while (i < 3) {
printf("%d ", i);
i++;
}
return 0;
}
// Output: 0 1 2

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.

Example: int main() {


for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
return 0;
}
// Output: 0 1 2 3 4

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.

Example: int main()


{
int x = 0;
if (x == 0) goto label;
printf("This won't print.");
label:
printf("Goto label");
return 0;
}
// Output: Goto label

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.

Example: void printHello()


{
printf("Hello");
}
int main() {
printHello();
return 0;
}
// Output: Hello

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

 Explanation: The extern keyword is used to declare a global variable or function in


another file or later in the same file. It is used to extend the visibility of
variables/functions across multiple files.

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>

// Declare the external variable


extern int value;

// Declare the external function


extern void displayvalue();

int main() {
// Use the external function
displayvalue();

// Access and print the external variable


printf("Value from file2.c: %d\n", value);

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.

Example: int main()


{
int x;
printf("Size of int: %lu bytes", sizeof(x));
return 0;
}
// Output: Size of int: 4 bytes

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.

Example: typedef unsigned long int ULI;


int main() {
ULI num = 1234567890;
printf("%lu", num);
return 0;
}
// Output: 1234567890

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.

Example: union Data


{
int i;
float f;
char str[20];
};
int main()
{
union Data data;
data.i = 10;
printf("data.i = %d\n", data.i);
data.f = 220.5;
printf("data.f = %.1f\n", data.f);
strcpy(data.str, "C Programming");
printf("data.str = %s\n", data.str);
return 0;
}
// Output:

// 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.

Example: int main()


{
unsigned int num = 10;
printf("%u", num);
return 0;
}
// Output: 10

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.

Example: int main()


{
volatile int x = 10;
// The compiler will not optimize the following line:
x = 20;
printf("%d", x);
return 0;
}
// Output: 20

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.

Example: int add(int a, int b)


{
return a + b;
}
int main()
{
int sum = add(3, 4);
printf("%d", sum);
return 0;
}
// Output: 7

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.

Example: void func()


{
static int count = 0;
count++;
printf("%d ", count);
}
int main()
{
func();
func();
func();
return 0;
}
// Output: 1 2 3

You might also like