Keywords in C
In C Programming language, there are many rules so to avoid different types of errors. One of such rule is not able to declare variable names with auto, long, etc. This is all because these are keywords. Let us check all keywords in C language.
What are Keywords?
Keywords are predefined or reserved words that have special meanings to the compiler. These are part of the syntax and cannot be used as identifiers in the program. A list of keywords in C or reserved words in the C programming language are mentioned below:
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
auto keyword is used to specify the automatic storage class which is the default storage class variable that is declared inside a function or a block. Automatic variables are also called local variables as they are local to a function.
Example:
#include <stdio.h>
int main() {
Update 'ones' with bits that have
// Auto variable
auto int a = 10;
printf("%d", a);
return 0;
}
Output
10
break and continue
The break keyword is used to terminate the innermost loop. It generally terminates a loop or a break statement. The continue keyword skips to the next iteration of the loop.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 2) {
// Using continue
continue;
}
if (i == 6) {
// Using break
break;
}
printf("%d ", i);
}
return 0;
}
Output
1 3 4 5
switch, case, and default
The switch keyword in C is used as an alternate to the if-else ladder statement. The case keyword allows us to execute different code for different possible values of a single variable. The default keyword is used to specify case that is executed when no case value is matched.
Example:
#include <stdio.h>
int main() {
int i = 4;
// Using swith statement
switch (i) {
case 1:
printf("Case 1");
break;
case 2:
printf("Case 2");
break;
case 3:
printf("Case 3");
break;
case 4:
printf("Case 4");
break;
default:
printf("Default");
break;
}
}
Output
Case 4
char
char keyword in C is used to declare a character variable in the C programming language.
Example:
#include <stdio.h>
int main() {
// Creating a character variable
char c = 'a';
printf("%c", c);
return 0;
}
Output
a
const
The const keyword defines a variable whose value cannot be changed.
Example:
#include <stdio.h>
int main() {
// Creting constant keyword
const int a = 11;
// Trying to modify
a = a + 2;
printf("%d", a);
return 0;
}
Output:
error: assignment of read-only variable 'a'
a = a + 2;
do
The do keyword is used to declare a do-while loop. A do-while loop is a loop that executes once and then checks its condition to see if it should continue through the loop.
Example:
#include <stdio.h>
int main() {
int i = 1;
// Do while loop
do {
printf("%d ", i);
i++;
} while(i <= 5);
return 0;
}
Output
1 2 3 4 5
double and float
The double and float keywords specify the datatypes used to declare decimal type variables. They are similar, but double have 15 decimal digits, and float only have 7.
Example:
#include <stdio.h>
int main() {
// Creating float and double variables
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 and else keywords are used in if-else statement is used to make decisions, where if a condition is true, then it will execute a block of code; if it isn’t true (else), then it will execute a different block of code.
Example:
#include <stdio.h>
int main() {
int a = 10;
// If condition and block
if(a < 11) {
printf("A is less than 11");
}
// Else block
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 enum. An enum is a user-defined datatype, which holds a list of user-defined integer constants.
Example:
#include<stdio.h>
// enum declaration:
enum week {Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main() {
// object of the enum (week), called day
enum week day;
day = Wed;
printf("%d", day);
return 0;
}
Output
2
extern
The extern keyword is used to declare a variable or a function that has an external linkage outside of the file declaration.
Example:
#include <stdio.h>
int main() {
// Extern variable
extern int a;
printf("%d", a);
return 0;
}
for
The for keyword is used to declare a for-loop. A for-loop is a loop that is specified to run a certain number of times.
Example:
#include <stdio.h>
int main() {
// For loop
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
return 0;
}
Output
0 1 2 3 4
goto
The goto keyword is used to transfer the control of the program to the given label. It is used to jump from anywhere to anywhere within a function.
Example:
#include <stdio.h>
int main(){
int n = 1;
label:
printf("%d ", n);
n++;
// Use goto keyword to move
// execution to label
if (n <= 10) goto label;
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
int
int keyword is used in a type of declaration to give a variable an integer type. In C, the integer variable must have a range of at least -32768 to +32767.
Example:
#include <stdio.h>
int main() {
// Integer type variable
// declare
int a = 10, b = 20;
int sum;
sum = a + b;
printf("%d", sum);
return 0;
}
Output
30
short, long, signed, and unsigned
short, long, signed, and unsigned are modifiers used to define the range and size of types in C.
- short and long keywords control the size of the integer (e.g., short uses less memory, long uses more).
- signed keyword means the integer can hold both positive and negative values, while unsigned keyword means the integer can only hold positive values.
Example:
#include <stdio.h>
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("%hd", a);
printf("\n%d", b);
printf("\n%u", c);
printf("\n%ld", d);
return 0;
}
Output
12345 -34 12 99998
return
The return keyword returns a value to where the function was called.
Example:
#include <stdio.h>
int sum(int x, int y) {
int sum;
sum = x + y;
// Function return sum using
// return keyword
return sum;
}
// Driver code
int main() {
int num1 = 10;
int num2 = 20;
printf("Sum: %d",
sum(num1, num2));
return 0;
}
Output
Sum: 30
sizeof
sizeof is a keyword that gets the size of an expression, (variables, arrays, pointers, etc.) in bytes.
Example:
#include <stdio.h>
int main() {
int x = 10;
// Use size of operator to
// know size of integer x
printf("%d", sizeof(x));
return 0;
}
Output
4
register
Register keywords tell the compiler to store variables in the CPU register instead of memory. Frequently used variables are kept in the CPU registers for faster access.
Example:
register char c = 's';
static
The static keyword is used to create static variables. A static variable is not limited by a scope and can be used throughout the program. Its value is preserved even after its scope.
Example:
static int num;
struct
The struct keyword in C programming language is used to declare a structure. A structure is a list of variables, (they can be of different data types), which are grouped together under one data type.
Example:
#include <stdio.h>
#include <string.h>
// Create a strucuture
struct Books {
char title[50];
char author[50];
};
int main( ) {
// Declare Book1 of type Book
struct Books book1;
strcpy(book1.title, "C++ Programming");
strcpy(book1.author, "Bjarne Stroustrup");
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 in C programming language is used to define a data type with a new name in the program. typedef keyword is used to make our code more readable.
Example:
typedef long num;
union
The union keyword is used as user-defined data type. All data members which are declared under the union keyword share the same memory location.
Example:
#include <stdio.h>
// Create an union
union student {
int age;
char marks;
} s;
int main() {
s.age = 15;
s.marks = 56;
printf("age = %d", s.age);
printf("\nmarks = %d", s.marks);
}
Output
age = 56 marks = 56
void
The void keyword means nothing i.e, NULL value. When the function return type is used as the void, the keyword void specifies that it has no return value.
Example:
void fun() {
// Function body
}
volatile
The volatile keyword is used to create volatile objects. Objects which are declared volatile are omitted from optimization as their values can be changed by code outside the scope of the current code at any point in time.
Example:
const volatile marks = 98;
while
The while keyword is used to declare a while loop that runs till the given condition is true.
Example:
#include <stdio.h>
int main() {
int i = 0;
// While loop that execute
// till i is less than 3
while (i < 3) {
printf("Hi\n");
i++;
}
return 0;
}
Output
Hi Hi Hi
Keywords vs Identifiers
The below table lists the primary differences between the keywords and identifiers in C:
Keywords | Identifiers |
---|---|
Keywords are the words, which have predefined specific meaning. | Identifiers are the place holder of the data types. |
Keyword contains only alphabetic characters. | Identifiers are mix of characters, digits and underscore. |
Most keywords are in lower case. | The are following naming convention. |
Example: int, float, print, ... etc. | Example: val, name, arr, ... etc. |