Variables in the programming languages

Techerz
2 min readFeb 20, 2022

--

What is a variable?

A variable is required to store a value, like numbers or characters, just like in maths.

Their value can be changed as per the requirement of the program. There are also constants whose value remains the same just like acceleration due to gravity ‘g’ is always 9.8 m/s², or the speed of light ‘c’ is 3*10⁸ m/s.

There are two parts to a variable:

  1. Name — Such as ‘x’. It is also known as an identifier. An identifier is a name that we provide to a function, variable or constant in C.
  2. Data type — This specifies the type of data being stored such as integer, character, decimal number, etc, we do it using keywords (stored words) in C. The different data types are:

a. int — Integers.

b. char — Characters. Stored using single inverted commas (‘a’).

c. double/float — Decimal numbers.

d. long — Increased range when used with the ones above, like long int.

e. short — Used as long, but this decreases the range.

f. unsigned — Only positive values.

g. signed — both positive and negative.

Declare a variable:

For example, declaring integer — int x;

declaring and initializing — int x=100;

Declaring a constant:

const int x=100;

as a preprocessor statement — #define LIMIT 100

Printing a variable:

printf(“Value of x=%d”,x);

We use %d for int, %c for char, %f for float,

Taking variable value from user:

printf(“Enter a number\n”);

scanf(“%d”,&x);

printf(“Value of x=%d”,x);

Remember to make a good interface for users, this is what differentiates between a good and great programmer. Anyone can write a code, but only great programmers make it easy to interact with users.

Video Link: https://www.youtube.com/watch?v=PJD2dxNeNR8

--

--