Pps Unit-1 Lesson Variables and Constants (1)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

VARIABLES AND

CONSTANTS
IN C
Variable:

 It is a data name that may be used to store a data


value.

 A variable may take different values at different


times during execution.

 A variable name can be chosen by the


programmer in a meaningful way so as to reflect
its function or nature in the program
Rules for naming Variables:

 Variable names may consist of letters, digits and under


score( _ ) character.
 First char must be an alphabet or an ‘_’.
 Length of the variable cannot exceed upto 8
characters.(few upto 31 chars)
 White space is not allowed.
 Variables name should not be a keyword.
 Uppercase and lower case are significant.
Examples:-

Valid :

Mark,Sum1,tot_value,Delhi
Invalid :

Prics$, group one, char


Declaration of variables with examples:
 Declaration does two things:
 It tells the compiler what the variable name is.
 It specifies what type of data the variable will hold.

Note :The declaration of variables must be done before they are


used in the c program.
 The syntax for declaring a variable is as follows:
data-type v1,v2,…….,vn;
For example :
int count;
int number, total;
Initialization of variable :

 Initialize a variable in c is to assign it a starting value.

 C does not initialize variables automatically.

 So if you do not initialize them properly, you can get


unexpected results.

 Fortunately, C makes it easy to initialize variables when


you declare them.
For Example :
int x=45;
CONSTANTS IN C
 C Constants is the most fundamental and essential part of the C
programming language.
 Constants in C are the fixed values that are used in a program.

 Its value remains the same during the entire execution of the program.
 Constants are also called literals.
 Constants can be any of the data types.
 It is considered best practice to define constants using only upper-
case names.
FIG :TYPES OF CONSTANTS

Types of c constants:
Types of c constants:
1.Integer constants
2.Real constants
3.Character constants
4.String constants
Integer constants:

An integer constant refers to a sequence of digits.


There are three types of integers, namely, decimal integer,
octal integer and hexadecimal integer.
Examples of Integer Constants:
426 ,+786 , -34(decimal integers)
037, 0345, 0661(octal integers)
0X2, 0X9F, 0X (hexadecimal integers)
Real constants:

These quantities are represented by numbers


containing fractional parts like 18.234.
Such numbers are called real (or floating point)
constants.

Examples of Real Constants:


+325.34
426.0
-32.67 etc.

Single Character constants:

Single character constant contains a single character


enclosed within a pair of single quote marks.
For ex. 'A', ‘5’ , ‘?’ , ‘ ‘ , ‘*’
Backslash Character Constants:

C supports some special backslash character constants that are used in


output functions.
Some of the back slash character constants are as follows:
String constants:

String constant contains a group of characters


enclosed within a pair of double quote marks.
For ex :
“hello”
“XYZ”
“1234”
“@#$%”
Using const keyword for constants:

const is a type qualifier in c.


A keyword applied to a data type that indicates that the data is read
only.

The qualifier const can be applied to the declaration of any variable to


specify that its value will not be changed

Syntax :
Const datatype nameofconstant = value;

Eg :const int x=10;


THANK YOU

You might also like