Variables in C

Written by

studymite

Variables in C

Let's understand Variables with a simple example:
Calculating the perimeter of rectangle:

Variables in C Language

Here,
Constant: The value which can't be changed.

Variable: Values which can be changed. (This should be a unique name, also known as identifier. To name a variable/identifier there are some predefined rules that must be followed.)

A variable must be of defined data type.

  • Rules for naming an identifier :
  • 1. Can start with a number(0 to 9) or underscore(_).
  • 2. Cant have spaces.
  • 3. Cant have spaces.

To use variable in a C program, we have to first define it, So the first thing is to tell the compiler, the type of data we are going to store.

Suppose we want to store age then,

#include &ltstdio.h>
 
int main()
{
  int age;
  age = 24;
  printf(“I am %d years old.”,age);
  return 0;
}

    </pre>

Variables in C Language
Output :

I am 24 years old.
  

We can also use mathematical
operators on variables.
For Example:

#include &#038;ltstdio.h>
 
int main()
{
  int age;
  age = 2018 - 1994;
  printf(&ldquo;I am %d year old&rdquo;,age);
  return 0;
}

    </pre>
Variables in C