Variable Declaration in C
What is a Variable?
A Variable is nothing more than a name for a memory place where data/information can be stored.
Any alphabet (from a to z or A to Z), the digit (0 to 9), and underscore (_) combination can be used for the variable name.
Variable names that begin with digits are not valid.
Every variable has a type, or data type (such as int, char, float, or double), which instructs the compiler on how much memory to allot to the variables in accordance with their type and how to handle bits contained in that specific memory location when carrying out certain operations.
Example:
char Car ='Toyota';
Car is a char type variable in this case. The value "Toyota" is given to the variable car.
Variable Declaration:
The variable must be declared before being used in various functional components. The compiler receives two instructions from a variable declaration:
- The variable's name
- What kind of information will the variable store
The general syntax for declaring a variable
There are two ways to declare variables in statements that use variables:
1. Declaration of a variable without assigning any initial data to it:
Syntax :- data_type variable_name;
//example
int a;
In the above example, the data type is “int”, and the variable name is “a” without any initialization of a value.
2. Declaration of a variable with assigning any initial data to it:
Syntax :- data_type variable_name=value;
//example
int a=6;
In the above example, the data type is “int”, and the variable name is “a” with an assigned value of “8”.
Ways of Variable Declaration
In C programming, variables can be declared in two different ways.
1. Primary Type Declaration
2. User Defined Type Declaration
1- Primary Type Declaration:
In this type of declaration, we use the Primitive data type, which is also known as the built-in data type.
The primary data types that are most frequently used are int, float, char, boolean, double, long, etc.
- Single Primary Type Declaration
We declare a single variable.
float percentage = 86.83;
- Multiple Primary type declarations in the same line
We can declare multiple variables in the same line with the help of a comma(,) which separates the multiple variables.
int a=5,b=6,c=7;
- Multiple primary type declarations in the different line
We can declare multiple variables in the different lines. To separate the numerous variables for this, we must use a semicolon (;).
int k=78;
float l = 87.8;
char m = 'c';
2-User Defined Type Declaration:
Data types that have been created by a user are known as user-defined data types. Since they are not built-in, class functions must be used to access them.
Struct, Union, enum, typedef, etc., are a few of the most often used data types.
1.typedef :
In order to declare the data type, we must use the keyword typedef. A feature called "type definition" in C programming enables a programmer to define an identifier representing an existing data type. Later in the program, variables can be declared using the user-defined identifier. The general syntax for declaring a variable using a user-defined type declaration is:
typedef int student_age;
typedef char student_name;
typedef float student_percentage;
student_age, student_name, and student_percentage are three new data types we have established here. Now we are having these data types, we can declare variables, as seen below.
student_age 16,17,18;
student_name Om,Shiv,Shankar;
student_percentage 88.73,88.40,90.2;
2. Union:
Unions give you the ability to make variables that share a shared memory area. In that they seem to be an aggregate data type. Unions and structures have some similarities. Union is used when we just need to reach one member at a time. A union variable's memory usage is equal to the amount of storage space required by the largest data member of the union.
union bag
{
char bookname[5] ;
int pen;
};
3.Structure:
The structure is also a user-defined data type in the C programming language that enables us to aggregate data of various sorts. A complex data type that is more meaningful can be built with the aid of structure.
To define a structure, use the struct keyword. A new data type called struct, which is made up of both primary and derived data types, is defined.
struct player
{
int player_no;
Char player_name;
}
Rules For Variable Declaration
We must declare a variable in C language with an appropriate data type and variable name. The following are some guidelines to remember when declaring a variable in C:
- Any alphabet, number, and underscore combination are allowed in a variable name.
- The variable name, however, shouldn't begin with a number.
- It will be better if we define variable names with some relevant names, as this will make the variable's function more obvious.
- The C language is case sensitive, and it treats lowercase and uppercase extremely differently. Typically, we maintain the variable name in lowercase.
- A semi-colon must follow each declaration statement (;)
- It is improper to declare two variables with the same name in the same scope.