Let’s implement the keywords which we have discussed above. It comes in very handy at times, for example in a case when the name of the structure defined by you is very long or you just need a short-hand notation of a per-existing data type. Typedef: typedef is used to give a new name to an already existing or even a custom data type (like a structure). Please refer to the void pointer in C for understanding the same. Note: void also has a significant use with pointers. Similarly, when its added to a function heading, it represents that the function takes no arguments. For example, when it is used as the return data type for a function it simply represents that the function returns no value. It means it has nothing or it holds no value. But what makes it so special? void, as it literally means, is an empty data type. Syntax: static data_type var_name = var_value By default, they are assigned the value 0 by the compiler. Global static variables can be accessed anywhere within that file as their scope is local to the file. Their scope is local to the function to which they were defined.
Thus, no new memory is allocated because they are not re-declared. So we can say that they are initialized only once and exist till the termination of the program. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. Static: static keyword is used to declare static variables, which are popularly used while writing programs in C language. Syntax: extern data_type var_name = var_value The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. It can be accessed within any function/block. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. Please refer the const qualifier in C for understanding the same.Įxtern: extern simply tells us that the variable is defined elsewhere and not within the same block where it is used. const keyword is also used with pointers. Note: Constant variables must be initialized during their declaration. Syntax: const data_type var_name = var_value Or in other words, the value assigned to them cannot be modified further down in the program. Constant variables are variables which, when initialized, can’t change their value. Let us discuss some of the other keywords which allow us to use the basic functionality of C:Ĭonst: const can be used to declare constant variables. Most of these keywords have already been discussed in the various sub-sections of the C language, like Data Types, Storage Classes, Control Statements, Functions etc. There are a total of 44 keywords in C (C89 – 32, C99 – 5, C11 – 7): auto extern short while So you can imagine that the list of keywords is not going to be a small one! Almost all of the words which help us use the functionality of the C language are included in the list of keywords. Keywords are specific reserved words in C each of which has a specific feature associated with it. In the above example x is an external variable which is used in multiple files. Printf("Global variable: %d", global_variable) The only difference is that variable y is explicitly declared with auto keyword.Įxternal variable can be shared between multiple C files.We can declare external variable using extern keyword.Įxtern int x=10 //external variable (also global)
In the above example both x and y are automatic variables. Left Shift and Right Shift Operators in C/C++.
Core Dump (Segmentation fault) in C/C++.Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc().Difference between #define and const in C?.Different ways to declare variable as constant in C and C++.Internal Linkage and External Linkage in C.How Linkers Resolve Global Symbols Defined at Multiple Places?.How are variables scoped in C – Static or Dynamic?.Compiling a C program:- Behind the Scenes.Interesting Facts about Macros and Preprocessors in C.ISRO CS Syllabus for Scientist/Engineer Exam.ISRO CS Original Papers and Official Keys.GATE CS Original Papers and Official Keys.