ADVERTISEMENT
Variables In C
In the language C, a variable is a named memory address that is used to store and retrieve data. The variable can be used to hold many kinds of data and can be reused an unlimited number of times to store different sorts of data.
They can be thought of as the names assigned to the memory location, allowing us to access it without needing to commit the memory address to memory. The type of data that the variable stores determines its size.
C Variable Syntax
The syntax to declare a variable in C specifies the name and the type of the variable
// defining single variabledata_type variable_name = value;
or
// defining multiple variabledata_type variable_name = variable_name1, variable_name2;
- data_type: Type of data that a variable can store.
- variable_name: Name of the variable given by the user.
- value: value assigned to the variable by the user.
Example
int marks; // integer variable
char ch; // character variable
float amount; // float variablesC ProgrammingVariable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.
Variable Definition in C
A variable definition tells the compiler where to store the variable and how much space it needs. A variable definition lists one or more variables of a certain type and specifies the type of data they hold.
type variable_list;C ProgrammingAnd in this case, type must be a valid C data type, like, char, int, float, double, bool, or any user-defined object. variable list can have one or more identifier names, separated by commas.
Here are some declarations that are valid:
int x, y, z;
char c, ch;
float f, amount;
double d;C ProgrammingThe line int x, y, z; declares and defines the variables x, y, and z. It tells the compiler to make three int variables with the names x, y, and z.
A defined variable will contain some random garbage value till it is not initialized.
Variable Initialization in C
Initialization of a variable is the process where the user assigns some value to the variable.
Example
int marks; // variable definition
marks = 70; // initialization
// OR
int marks = 70; // variable definition + initialization
C ProgrammingHow to use variables in C?
You can see an example of this below. Variables are declared at the top, but they are defined and set up in the main function.
#include <stdio.h>
int main()
{
// declaration with definition
int marks;
printf("Marks: %d: ", marks);
// initialization
marks = 70;
// declaration + definition + initialization
int total_marks = 150;
printf("Value of defined_var after initialization: %d\n",marks);
printf("Value of total marks: %d", total_marks);
return 0;
}
C ProgrammingMarks: 0
Value of marks after initialization: 70
Value of total marks: 150The same idea applies to declaring functions: you give them a name when you declare them, but you can give their definition anywhere else.
Example with function
#include <stdio.h>
// function declaration
int func();
int main() {
// function call
int i = func();
printf("Function value: %d",i);
}
// function definition
int func() {
return 0;
}C ProgrammingFunction value: 0Rules for Naming Variables in C
- A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
- The first letter of an identifier should be either a letter or an underscore.
- You cannot use keywords like
int,whileetc. as identifiers. - There is no rule on how long an identifier can be. However, you may run into problems in some compilers if the identifier is longer than 31 characters.
Note:
You should always try to give meaningful names to variables.
For example: firstName is a better variable name than fn.
Type of Variables in C
- Local Variables
- Global Variables
- Static Variables
- Automatic Variables
- Extern Variables
- Register Variables
1. Local Variables in C
A Local variable is declared inside a function or a block of code. Its scope is limited to the block or function in which it is declared.
Local Variable Example:
// declare and print local variable inside a function.
#include <stdio.h>
void function()
{
int marks = 70; // local variable
printf("%d", marks);
}
int main()
{
function();
}C ProgrammingIn the above code, marks can be used only in the scope of function(). Using it in the main function will give an error.
Function value: 702. Global Variables in C
A Global variable is declared outside the function or a block of code. Its scope is the whole program i.e. we can access the global variable anywhere in the C program after it is declared.
Global Variable Example:
// declare and print global variable.
#include <stdio.h>
int marks = 50; // global variable
void std1()
{
printf("std 1: %d\n", marks);
}
void std2()
{
printf("std 2: %d\n", marks);
}
int main()
{
printf("The Value of marks: %d\n",marks);
std1();
std2();
return 0;
}
C ProgrammingIn the above code, both functions can use the global variable as global variables are accessible by all the functions.
The Value of marks: 50
std 1: 50
std 2: 50Note: When we have same name for local and global variable, local variable will be given preference over the global variable by the compiler.