TRENDING TOPICS

×

Variables Declaration, Initialization & Rules

A variable is the name of a memory block/location which declares by the user, and it is used to store value.

The name of variable can not be changed once it is declared, while the value stored in variables can be changed at any point of the entire program. You can give any name to the variable except reserved keywords.

Variable declaration syntax

data_type variable_name;

Example

Explanation

In the above example, we have declared 4 variables which are

int age; → In this example int is data type and age is variable.

char is_true; → In this example char is data type and is_true is variable.

string name[50]; → In this example string is data type and name is variable.

float tempeture; → In this example float is data type and tempeture is variable.

Note: Don't worry about data type, we will study data type in detail in the next chapter.

Why Variables

Computer memory addresses are represented in the forms of ‘0’ and ‘1’ in bits segment. For example 1001, 1002, 1003 etc. we can store value at these memory locations, but it will be very complicated to remember the address in form of ‘0’ and ‘1’ for future use. To solve this issue, the concept of variables is created. Developers can provide any meaningful name to a variable.

When we create a variable we do not need to worry about where it’s going to reserve memory block in entire memory space. We just have to worry about the type and size of data that will be stored at a variable location.

c variables

In above image name, age and address are variables which are used to store respective values at memory position 1001, (1002, 1003), and (1004, 1005, 1006, 1007) locations.

Variable's Data Type and Size

While declaring a variable, it is must to define a data-type and size of space to be reserved for data storage. Data Type is used to specify the type of data which will be stored at the variable location.

Data Type Size in byte (64 bit system )
char 1
int 4
float 4
double 8
  • char: It can hold a character value.
  • int: It can hold an integer value.
  • float: It can hold a flot value.
  • double: It can hold a float value upto 15 ponts of precision.

Variable Naming Convention in C

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

Variable Declaration in C

Variable Declaration in C is the process of reserving computer memory block/location, where in the future we will store some value. To declare a variable we define the data type and variable name. Always remember that declaration of variables must be done before they are used in the program.

  • Declaration of variable is used to tell the compiler, what the variable name is.
  • It specifies that what type of data variable can store.
  • Compiler reserve the space when we declare the variable according to data type and size.
  • We can declare more than one variable together which have same data types.

Examples

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, either on the time of declaration or later, see below example.

int a, c=10;
a=7;

Note: The value present in variable can be changed at any time in the entire program

What is the output?

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at "advertise@gdatamart.com"

© 2021 GDATAMART.COM (All Rights Reserved)