![]() |
C Programming Reference >> C Basic Programming Tutorials >> C Keywords Home >> C Keywords Combined Reviewuisite Knowledege 30/12/06 - Views - Ratings : 0 of 5 / Votes Level - Beginner This Article contains a description of all the standard keywords define in C89 and C99 with appropriate easy to understand source code accompanying it. There are 32 Keywords define in C89 and 5 new in C99. Keywords are reserved words and you cannot use them as identifiers such as variable name. We discuss here only standard keywords provide by all compilers but your compiler must be having many more to exploit the operating enviorment, for them you shoud refer to your compilers manaul. Contents - C89 Keywords - 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 C99 Keywords 33. _Bool 34. _Complex 35. _Imaginary 36. inline 37. restrict 1. auto As you have learned there mainly two variables local or global depending upon on thier scope or lifetime withrespect to function. Local variables are the variables declared within the function. Earlier to mark local variables keyword 'auto' was used. Now all compilers by default assume all variables declare within the function as local unless specified. So this keyword is now obsolete and virtually never used. Since it has been not used for decades its a possibility that modern compilers does not support it. Hence its highly recommended you make use of this keyword so that your code can be compiled by legacy of compilers. Code - void function (void)
{
auto int a = 10;
auto int b;
b = a;
}
2. break This keyword is used to break out of loops. It is also used with switch to terminate cases. Code - void function (void)
{
for (int i = 0; i<100; i++)
{
printf ("\n%d", i);
if (i == 10); \\ This code prints value only upto 10.
break;
}
}
3. case This statement is used only and only with switch and cannt exist outside the switch block. It is used to represent code related to particular input value to the switch loop. If a break statement is not end of the case the execution continue with next case. Code - void function (void)
{
char ch;
printf ("\nYou Entered A");
ch = getchar ();
switch (ch)
{
case 'A' : printf ("You Entered A");
break;
case 'B' : printf ("You Entered B");
break;
case 'C' : printf ("You Entered C");
break;
default : printf ("You Didnot Entered A, B or C");
}
}
4. char This is used to declare a character data type. Character data type is of 2 bytes and is used to store a ASCII character. Code - void function (void)
{
char a = 'A';
char b;
b = getchar ();
printf ("%c %c",a, b);
}
5. const This used to declare a variable constant. This means that the value of variable is unchangable. It can also be used to declare a pointer constant in that case value pointed by that pointer is unchangable. This specially desirable when we want the value passed by refrence to a function remain unchanged under all circumstances. Code - void function (void)
{
const int a = 10;
printf ("%d",a);
\\ a = 20; If you remove the comments symbol this will generate an compile time error.
}
6. continue Continue is reverse of that of break. It forces the loop to execute again jumping the statements below the continue. In case of for the continue increments the value of variable and forces a new cycle of loop. Code - void function (void)
{
for (int i = 0; i<100; i++)
{
if (i == 10); \\ This code prints value only upto 9 even though loop executes 100 times.
continue ;
printf ("\n%d", i);
}
}
7. default This is used with switch statement the default executes when all the cases are failed to match. Code - void function (void)
{
char ch;
printf ("\nYou Entered A");
ch = getchar ();
switch (ch)
{
case 'A' : printf ("You Entered A");
break;
case 'B' : printf ("You Entered B");
break;
case 'C' : printf ("You Entered C");
break;
default : printf ("You Didnot Entered A, B or C");
}
8. do This a keyword use to generate a loop in combination with while keywords. This loop always executes once. This is a exit control loop that is condition for exit is checked at the end of execution of loop. Code - void function (void)
{
int i = 0;
do
{
printf ("\n%d", i);
i++;
} while (i>10) \\ Loop prints values 0 to 9.
}
9. double This is used to declare a real\Decimal data type. Double data type is of 8 bytes and is used to store a floating point numbers of very high accuracy. Code - void function (void)
{
double a = 20.007;
double b;
b = scanf ("%lf", &b);
printf ("%lf %lf",a, b);
}
10. else This keyowrd can only be used with if and not by its own. The code block asssociated with else is executed when conditon of if fails to satisfy. ALways either if code block or else code block is executed but never both. Code - void function (void)
{
int a,b;
scanf ("%d %d", &a, &b);
if (a==b) printf ("A equals to B");
else printf ("A not equals to B");
}
11. enum The enum keyword is use to create user defined integer variables . In other words its giving name to a integer number. Enum keyword is hardly used for programming due its little use. The default value of first enum variable is zero. Intial Value of any variable can be changed and next value is one greater than the last. Following example will make it clear - Code - enum alphabets {A,B,C=100,D};
void function (void)
{
printf ("%d %d %d %d", A,B,C,D); \\This displays 0 1 100 101
}
12. extern extern is a storage class specifer ie. its used for modfying meaning of the variable during declaring it . It allows you to declare variables without defining them. In layman terms this means that it tells the compiler that the variable has been defined somewhere else and he have to look for a match. Varible can be a global variable of same or other files. Code - void function (void)
{
extern int a;
printf ("%d" , a); \\ Displays 10;
}
int a = 10;
13. float This is used to declare a real\Decimal data type. Float data type is of 4 bytes and is used to store a floating point numbers of low accuracy or precision. Code - void function (void)
{
float a = 20.007;
float b;
b = scanf ("%f", &b);
printf ("%f %f",a, b);
}
14. for This keyword is used to generate loops. This keyword is very frequently used. Every for statement has three parts, intialisation, conditon and increment. Intialisation intialise the loop control variable, Condition checks the conditon for loop execution and increment changes the value of loop control variable. These parts can do more than above mention function but its highly reommended you use them traditionally. Any or all of the above parts can be empty. Each parts are seprated by semicolon. Multiple staments in each part can be linked together using comma. For can also be used to generate infinite loops. Code - void function (void)
{
int a,b;
for (a = 0, b = 10; a < b;a++)
{
printf ("%d", a); \\ Prints numbers 0 to 9
}
}
15. goto Goto is used for jumping from one stament to another. It can also be used for generating loops. You cann't jump from one function to another. To mark a point to jump to label stament is used. Use of goto is highly discouraged in C Programming due its inherent properties of breaking the code and making it unreadable. Code - void function (void)
{
a = 1;
loop:; \\ label stament
printf ("\n%d",a);
if (a < 10) goto loop ; \\ jump statement
}
16. if If is one the most frequnetly used keyword . It is used for condtion checking. It can also accompany an else block which is executed when if condition fails. Else block is optional. Code - void function (void)
{
int a,b;
scanf ("%d %d", &a, &b);
if (a==b) printf ("A equals to B");
else printf ("A not equals to B");
}
17. int This is used to declare a integer data type.Integer data type is of 2 or 4 bytes depending upon the Operating System. Code - void function (void)
{
int a = 10;
int b;
b = scanf ("%d", &b);
printf ("%d %d",a, b);
}
18. long This a type modifier used with int and double to increase its range by increasing its size. Long int is of 4 bytes while long double is of 16 bytes. Code - void function ( void)
{
auto int a = 10;
auto int b;
b = a;
}
19. register Its a storage class specifier . Variable marked as register will be save into CPU register rather than CPU memmory leading to very fast access. Normally int and char gets benfit from register modifier and also only limited numbers of variables can be simultaneously stored in register. These type and number are highly machine dependent. Also register is just an request to compiler and can be completely ignored. Register varaibles cannot be addresed using pointers. Generally loop control variable are made register variables. Code - void function (void)
{
register int a;
for (a = 0; a < 10;a++)
{
printf ("%d", a); \\ Prints numbers 0 to 9
}
}
20. return This keyword is used to return from a function. In void returning statements this keyword is optional. In non void returning functions this keyword must be present and returns a value associated with the functions to the calling functions. Main function returns values to operating system. Functions can return value in form of any valid data type. Code - int function (void)
{
int b;
b = scanf ("%d", &b);
return b; \\ returns the value of b to the calling function.
}
21. short This is a type modifier and is used to modify int. As you know int can be of 2 or 4 bytes depending upon the machine. To make the code portable you cannot assume a size to vary from machine to machine. Short int is always of 2 bytes. Code - void function (void)
{
short int a = 10;
short int b;
b = scanf ("%d", &b);
printf ("%d %d",a, b);
}
22. signed It is a type modifier applicable to int and char data type. It tells the compiler that first bit of the data type is sign bit which tells wether the number is positive or negative. Int is signed by default while char is unsigned by default. Signed variables have half the range of those of unsigned varaibles but can distinguish between positve and negative numbers. Code - void function (void)
{
signed int a = 10;
signed int b;
b = scanf ("%d", &b);
printf ("%d %d",a, b);
}
23. sizeof This keyword is used to calculate sizeof variable and standard data types. The object whose size has to be calculated is put into brackets following operator. Sizeof is used to make code more portable. Sizeof returns value of type size_t which is defined as another name of unsigned int and is capable of storing largest object size possible. Sizeof is also used in dynamic memory allocation. Code - void function ( void)
{
printf ("Int has size on this system %d bytes", sizeof (int));
}
24. static Static is a storage class specifier. It can be applied to any built in data type. It has different effect on local and global varaibles. When applied to global variables its scope is limited to file only ie. other files included in the program has no clue of its existence. When applied to local variables the variable is stored in global variable memmory space and not on stack. Even though the varaible has functions scope it does not get destroyed like local variable when function returns, instead it retains it value on next call. Static local varaibles are intialised only once on the first function call. Code - void function ( void)
{
static int a = 0;
a++;
printf ("Function have been called %d time",a);
}
25. struct Its used to declare user defined aggregated varaibles known as structure. Each member of structure is defined in code block following the keyword . Name of the structure comes in between the struct keyword and the opening curly bracket. Name of the objects follows the closing curly bracket. Either name or object are optional but not both. Code - struct my
{
int a;
float b;
}o1;
void function (void)
{
struct my o2;
o2.a = 10;
o1 = o2 ;
printf ("%d %d", o1.a ,o2.a);
}
26. switch Its a multiple selection statement. Several cases are coded. When value is passed to switch, the execution of code start where case is match and dont stop until it comes across a break statement. If no case match default case is executed if available. Only int and char constants can be used as case paramenters. Swithc is most commonly used to generate menus. Code - void function (void)
{
char ch;
printf ("\nYou Entered A");
ch = getchar ();
switch (ch)
{
case 'A' : printf ("You Entered A");
break;
case 'B' : printf ("You Entered B");
break;
case 'C' : printf ("You Entered C");
break;
default : printf ("You Didnot Entered A, B or C");
}
}
27. typedef Using typedef you define new names of exisiting data types. This does not create a new physical datatype but is simply new name of calling existing typedef. Typedef helps in making code portable and more readable. Code - typedef int A;
typedef A B;
void function (void)
{
A a = 10;
B b; \\ A and B both created an int variable.
}
28. union The union Keyword is used to defined a user defined varaible whose each member share the same memmory location. Each member of union is defined in code block following the keyword . Name of the union comes in between the union keyword and the opening curly bracket. Name of the objects follows the closing curly bracket. Either name or object are optional but not both. Code - union my
{
int a;
float b;
}o1;
void function (void)
{
union my o2;
o2.a = 10;
o2.b = 0.007;
o1 = o2 ;
printf ("%d %d", o1.a ,o2.a);
printf ("%f %f", o1.b ,o2.b);
}
29. unsigned It is a type modifier applicable to int and char data type. It tells the compiler that first bit of the data type is data bit not sign bit. Int is signed by default while char is unsigned by default. Unsigned variables have double the range of those of signed varaibles but cannot distinguish between positve and negative numbers. Code - void function (void)
{
unsigned int a = 10;
unsigned int b;
b = scanf ("%ud", &b);
printf ("%ud %ud",a, b);
}
30. void This keyword appears frequently in c programming. It has different meaning at different postion. It is used represent a non value returning function. It is used to represent a empty parameter list of functon. It is also used to create generic pointers. These pointers can be converted to any valid pointer type. This is also pointer returned by dynamic memory allocation functions. Code - void function (void)
{
int a = 10;
void *ptr;
int *b;
ptr = &a;
b = (int *) ptr;
}
31. volatile Its a type specifier. It tells the compiler that variable can get change without explicit specifications. This is imortant because some compilers doesnot assume the value to modify until it occurs on left hand side of the expression in program leading to wrong results. Example of such variable can be the variable used to store system time. Its important to note that volatile is not opposite to constant. A variable can be simulatenously constant and volatile. This make sure value of variable is change only by external hardware means. Code - void function ( void)
{
volatile constant int a = inportb (0X60);
printf ("%d",a);
}
32. while This keyword is used to generate loops. This is an entry control loop that is value is checked starting of loop. Loop doesnot execute even once if conditons fails. Code - void function (void)
{
int i = 0;
while (i>10)
{
printf ("\n%d", i); \\ Loop prints values 0 to 9.
i++;
}
}
33. _Bool C99 has introduced this new data type similar to C++ bool. This data type can store only two values true or false represented by 0 or 1 (Non Zero) respectively. Also true and false macro defined in header <stdbool.h> Code - void function (void)
{
int a;
_Bool b = true;
for (a = 0; b; b;a++)
{
printf ("%d", a); \\ Prints numbers 0 to 9
b = (a<10);
}
}
34. _Complex _Complex is a new data type introduced in C99. This type is used to store real part of complex number and used with _Imaginary to provide support for Numeric Programming. Only float, double and long double type of _Complex forms is defined and this data type find very less uses in C programming. Code -
void function ( void)
{
float_Complex a = 1;
float_Imaginary a = 2;
printf ("Our Imaginary number is %f %f",a,b);
}
35. _Imaginary _Imaginary is a new data type introduced in C99. This type is used to store imaginary part of complex number and used with _Complex to provide support for Numeric Programming. Only float, double and long double type of _Imaginary forms is defined and this data type find very less uses in C programming. Code -
void function ( void)
{
float_Complex a = 1;
float_Imaginary a = 2;
printf ("Our Imaginary number is %f %f",a,b);
}
36. inline This keyword is taken from C++ . Its applicable to functions. This keyword tells compiler that the function has to be expanded inline. This means that where ever in the program there is a function call to this function the function code has to be pasted there. This makes the execution of program faster at expense of storage space as size of the code increases. Inline function is only a request to the compiler and various compilers decide in their own way wether the function deserve to be inline or is thier an efficiency gain by making function inline only then the function is made inline. Generaly a function which is small in size and called several times by your program should be made inline. Code - inline void function (void)
{
int a = 10;
int b;
scanf ("%d", &b);
a = b;
printf ("a is now %d", a);
}
37. restrict This a very innovative new feature added by C99. Restrict keyword are applicable only to pointers. Restrict qualifiers tells the compiler that pointer is the only mean by which the object can be accessed. Another pointer can access it only if its based on the first one. This enables complier to produce highly efficient code by assuming that if two pointers are declared as restrict then these two pointers doesnot overlap. Code - void function (restrict int *a, restrict int *b) \\ a and b pointers cannot overlap ie. point to same memmpry location.
{
int temp = *a;
*a = *b;
b = temp;
}
|