For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
Now Reading
131. Is C language case sensitive
132. Fibonacci Series in C
In C language, variables can be considered containers that store data values such as numbers, characters, etc.
Understanding variables is essential to programming as it allows you to store and manipulate data while executing different programs. Variables help store temporary or permanent values required for your program to run calculations, make decisions and interact with the user.
This tutorial will give you an in-and-out understanding of what variables are in C programming, their types, use cases, and everything relevant.
So let’s get started!
In C, a variable is a named storage location in the computer's memory where you can store values. It acts as a container that holds the value of a specific data type, such as integers, floating-point numbers, characters, etc.
Here are some prime reasons why you should have a sound understanding of variables in C programming.
Variables allow you to store different types of data and perform operations with them. For example, you can store user input, perform mathematical calculations, or manipulate strings using variables.
Variables are essential for tracking the state of your program. You can use variables to store intermediate results, update their values based on program logic and use those values later in the program.
With variables, you can create programs with dynamic behaviours. Variables can change their values during program execution based on user input, program logic, or external factors. This allows your programs to adapt and respond to different situations.
Variables help you to organise your codes by adding meaningful names to different data values. This makes your code more readable and easier to understand, both for yourself and for others who might read or maintain your code.
However, please note that to use variables in C, you need to declare them first. A variable declaration specifies its name and data type. For example -
int age; // Declaring an integer variable named "age" |
Once declared, you can assign values to variables using the assignment operator (=). For example -
int age = 25; // Assigning a value of 25 to the variable "age" |
Variables can be used in expressions, passed as arguments to functions, and their values can be modified as your program executes.
Understanding variables and how to work with them is a fundamental concept in C programming, as it forms the basis for more complex operations and concepts. It allows you to store and manipulate data effectively, making your programs more powerful and versatile.
Variables in C are defined through three aspects: variable definition, initialisation, and declaration.
The variable definition refers to the process of declaring a variable and allocating memory for it. It involves specifying the variable's name and data type. In C, a variable definition typically includes the storage class (optional), data type, and variable name. For example -
int age; // Variable definition: "age" is an integer variable |
In the above examples, the variables ‘age’, ‘pi’, and ‘initial’ are defined with their respective data types (‘int’, ‘float’, and ‘char’).
Variable initialization involves assigning an initial value to a variable at the time of its declaration. Initialization is optional, and variables can be used without being initialised, but it's good practice to initialise variables to a known value before using them. You can initialise a variable using the assignment operator (‘=’). For example:
int age = 25; // Variable initialization with an initial value of 25 |
In the above examples, the variables ‘age’, ‘pi’, and ‘initial’ are not only defined but also initialised with specific values.
Declaration of variable in C refers to the process of introducing a variable by specifying its name and data type without allocating memory for it. It informs the compiler about the existence of a variable before it is used. A variable can be declared without initialization. For example -
extern int age; // Variable declaration: "age" is an integer variable |
In the above examples, the variables ‘age’, ‘pi’, and ‘initial’ are declared using the ‘extern’ keyword. This informs the compiler that the memory for these variables will be allocated elsewhere, such as in another file.
Variable declaration is typically used when you want to use variables defined in other files or in a header file since variable declaration introduces a variable by specifying its name and data type without allocating memory.
In C, the syntax is used for declaring variables and comprises multiple components such as data type, identifier, initialization, and semicolon.
Every variable in C has to have a data type that helps specify the type of data the variable can hold. Some common data types in C include ‘int’ (for integers), ‘float’ (for floating-point numbers), ‘char’ (for characters), and ‘double’ (for double-precision floating-point numbers), among others.
An identifier is the name given to a variable. It can be any valid combination of letters (both uppercase and lowercase), digits, and underscores. The first character of an identifier must be a letter or an underscore.
The identifier should also not be a reserved keyword in C (such as ‘int’, ‘float’, etc.). Choose meaningful and descriptive names for your variables to enhance code readability.
Initialization is optional but recommended. It involves assigning an initial value to the variable at the time of declaration. Initialization can be done using the assignment operator (=). For example, ‘int age = 25;’
Each variable declaration statement in C must end with a semicolon (‘;’). The semicolon is used to indicate the end of the statement.
Below is an example of the syntax used to declare variables in C.
#include <stdio.h> |
Here, in this example,
Also, note that it is a must to declare variables before you use them.
To use variables in C, you need to follow a four-stepped approach, which starts with Declaration, followed by Initialization and Assignment and ends with Usage.
1. Variable Declaration: Start by declaring the variables with the appropriate data types and names. This informs the compiler about the existence of the variables. For example -
int age; // Declaration of an integer variable named "age" |
2. Variable Initialization: Optionally, you can initialize the variables with initial values at the time of declaration. Initialization assigns a value to the variable. For example -
int age = 25; // Initialization of "age" with a value of 25 |
3. Assignment: To assign a new value to a variable at any point in your program, you use the assignment operator (=). The new value replaces the existing value stored in the variable. For example -
age = 30; // Assigning a new value of 30 to the variable "age" |
4. Usage: You can use variables in expressions, statements, and function calls throughout your program. For example -
int newAge = age + 5; // Using "age" variable in an expression |
In the above examples, we use the variables age and pi in an expression, a printf statement, and a scanf statement, respectively. You can perform mathematical calculations, display variables' values, or take user input using variables.
Also, remember that in C, variable names are case-sensitive. Therefore, age and Age are considered different variables.
Based on characteristics, variables in C are of different types. Here are some major types of variables in C examples.
Local variables are declared within a block of code, such as within a function. They are only accessible within the scope of the block where they are declared. Local variables comprise automated storage duration, which means they are generated when the block is entered and destroyed when the block is exited.
The global variable in C is declared outside of any function or block. They are accessible from any part of the program and have a global scope. Global variables have static storage duration, meaning they are created at program startup and remain in memory until the program terminates. Global variables should be used with caution since they can lead to code complexity and make it harder to track dependencies.
Function parameters are variables that are passed to a function during a function call. They allow you to pass values to functions and use them within the function's code. Function parameters act as local variables within the function and are initialised with the values passed in from the calling code.
Static variable in C have a local scope but retain their values between function calls. They are declared with the static keyword. Static variables have static storage duration, meaning they are created and initialised only once, and their values persist across multiple function calls.
Constants are variables whose values cannot be changed once they are assigned. They are declared using the const keyword. Constants can improve code readability and maintainability by explicitly indicating that a value should not be modified.
Pointer variables store memory addresses of other variables. They allow you to indirectly access and manipulate variables by referring to their memory locations. Pointers are declared using the ‘*’ symbol. Pointers play a crucial role in advanced C programming, enabling dynamic memory allocation, data structures, and more.
Finally, for a quick recap, a variable in C is a named storage location that is used for storing data during program execution. They have data types that determine the kind of data they can hold, i.e., integers, floating-point numbers, characters, etc.
We discussed variable declaration and definition in C and learned that you must declare a variable before using them. Depending on the type ( local or global), the scope of variables in C also changes.
Understanding variables is crucial as they allow you to store and manipulate data in programs. By following the syntax rules, declaring variables with the appropriate data types, and using them effectively, you can create dynamic and functional C programs.
On the other hand, upskilling with upGrad’s Data Science and Analytics Bootcamp will further offer you an edge to expand your knowledge and gain efficiency in programming.
Q: What is a variable in C language?
Variable refers to the name provided to a storage location, which holds the value of the data stored. This may include, grades, height, income or your residential details.
Q: What are the different types of variables?
Different types of variables in C include: Local, Global, Function, Static, Constants and Pointer variables in C.
Q: Are there any restrictions for using variable names in C?
Yes, there are several restrictions to ensure the correct usage of variables in C. For example, variable names are case-sensitive, and must contain only alpha-numeric characters and underscores, while it should not contain any spaces.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.