Menu
×
Tutorials Ms Word Tutorial Ms Excel Tutorial Ms PowerPoint Tutorial C Language Tutorial C++ Tutorial C Sharp Tutorial Visual Basic Tutorial HTML Tutorial CSS Tutorial JavaScript Tutorial WordPress Tutorial
     ❯   

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 variable
data_type variable_name = value;
            or
// defining multiple variable
data_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 variables
C Programming

Variable 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 Programming

And 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 Programming

The 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 Programming

How 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 Programming
OUTPUT
Marks: 0
Value of marks after initialization: 70
Value of total marks: 150

The 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 Programming

OUTPUT
Function value: 0

Rules for Naming Variables in C

  1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
  2. The first letter of an identifier should be either a letter or an underscore.
  3. You cannot use keywords like  int ,  while etc. as identifiers.
  4. 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

  1. Local Variables
  2. Global Variables
  3. Static Variables
  4. Automatic Variables
  5. Extern Variables
  6. 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 Programming
OUTPUT
Function value: 70

2. 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 Programming

In the above code, both functions can use the global variable as global variables are accessible by all the functions.

OUTPUT
The Value of marks: 50
std 1: 50
std 2: 50

Note: When we have same name for local and global variable, local variable will be given preference over the global variable by the compiler.

Spread the love
Scroll to Top
Scroll to Top