TOP CATEGORIES

×

What are Variables in C Programming

Variables in C_1

Variables in C

Variable in C is a user-defined name of a memory block where we store value (data). Declaring a variable means reserving a block of memory in a large memory (RAM) and providing an identity (name) to that block of memory.

  • Variable names are easy to remember and understand.
  • Variable names help us to store value in reserved memory blocks.
  • Variable names help us to retrieve values from values stored in memory blocks.
  • Users can create as many variables as they want.

Take an example of a storeroom that has many cabinets. You kept different ingredients in each cabinet. You wrote the name of the ingredient on a particular cabinet, it will make you find out particular ingredients very easily in the future. Now you can imagine the storeroom as a large memory (RAM), a cabinet as a memory block, and an ingredient name as a variable.

Variable Declaration In C Programming

Data-Type Variable-Name;

Data-Type defines the type of value stored in a particular memory block.

Variable-Name is the user-provided name to a particular memory block.

Example

int age;

In the above example, int is Data-Type whereas age is a variable. Let’s see some more examples

char is_true;

string name[50];

float temperature;

In the above example char, string, float are Data-Types, and is_true, name, temperature are variables.

Note: Don’t worry about Data-Types, we will learn this in the next article.

Variable Naming Convention in C

  • Variable names can consist of alphabets, digits, and special symbols (only _ is allowed).
  • Variable Name can't start with a number.
  • The variable name is case sensitive, for example (age and Age both are different).
  • Variable names can not contain any white space.
  • C keywords are not allowed as a variable name.

Variable’s Data-Type & Size

When a variable declares it reserved some memory block, The size of the reserved block is defined by the Data-Types. The size of basic Data-Types is given below (for a 64-bit system).

  • char (1 byte)
  • int (4 bytes)
  • float (4 bytes)
  • double (8 bytes)

Explanation

  • When a char type variable is declared, it reserved 1-byte space in memory
  • When an int type variable is declared, it reserves 4 bites space in memory.
  • When a float type variable is declared, it reserves 4 bytes of space in memory.
  • When a double type variable is declared, it reserves 8 bites space in memory.

Till now we have learned how we can create (declare) a variable, now let’s learn how we can assign (store) a value in a reserved memory block using a variable.

Variable Initialization in C

Variable Initialization in C is the process of assigning value to the variable. Initialization of variable can be done in two ways

  • Pre Initialization
  • Post Initialization

These are just different terms for saying although technically these are the same thing.

Example

1. Pre Initialization

int a=20

2. Post Initialization

int salary;

salary=20000;

Since Variables & Data Types are strongly related to each other, To properly understand Variables it is important to understand data types properly.

Email: advertise@gdatamart.com

Donate: To Support US

© 2024 GDATAMART.COM (All Rights Reserved)