Data Structure Variable and constants.pptx

 Data Structure


Variables and Constants


What is a Variable?


A variable is an identifier which is used to store

some value.


Constants can never change at the time of

execution. Variables can change during the

execution of a program and update the value

stored inside it.


A single variable can be used at multiple locations

in a program.


A variable name must be meaningful. It should

represent the values which are required for the

program.


Variable


A variable must be declared first before it is

used somewhere inside the program. A

variable name is formed using characters,

digits and an underscore.


Rules to create variable


Following are the rules that must be followed

while creating a variable:


A variable name should consist of only characters,

digits and an underscore.


A variable name should not begin with a number.


A variable name should not consist of whitespace.


A variable name should not consist of a keyword.


'C' is a case sensitive language that means a

variable named 'age' and 'AGE' are different.


Examples of variables


Following are the examples of valid variable names in

a 'C' program:


height or HEIGHT


_height


_height1


My_name


Following are the examples of invalid variable names in a

'C' program:


1height


Hei$ght


My name


Type declaration of a variable


int main()


{


int x, y;


float salary = 13.48;


char letter = 'K';


x = 25;


y = 34;


int z = x+y;


printf("%d \n", z);


printf("%f \n", salary);


printf("%c \n", letter);


return 0;


}


Output:


59


13.480000


K


Constants


Constants are the fixed values that never change

during the execution of a program. Following are

the various types of constants:


Integer constants


An integer constant is nothing but a value

consisting of digits or numbers. These values

never change during the execution of a program.

Integer constants can be octal, decimal and

hexadecimal.


Decimal constant contains digits from 0-9 such as,


Example, 111, 1234


Constants


Octal constant contains digits from 0-7, and these

types of constants are always preceded by 0.


Example, 012, 065Above are the valid Octal

constants.


Hexadecimal constant contains a digit from 0-9 as

well as characters from A-F. Hexadecimal

constants are always preceded by 0X.


Example, 0X2, 0XbcdAbove are the valid

hexadecimal constants.


The octal and hexadecimal integer constants are

very rarely used in programming with 'C'.


1’s complement and 2’s complement


4 (decimal) to binary “0100”


1’s complement : 1011


2’s Complememt: 1011 +1 = 1100


Character & String constants


A character constant contains only a single

character enclosed within a single quote ('').

We can also represent character constant by

providing ASCII value of it.


Example, 'A', '9‘


String constants


A string constant contains a sequence of

characters enclosed within double quotes ("").


Example, "Hello", "Programming"


Floating Point Constants/Real

Constants


Like integer constants that always contains an

integer value. 'C' also provides real constants

that contain a decimal point or a fraction

value. The real constants are also called as

floating point constants. The real constant

contains a decimal point and a fractional

value.


Example, 202.15, 300.00


Declaration of constants


For example, to declare a value that does not change like the

classic circle constant PI, there are two ways to declare this

constant


By using the const keyword in a variable declaration which will

reserve a storage memory


#include <stdio.h>


int main()


{


const double PI = 3.14;


printf("%f", PI);


//PI++; // This will generate an error as constants cannot be

changed


return 0;


}


By using the #define pre-processor directive which

doesn't use memory for storage and without putting a

semicolon character at the end of that statement.


#include <stdio.h>


#define PI 3.14


int main() {


printf("%f", PI);


return 0;


}


Thank you

Comments

Popular posts from this blog

Bihar Board 12th Result 2022 - Check BSEB Inter Result at BiharBoardonline.Bihar.gov.in

Chennai Super Kings against Mumbai Indians in Mumbai