Variables

A variable is defined as the name given to store data in our C programs. Each variable inside a C language is of a specific type that defines the size of the memory of variables. Also, the range of values can also be stored inside the memory.

The variable name can be made of letters, digits, or underscore characters. C is case sensitive that is why upper and lower case matters a lot. Now let's know in brief about the variables in the C language.

 

Type

 Description

char

Typically a single octet(one byte). (It is an integer type. char x = 'x')

int

The most natural size of integer for the machine. (int d=3)

float

A single-precision floating-point value. (float f = 10.0)

double

A double-precision floating-point value. 

void

Represents the absence of type.

 

Variables in C

A variable in C language tells the compiler where and how much storage is to create the variable. It specifies a data type that contains a list of one or more variables. Here is the syntax of the different variables in C.

 

type variable_list;

 

Here the type is a C data type including char, int, float, etc for any user-defined object. "variable_list" contains all the variables of that type. Here is an example to understand this statement.

  • int    p, q, a;
  • char   a, ch;
  • float  b, salary;
  • double a;

 

"int p, q, a;" declares the variables "p, q, a" as int type. It helps the compiler to create variables of type int.

If you want to define some value to your variable in C language then you have to use this syntax.

 

type variable_name = value;

 

For Examples-

int a = 5, b = 7;           // defining and initializing d and f. 

byte z = 22;                // It initialize z variable as a byte and define its value.

char x = 'x';               // the variable x has the value 'x'.

Here is an example of a full program to define variables in brief.

 

Program:

#include <stdio.h>

 

// Declaration of Variables:

extern int a, b;

extern int c;

extern float f;

 

int main () {

 

   /* Defining a variable: */

   int a, b;

   int c;

   float f;

 

   /* Initializing a Value */

   a = 30;

   b = 50;

  

   c = a + b;

   printf("Value of c is: %d \n", c);

 

   f = 40.0/3.0;

   printf("Value of f is: %f \n", f);

 

   return 0;

}

 

Output:

Value of c is: 80                                                       

Value of f is: 13.333333 

Subscribe For Daily Updates