In this article we will learn about variables in C. First we will see the definition of a variable and then learn how to declare and initialize a variable in C.
In the programs, generally we need to store values in the memory and perform operations on those values. This can be achieved in C, by the concept of variables.
A variable is a placeholder for holding a value in the main memory (RAM). As the name implies, the value in the variable can change at any point of execution of the program. For using variables in our programs, there are essentially two steps:
1) Declare the variable
2) Initialize the variable
Declaring a Variable
Before using a variable in the program, we have to declare the variable. The syntax for declaring a variable in a program is as shown below:
The “type” in the above syntax represents the data type. The “variable-name” is the identifier. There are certain rules that must be followed while writing the variable name. They are as follows:
- A variable name must always start with an alphabet (letter) or an underscore ( _ ).
- The variable name must not be more than 31 characters. The suggested length of a variable name is 8 characters.
- C is case sensitive. So, the variable name “average” is different from “AVERAGE”.
- Keywords must not be used for declaring variables.
- White spaces are not allowed within the variable name.
Initializing a Variable
After declaring the variable, we can assign a value to the variable. This process of assigning a value to the variable is known as initialization. Syntax for initializing a variable is as shown below:
The value we assign to the variable depends on the data type of the variable.
Example:
The declaration and initialization can be combined into a single line as shown below:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply