Keywords In C

Keywords In C

Keywords in C programming are like building blocks that give the language its structure and meaning. Whether you’re new to programming or have experience, understanding these words is crucial for using C effectively. In this discussion, we’ll dive into what C keywords are, why they matter, and the different types you’ll encounter in the language.

What are Keywords?

Keywords in programming are like special words that have specific meanings for the computer. These words are already reserved for specific tasks, and you can’t use them as names for things in your program. They are crucial for the program’s structure and operation.

Here’s a list of keywords or reserved words in the C programming language:

  1. auto
  2. break
  3. case
  4. char
  5. const
  6. continue
  7. default
  8. do
  9. double
  10. else
  11. enum
  12. extern
  13. float
  14. for
  15. goto
  16. if
  17. int
  18. long
  19. register
  20. return
  21. short
  22. signed
  23. sizeof
  24. static
  25. struct
  26. switch
  27. typedef
  28. union
  29. unsigned
  30. void
  31. volatile
  32. while

These keywords have specific meanings in C and are used for various programming tasks and control flow operations. It’s important not to use them as variable or function names in your C programs, as they are reserved for the language’s syntax.

auto

“Auto” is a special word in C that you can use when you declare a variable inside a function or a specific part of your code. These “auto” variables are like temporary placeholders and can only be used in that specific part of your code. By default, they have random values, so it’s essential to give them a value before using them. Think of them as local, short-lived storage spots that only make sense within a particular section of your program.

auto int num;

In this C program, we have a variable named “num” with the “auto” storage class specifier, and its type is “int.” The “auto” keyword is used to specify that “num” is an automatic variable. Here’s an example of how this might look in a program:

// C program to demonstrate
// auto keyword
#include <stdio.h>
 
int printvalue()
{
  auto int a = 20;
  printf("%d", a);
}
 
// Driver code
int main()
{
  printvalue();
  return 0;
}

Output

20

break and continue

In this C program, we have a variable named “num” with the “auto” storage class specifier, and its type is “int.” The “auto” keyword is used to specify that “num” is an automatic variable. Here’s an example of how this might look in a program

// C program to show use
// of break and continue
#include <stdio.h>
 
// Driver code
int main()
{
  for (int i = 1; i <= 10; i++)
  {
    if (i == 2)
    {
      continue;
    }
    if (i == 6)
    {
      break;
    }
    printf("%d ", i);
  }
  return 0;
}

Output

1 3 4 5

Switch, case, and default

The switch statement in C is a useful alternative to the if-else ladder statement. It allows us to perform different operations based on the possible values of a single variable, known as the switch variable. Instead of writing a series of if-else conditions, you can use the switch statement to make the code more organized and easier to read when dealing with multiple cases based on the value of a single variable.

switch(Expression)
{
case ‘1’: // operation 1
break;
case:’2′: // operation 2
break;
default: // default statement to be executed
}

Below is the C program to demonstrate the switch case statement:

// C program to demonstrate
// switch case statement
#include <stdio.h>
 
// Driver code
int main() {
  int i = 4;
  switch (i) {
    case 1:
      printf("Case 1\n");break;
    case 2:
      printf("Case 2\n");break;
    case 3:
      printf("Case 3\n");break;
    case 4:
      printf("Case 4\n");break;
    default:
      printf("Default\n");break;
  }
}

Output

Case 4

Output

Case 4

Default

char

In C programming, the “char” keyword is used to declare a character variable. For example:

Below is the C program to demonstrate the char keyword:

// C program to demonstrate

// char keyword

#include <stdio.h>

// Driver code

intmain() {

  charc = 'a';

  printf("%c", c);

  return0;

}

Output

a

const

In C, the “const” keyword is used to define a variable whose value cannot be changed once it’s been assigned. It creates a constant variable

const int num = 10;

Below is the C program to demonstrate the const keyword:

// C program to demonstrate

// const keyword

#include <stdio.h>

// Driver code

intmain() {

  constinta = 11;

  a = a + 2;

  printf("%d", a);

  return0;

}

Output

error: assignment of read-only variable ‘a’
a = a + 2;

Do

The do-while loop is used to create a loop that executes its code block at least once before checking the condition to determine if it should continue looping. After the initial execution, it continues to repeat the code block as long as the specified condition remains true.

// C program to demonstrate
// do-while keyword
#include <stdio.h>
 
// Driver code
int main()
{
  int i = 1;
  do {
    printf("%d ", i);
    i++;
  } while(i <= 5);
   
  return 0;
}

output

12345

double and float

In C, both doubles and floats are used to declare variables that can store decimal numbers (floating-point numbers). However, they differ in terms of precision:

  1. Floats: Floats have 7 decimal digits of precision. They are represented using 32 bits in memory.
  2. Doubles: Doubles, on the other hand, have 15 decimal digits of precision. They are represented using 64 bits in memory.

Doubles provide greater precision compared to floats, but they also consume more memory. The choice between using float or double depends on the specific requirements of a program. Use floats when memory is a concern, and you don’t need high precision, and use doubles when precision is essential, even if it means using more memory.

 C program to demonstrate double float keyword is provided below :

// C program to demonstrate
// double float keyword
#include <stdio.h>
 
// Driver code
int main() {
  float f = 0.3;
  double d = 10.67;
  printf("Float value: %f\n", f);
  printf("Double value: %f\n", d);
  return 0;
}

Output

Float value: 0.300000 Double value: 10.670000

if-else

The if-else statement is used to make decisions in a program. It checks a specified condition, and based on whether that condition is true or false, it executes different blocks of code.

if(marks == 97) {
// if marks are 97 then will execute this block of code
}
else {
// else it will execute this block of code
}

 C program to demonstrate an if-else statement

// C program to demonstrate
// if-else keyword
#include <stdio.h>
 
// Driver code
int main()
{
  int a = 10;
  if(a < 11)
  {
    printf("A is less than 11");
  }
  else
  {
    printf("A is equal to or "
           "greater than 11");
  } 
  return 0;
}

Output

A is less than 11

enum

The enum keyword is used to declare an enumeration, which is a user-defined data type in C. An enumeration holds a list of user-defined integer constants, and by default, the value of each constant starts at zero and increments by one for each subsequent constant. However, you can change the initial values if needed.

/ An example program to
// demonstrate working of
// enum in C
#include<stdio.h>
 
// enum declaration:
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
 
// Driver code
int main()
{
//object of the enum (week), called day
  enum week day;
  day = Wed;
  printf("%d", day);
  return 0;
}

Output

2

extern

Your description of the extern keyword in C is accurate.

The extern keyword is used to declare a variable or a function that has external linkage, meaning it’s defined in another source file or somewhere else in the program. When you declare a variable or function as extern, you are telling the compiler that the actual definition of that variable or function is located elsewhere, and it should be linked during the linking phase of compilation.

#include <stdio.h>
 
extern int a;
 
int main(){
   
    printf("%d", a);
 
      return 0;
}

for

The “for” keyword is used to declare a for-loop, which is a type of loop that is designed to run a specified number of times. It’s a fundamental control structure in C and many other programming languages.

// C program to demonstrate
// for keyword
#include <stdio.h>
 
// Driver code
int main()
{
  for (int i = 0; i < 5; i++)
  {
    printf("%d ", i);
  }
  return 0;
}

Output

01234

goto

The goto statement is used to transfer control of the program to a specified label within the same function or block of code. It allows you to perform an unconditional jump from one part of your code to another. While goto can be a powerful tool, it’s often discouraged in modern programming because it can make code less structured and harder to understand.

Example

goto label;

// code

label:

// C program demonstrate
// goto keyword
#include <stdio.h>
 
// Function to print numbers
// from 1 to 10
void printNumbers() {
    int n = 1;
 
label:
    printf("%d ", n);
    n++;
    if (n <= 10) goto label;
}
 
// Driver code
int main(){
    printNumbers();
    return 0;
}

Output

12345678910

int

Your description of the int keyword in C is mostly accurate.

The int keyword is indeed used in a type declaration to specify that a variable is of integer type. However, the specific range of an int variable is not fixed and can vary depending on the platform and compiler. In standard C, an int must have a range of at least -32,767 to +32,767, but it can be larger on many modern systems. It’s important to be aware of platform-specific variations when dealing with integer ranges in C programming.

Example

int x = 10;

// C program to demonstrate
// int keyword
#include <stdio.h>
 
void sum() {
    int a = 10, b = 20;
    int sum;
    sum = a + b;
    printf("%d", sum);
}
 
// Driver code
int main() {
    sum();
    return 0;
}

Output

30

Short, long, signed, and unsigned

Data types in C can have different ranges, and these ranges can vary from one compiler and system to another. It’s important to be aware of these ranges when working with different data types.

Data TypeMemory (bytes)RangeFormat Specifier
short int 2-32,768 to 32,767 %hd 
unsigned short int 2  0 to 65,535%hu
unsigned int 0 to 4,294,967,295 %u 
long int -2,147,483,648 to 2,147,483,647%ld 
unsigned long int0 to 4,294,967,295 %lu
long long int  8-(2^63) to (2^63)-1 %lld 
unsigned long long int80 to 18,446,744,073,709,551,615%llu
signed char1 -128 to 127 %c
unsigned char 

10 to 255 

%c
long double

16

3.4E-4932 to 1.1E+4932

%Lf

C program to demonstrate the short, long, signed, and unsigned keywords is provided below:

// C program to demonstrate
// short, long, signed,
// and unsigned keyword
#include <stdio.h>
 
// Driver code
int main() {
  // short integer
  short int a = 12345;
   
  // signed integer
  signed int b = -34;
   
  // unsigned integer
  unsigned int c = 12;
   
  // L or l is used for
  // long int in C.
  long int d = 99998L;
   
  printf("Integer value with a short int data: %hd", a);
  printf("\nInteger value with a signed int data: %d", b);
  printf("\nInteger value with an unsigned int data: %u", c);
  printf("\nInteger value with a long int data: %ld", d);
  return 0;
}

Output

Integer value with a short int data: 12345
Integer value with a signed int data: -34
Integer value with an unsigned int data: 12
Integer value with a long int data: 99998

return

The return statement in C exits a function and sends a value back to where the function was called. It’s a way for functions to provide results to the calling code.

Below is the C program to demonstrate the return keyword:

// C program to demonstrate
// return keyword
#include <stdio.h>
int sum(int x, int y) {
  int sum;
  sum = x + y;
  return sum;
}
 
// Driver code
int main() {
  int num1 = 10;
  int num2 = 20;
  printf("Sum: %d",
          sum(num1, num2));
  return 0;
}

Output

Sum: 30

sizeof 

The sizeof keyword is used to determine the size of an expression in bytes. It can be applied to various entities in C, including variables, arrays, pointers, and data types. It’s a useful tool for calculating memory requirements and is often used in memory allocation and manipulation tasks.

Example

sizeof(char);
sizeof(int);
sizeof(float); in bytes.

C program to demonstrate sizeof keyword is given below:

// C program to demonsstrate
// sizeof keyword
#include <stdio.h>
 
// Driver code
int main() {
  int x = 10;
  printf("%d", sizeof(x));
  return 0;
}

Output

4

register

In C, you can declare variables as “register” variables, which suggest to the compiler that these variables should be stored in CPU registers rather than in memory. This is done to optimize the access time for frequently used variables, as accessing data from registers is significantly faster than accessing data from memory. However, it’s important to note that the use of the register keyword is a hint to the compiler, and modern compilers are often good at optimizing variable storage on their own, so explicit use of register is less common in modern C programming.

Example:

register char c = 's'; 

static 

The static keyword is used to create static variables, which are not limited by a particular scope and can retain their values across function calls. They are preserved throughout the program’s execution and retain their values even after their scope has ended. Static variables are typically initialized only once, and their values persist between function calls, making them useful for various purposes such as maintaining state across function calls or sharing data among different functions within the same file.

For Example:

static int num;

struct 

The struct keyword is used to declare a structure, which is a user-defined composite data type in C. A structure allows you to group together variables of different data types under one custom data type. This enables you to create complex data structures that can represent real-world entities with multiple attributes. Structures are widely used for organizing and managing data in C programs, and they provide a way to encapsulate related data members into a single unit, making the code more organized and readable.

For Example:

struct Geek {
    char name[50];
    int num;
    double var;
};

C program for the struct keyword is given below :

// C program to demonstrate
// struct keyword
#include <stdio.h>
#include <string.h>
 
struct Books {
  char  title[50];
  char  author[50];
};
 
// Driver code
int main( ) {
  // Declare Book1 of type Book
  struct Books book1;       
    
 // book 1 specification
 strcpy(book1.title, "C++ Programming");
 strcpy(book1.author, "Bjarne Stroustrup");
  
 // Print book details
 printf("Book 1 title : %s\n", book1.title);
 printf("Book 1 author : %s\n", book1.author);
 return 0;
}

Output

Book 1 title : C++ Programming
Book 1 author : Bjarne Stroustrup

typedef 

The typedef keyword is used to create new, user-defined data types with more descriptive and meaningful names. It allows you to define a data type using an alias or a new name, which can make your code more readable and maintainable. This is particularly useful when you want to create shorter or more descriptive names for complex data types, making your code easier to understand. For example, you can use typedef to create custom names for structures, pointers, or other data types to enhance code clarity and maintainability.

For Example

typedef long num

we have changed the datatype name of “long” to “num” in the above exmaple

union 

A union is a user-defined data type in C where all its data members, often referred to as “fields” or “members,” share the same memory location. Unlike structures, where each data member has its own memory location, in a union, the memory allocated for one data member can be used to store another data member. This means that the size of a union is determined by the size of its largest data member.

Unions are typically used when you want to represent a single value that can be of different data types at different times during program execution. The ability to share memory among its members makes unions memory-efficient but requires careful handling to ensure that the correct data type is accessed at the appropriate time

Example :

union GeekforGeeks {
    int x;
    char s;
} obj;

C program for the union keyword is given below:


#include <stdio.h>
union student { 
  int age; 
  char marks;  
} s; 
 
// Driver code
int main() { 
  s.age = 15; 
  s.marks = 56;
  printf("age = %d", s.age); 
  printf("\nmarks = %d", s.marks); 
}

Output

age = 56
marks = 56

void 

In C, the void keyword is used to represent nothing or a null value. When used as a function’s return type, void indicates that the function does not return any value. This is commonly used for functions that perform actions or operations but do not produce a result that needs to be returned to the caller.

Example:

void fun() {
    // program
}

volatile

In C, the volatile keyword is used to declare objects as volatile. A volatile object is one whose value can be changed at any time, even by code outside the current code’s scope. When a variable is declared as volatile, the compiler will not optimize accesses to that variable, ensuring that reads and writes to it are performed as explicitly specified in the code.

Example

const volatile marks = 98;

FAQ- Keywords in C

Q1. What is keywords in C?

Ans.Keywords in a programming language, such as C, are predefined or reserved words that serve specific functions and have fixed meanings. They cannot be used as variable names or identifiers within the program. Keywords play a crucial role in defining the syntax and structure of a C program, and they are essential building blocks for writing correct and well-structured code.

Q2. What is loop in C?

Ans. Loops in C are used to execute a block of code repeatedly based on a given condition or a set number of iterations. They allow you to perform tasks multiple times, which can save code and make it more efficient. Loops are particularly useful for tasks such as iterating through elements of an array, processing data, and performing repetitive operations. They provide a powerful mechanism for automating repetitive tasks and controlling the flow of a program.

Q3.What is a variable in C?

Ans. A variable is a named memory location used to store data. It can be changed and reused in a program, identified by symbols.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment