We have already study Classification of data , now we will study the mechanism for storing them.

C Variables Store world inside your Computer

As a programmer, you will frequently want your program to “remember” a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later.

When we want to store any information(data) on our computer/laptop, we store it in the computer's memory space. Instead of remembering the complex address of that memory space where we have stored our data, our operating system provides us with an option to create folders, name them, so that it becomes easier for us to find it and access it.

Similarly, in C language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.

The naming of an address is known as variable. Variable is the name of memory location.

A programmer can choose a meaningful variable name. Example : average, height, age, total etc.

Q) What is identifier in below code ?

int c = 0 ;
1

Hint: Name given to variables , function etc

Defining a variable

To define a variable we must provide the datatype and the variable name.

Syntax :

datatypes variable_name ;
1

Data type determines the type of data a variable will hold. If a variable x is declared as int. it means x can hold only integer values.

int x;
1

We can even define multiple variables of same datatype in a single line by using comma to separate them.

float b, c;
1

Initializing a variable

Initializing a variable means to provide it with a value.

syntax

variable_name = value;
1

Eg:

x = 10;
1

A variable can be initialized and defined in a single statement, like:

int x = 10;
1
(adsbygoogle = window.adsbygoogle || []).push({});

Printing a variable

The following line shows how to output the value of a variable using printf.

printf ("%d " , b);
1

The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words. One way to accomplish that is like this:

printf("The temperature is");
printf("% d" ,b);
printf("degrees \n");
1
2
3

An easier way is to say this:

printf("The temperature is %d degrees \ n " , b );
1

You can also use multiple %d placeholders in one printf statement:

printf("%d + %d = %d \ n" , a , b , c );
1

You can print all of the normal C types with printf by using different placeholders:

  1. int (integer values) uses %d
  2. float (floating point values) uses %f
  3. char (single character values) uses %c
  4. character strings (arrays of characters, discussed later) use %s

TASKS


  1. Declare and initialize 3 variable of each datatypes (not of string) with random value and print each of them using multiple placeholder

Hint :

put the value of character inside 'value'

  1. Print the following output
Name   : A
Age    : 20
Income : 0.00
1
2
3

Hint :

 printf("Name   : %c \n" , name);
 printf("Income : %f \n" , income);
1
2
(adsbygoogle = window.adsbygoogle || []).push({});
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date(); (function(){ var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0]; s1.async=true; s1.src='https://embed.tawk.to/5bd7b562476c2f239ff683f0/default'; s1.charset='UTF-8'; s1.setAttribute('crossorigin','*'); s0.parentNode.insertBefore(s1,s0); })();
(adsbygoogle = window.adsbygoogle || []).push({});