Variables in C: Simple Explanation With Code Examples

What is a Variable?

In any programming language variables are containers. It is used to store values or data. Values or data can be in the form of numbers or characters.

Variables are also called identifiers. It identifies memory locations for different values. We call it variables because it’s values changes as per the programming requirements. Variables are just a name that holds any random variables.

 

How to Declare Variables in C?

Syntax for Declaring Variables in C

1.

datatype variableName;

int Roll;

or

2.

datatype variablename = values;

int Roll = 10;

Here int is datatype, Roll is variable name, = is assignment operator, 10 is value, and ; this semicolon in C is called terminator (Every statement in C language must be end with semicolon, or semicolon indicates compiler that this line of code is completed and new line of code will begin after that. Therefore we can write multiple statements in single line after putting semicolon).

int Roll = 10; We can read this line of code as Roll is a variable of int type and value 10 has been assigned in variable name Roll.

We will learn about data types in the below sections of this blog post.

 

Naming Conventions of Variables in C

What are naming conventions in C? There are certain rules we need to follow while naming a variable. There are some legal ways of declaring variables that is syntactically correct and acceptable to C compilers.

 

Rules for declaring variables is mentioned below:

  1. Variable names must begin with a letter or an underscore(_)
  2. Names can contain combination of letters, digits, and underscores.
  3. Variable names are case- sensitive. (ROLL and roll are different variables)
  4. Names can not contain whitespace or special symbols(student name, #name, @name these are invalid declarations)
  5. Variables name must not start with numbers(1Name, 2Name : Such variable declarations are not allowed). But we can use numbers in variable name between characters or in last(name1, name2: such variable declarations are allowed).
  6. Reserved keywords can not be used as variable names

 

What are the data types in C?

First, let me define what is data?

Data is the smallest unit of any information. Therefore we collect data to bring meaningful information.

Data can be collected in many forms. Broadly we divide into two kinds: 1. Numeric 2. Characters.

In C, There are four types of data:

  1. int
  2. float
  3. char
  4. double

int is numeric data type, if we have to store Integer values without fraction or decimal points then we use int data type.

Syntax for declaring int datatype

int Roll = 10;

float data type: If we have to store values in decimal form then we use float data type.

Syntax for declaring float datatype:

float weight = 55.4;

char data type: If we have to store a character then we use char data type. 

Syntax:

char gender = ‘M’;

In char data type we always store single character. and single character should be inclosed in single quote.

To store multiple characters in C, there is no specific data type in C but we can make a char variable as array.

Syntax for storing multiple characters:

char myname[] =”Analytics Siksha”;

Multiple characters also called Strings must be enclosed in double quote and while we print must use %s as access specifier/modifier.

double data type: The size of float is only 4 byte. But if you have to store large decimal numbers then must use double data type.

Leave a Comment

Your email address will not be published. Required fields are marked *