Keywords in C are predefined reserved words it means their meaning and purpose already defined in C library used to perform internal operation. Every programming language has a set of keywords with predefined values that cannot be used as variable or function names.
C has 44 keywords including C89 – 32, C99 – 5, C11 – 7. As we already know C is a case sensitive language, all keywords are in a lowercase.
But later few keywords are added which starts with an underscore and a capital letter, because identifiers of this form were previously reserved by C standard for use only by implementations.
C89 Keywords:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
C99 Keywords:
_Bool _Imaginary restrict
_Complex inline
C11 Keywords:
_Alignas _Atomic _Noreturn _Thread_local
_Alignof _Generic _Static_assert
Variables in C language
A Variable in C language is a place holder for data. In simple, Variable is a storage place with memory allocated to store given different type of input data. Variables of different data type requires different amounts of memory and only specific set of operations can be performed on them.
Variable Declaration and Definition:
Variable declaration informs the compiler what the variable name and type of input it will hold after definition. Most of the time, Variable declared before its use in program and memory assigned only after it is defined with value. Mostly, Variables declared and defined at same time.
Declaration include type followed by variable name and a semicolon. In multiple declaration variables separated using comma.
Definition include assigning value to variable, and variable is allocated memory only after it is defined.
Declaration syntax:
type variable_name; //for single variable declaration
type V_name1, V_name2, V_name3; //for multiple declaration
Variables Naming rules
- Variable name should not start with a digit.
- Special symbols are not allowed in variable name except underscore (_).
- Allowed Capital Letters (A – Z), Small Letters (a – z), Digits (0 – 9) and underscore.
- Blank or spaces are not allowed within variable name.
- Keywords cannot be used as variable name.
- C is case-sensitive hence upper and lower case names are different and it is suggested to use variable names in lower case.
#include <stdio.h>
int main ()
{
int xyz123 = 10; //declared and defined
int exc; //declared and defined with garbage value
int _u, a, e;
/* multiple declarations and definition with garbage value generate warning */
//int 3ar;
//uncomment above line to see effect
printf("%d %d %d %d %d \n", xyz123,exc,_u,a,e);
return 0;
}
Output:
10 8 76 4200672 4200766
Warning message:
||=== Build: Debug in ex (compiler: GNU GCC Compiler) ===|
\main.c||In function 'main':|
\main.c|11|warning: 'exc' is used uninitialized in this function [-Wuninitialized]|
\main.c|11|warning: '_u' is used uninitialized in this function [-Wuninitialized]|
\main.c|11|warning: 'a' is used uninitialized in this function [-Wuninitialized]|
\main.c|11|warning: 'e' is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
Types of Variables in C
Based on Variables availability in or outside block where it is declared and defined variables are classified as follows.
- automatic variable
- local variable
- global variable
- static variable
Automatic or Local Variable
By default, All the Variables declared inside the block are automatic/local variables and their availability or scope end with block. Use of keyword auto is optional here.
Local variable must be initialized before it is used.
#include<stdio.h>
int print();
int main () {
int i=1; //local or automatic variable
auto int j=2; //keyword auto optional here
printf("%d %d",i,j);
//print();
return 0;
}
/*
int print(){
printf(“%d %d”,i,j);
}
*/
Output
1 2
Remove comment (//,/*xx*/
) from line numbers 13 and 19 to see that variables i & j are not accessible.
Global Variables
A variables declared outside the blocks or function which hold their values throughout the lifetime of a program after its declaration are called a global variable.
Global variable available to all functions and can be accessed by any function inside the program. Any function can change its value and mostly it is declared at the start of the program.
#include<stdio.h>
int G_varabale = 20; //global variable
void add1(int);
int main () {
int i=10; //local variable
int add;
add = i+G_varabale;
printf("%d + %d = %d \n",i,G_varabale,add);
G_varabale = 50; //changing global variable
printf("%d \n\n",G_varabale);
add1(i);
return 0;
}
void add1(int i) // ‘i’ is local to this block only
{
printf("Global variable available here and we can change it here too \n\n");
printf("%d + %d = %d \n",i,G_varabale,i+G_varabale);
G_varabale = 80; //changing global variable
printf("%d \n",G_varabale);
}
Output:
10 + 20 = 30 50 Global variable available here and we can change it here too 10 + 50 = 60 80
Static Variables
Static variable holds their recent state throughout multiple function calls. Declared with keyword static these variables do not change last updated value unlike local or global.
#include<stdio.h>
void func1();
int main () {
func1();
func1();
func1();
return 0;
}
void func1(){
int i=1;//local variable
static int j=1;//static variable
i=i+1;
j=j+1;
printf("%d,%d\n",i,j);
}
Output:
2,2 2,3 2,4
Consider above example I value goes back to 2 between function calls while static hold updated value.
And there is one other important set of variables called external variable which will be explained in separate guide for better understanding.
Don’t Forget to share and Subscribe!