Naming has been a vital part of technology and anything that revolves around it. Similarly, programming a solution to a technical problem also involves naming. Should you ask how? That's where you must come across variables and data types.
In this tutorial about variables in c programming, you will learn about the naming conventions followed and how exactly the variables in C operate in real-time.
What Are Variables in C?
In C programming language, a variable is a user-defined or a user-readable custom name assigned to a memory location. Variables hold a value that can be modified and reused many times during the program execution.
A variable can be an alphabet or digits and start with an underscore, but you cannot declare a keyword as a variable name.
Now that you have understood the fundamental definition of the Variables in C, go through the declaration part.
Declaration of Variables in C
A few simple parameters are considered to declare a variable in the C programming language. These parameters are collectively termed syntax.
The following syntax is strictly followed to declare any variable in C:
<data type> <variable-list>;
int a, b, sum;
So far, you understood the declaration phenomenon. Now, take a step ahead and learn how to initialize variables in C once they are declared.
Initialization of Variables in C
There are two ways to utilize variables in C. To utilize variables, you either have to declare them or initialize them at the declaration stage.
When a variable is declared, C compilers store a random or a garbage value in the declared variable. If you do not wish to store a garbage value and intend to store a dedicated value in your variable, you can consider initializing your variable with a valid value.
The following will explain the process in an elaborate yet simple manner.
<data type> <variable-name = value>;
You may also declare and initialize multiple variables at the same time as given below:
<data type> <var1 = val1>, <var2 = val2>,.....<varn = valn>;
int a = 10, b=20,c;
You have come across the technical definition, declaration, and initialization of Variables in C. Now, you have an important stage. There are rules and regulations to declare and initialize the variables in C.
Now, you will go through the rules and regulations to have a smoother learning experience.
Rules for Naming Variables in C
Rules and Regulations will always make things simpler and eliminates chaos to a greater extent. Similarly, in C Programming, there are rules for Naming Variables in C, as mentioned below.
- The names for Variables in C can only have letters, an alphabet, a number, or an underscore in the first position.
- No variable in C should ever start with a special character, space, or other than a number, alphabet, or underscore.
- No keywords will be considered as a variable.
- The identifier length is limited to 31 characters.
You will go through a few examples that will help you understand the valid variable names and invalid variable names for a better understanding.
Invalid Variable Names Declaration:
Errors for all the invalid variables declaration are shown in the below image.
Now that you have understood the rules and regulations and all the technicalities revolving around the Variables in C, the only thing left out will be the types of variables in C. You will learn them in detail with proper technical definitions and examples as follows.
Types of Variables in C
C language helps you with providing a wide range of variable varieties for different programming purposes, as described below.
- Local variables
- Global variables
- Static variables
- External variables
- Automatic variables
You will go through each of them in detail.
1. Local Variables
Variables that are declared inside the functions with the keyword local are called local variables. The local variable's scope is inside the function in which it is declared.
Followed by Local variables, you will learn all about the Global variables.
2. Global Variables
Variables declared outside the functions with the keyword global are called global variables. Global variables can be accessed by multiple functions defined in the program.
After Global variables, you have Static variables.
3. Static Variables
Variables declared with a keyword static are static variables, whose lifetime is throughout the program run time.
- In the above example, you declare a static variable "sum" and initialize its initial value to zero.
- After the first function call, the sum value is incremented by one, remaining the same value throughout the program runtime.
- The sum value will be incremented by one for the following function call and store/display a new value 2.
Followed by static variables, you have external variables.
4. External Variables
External variables share the variables among the multiple C files. The extern keyword is used in the C programming language to declare external variables.
Declare external variables in a file with .h extension(a header file demo.h)
In File 1(demo. c): access external variables x and y to perform addition operations by including the header file(demo. h).
To access the same external variables x and y in the second file (demo1.c) to perform subtraction operation, you include the header file (demo. h).
Up next, you have Automatic variables as the last variety.
5. Automatic Variables
Variables declared with auto keywords are known as automatic variables. The variables declared inside the block of functions are automatic variables by default. The scope of automatic variables is inside the block in which it is declared.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
"Data Structures in C" can be your next topic. So far, you have learned the variables in C programming Language. The next fundamentals will be the data structures and the varieties in data structures used for different purposes.
If you are interested in building a career in software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages today? If yes, our Post Graduate Program in Full Stack Development should be just the right thing for your career. Explore the course and enroll soon.
If you have any questions regarding the "variables in C” tutorial, please let us know in the comment section below. Our experts will get back to you ASAP.