A variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory, and have some specific set of operations which can be applied on them.
A typical variable declaration is of the form:
type variable_name; or for multiple variables: type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.
Difference b/w variable declaration and definition
Variable declaration refers to the part where a variable is first declared or introduced before its first use. Variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.
See the following C program for better clarification:
Keywords are specific reserved words in C each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the C language are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one!
There are a total of 32 keywords in C:
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
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.
Let us discuss some of the other keywords which allow us to use the basic functionality of C:
const: const can be used to declare constant variables. Constant variables are variables which, when initialized, can’t change their value. Or in other words, the value assigned to them cannot be modified further down in the program.
const data_type var_name = var_value;
Note: Constant variables must be initialized during their declaration. const keyword is also used with pointers. Please refer the const qualifier in C for understanding the same.
extern: extern simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. 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. It can be accessed within any function/block. Also, a normal global variable can me made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
extern data_type var_name = var_value;
static: static keyword is used to declare static variables, which are popularly used while writing programs in C language. 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. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere within that file as their scope is local to the file. By default, they are assigned the value 0 by the compiler.
static data_type var_name = var_value;
void: void is a special data type. But what makes it so special? void, as it literally means, is an empty data type. It means it has nothing or it holds no value. For example, when it is used as the return data type for a function it simply represents that the function returns no value. Similarly, when its added to a function heading, it represents that the function takes no arguments.
Note: void also has a significant use with pointers. Please refer to the void pointer in C for understanding the same.
typedef: typedef is used to give a new name to an already existing or even a custom data type (like a structure). 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.
Let’s implement the keywords which we have discussed above. Take a look at the following code which is a working example to demonstrate these keywords:
Hello World This is the value of the constant variable 'a': 32 'b' is a char variable. Its value is G 'c' is a long long int variable. Its value is 1000000 These are the values of the extern variables 'x' and 'z' respectively: 9 and 10 These are the modified values of the extern variables 'x' and 'z' respectively: 2 and 5 The value of static variable 'y' is NOT initialized to 5 after the first iteration! See for yourself :) The value of y is 6 The value of y is 7 The square of 5 is 25 Bye! See you soon. :)
This article is contributed by Ayush Jaggi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python | Set 4 (Dictionary, Keywords in Python)
- Variables in Java
- Static Variables in C
- Implicit initialization of variables with 0 or 1 in C
- Can Global Variables be dangerous ?
- Initialization of static variables in C
- Operations on struct variables in C
- Scope of Variables In Java
- What are the default values of static variables in C?
- How are variables scoped in C - Static or Dynamic?
- How will you show memory representation of C variables?
- Global and Local Variables in Python
- C Program to print environment variables
- Initialization of global and static variables in C
- Initialization of variables sized arrays in C