The One-Stop Solution to Learn Everything You Need to Know About Variables in C

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.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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:

Syntax: 

<data type> <variable-list>;

Example: 

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.

Syntax: 

<data type> <variable-name = value>;

You may also declare and initialize multiple variables at the same time as given below:

Syntax: 

<data type> <var1 = val1>, <var2 = val2>,.....<varn = valn>;

Example: 

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.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

variables_In_C_1

Invalid Variable Names Declaration:

Variables_In_C_2

Output: 

Errors for all the invalid variables declaration are shown in the below image.

Variables_In_C_3

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. 

Example: 

Variables_In_C_4

Output:

variables_In_C_5

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.

Example: 

Variables_In_C_6

Output:

/variables_In_C_7

After Global variables, you have Static variables.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

3. Static Variables

Variables declared with a keyword static are static variables, whose lifetime is throughout the program run time.  

Example: 

Variables_In_C_8

Output:

variables_In_C_9

  • 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.

Example: 

Declare external variables in a file with .h extension(a header file demo.h) 

Variables_In_C_10

In File 1(demo. c): access external variables x and y to perform addition operations by including the header file(demo. h).

Variables_In_C_11

Output:

Variables_In_C_12.

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).

Variables_In_C_13

Output:

Variables_In_C_14

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. 

Example: 

Variables_In_C_15.     

Output:

variables_In_C_16

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Next Steps

"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.

About the Author

Hoor Sania SHoor Sania S

Hoor is a Bangalore-based Research Analyst. She has a knack for learning computer languages and technologies. In her free time, she enjoys dancing and singing.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors