0% found this document useful (0 votes)
22 views

2 C Variables, Constants and Literals

Uploaded by

goyalkeshav119
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

2 C Variables, Constants and Literals

Uploaded by

goyalkeshav119
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Programming

Fundamentals
(CO 101):
C Variables, Constants
and Literals

- Lakshita Agarwal
Department of Information Technology.
Mail ID- [email protected]
Variables
• In programming, a variable is a container (storage area)
to hold data.
• To indicate the storage area, each variable should be
given a unique name (identifier).
• Variable names are just the symbolic representation of a
memory location.
For example: int playerScore = 95;
• Here, playerScore is a variable of int type. Here, the
variable is assigned an integer value 95.
• The value of a variable can be changed, hence the name
Rules for naming a
variables
• A variable name can only have letters (both
uppercase and lowercase letters), digits and
underscore.
• The first letter of a variable should be either a letter
or an underscore.
• There is no rule on how long a variable name
(identifier) can be.
• However, you may run into problems in some
compilers if the variable name is longer than 31
Rules for naming a
variables
• C is a strongly typed language. This means that the
variable type cannot be changed once it is declared.
• Here, the type of number variable is int.
• You cannot assign a floating-point (decimal) value 5.5
to this variable.
• Also, you cannot redefine the data type of the
variable to double.
• To store the decimal values in C, you need to declare
its type to either double or float.
Literals

• Literals are data used for representing fixed values.


• They can be used directly in the code.
For example: 1, 2.5, 'c' etc.
• Here, 1, 2.5 and 'c' are literals.
• Why? You cannot assign different values to these
terms.
Literals: Integers

• An integer is a numeric literal(associated with


numbers) without any fractional or exponential
part.
• There are three types of integer literals in C
programming:
• decimal (base 10)
• octal (base 8)
• hexadecimal (base 16)
Literals: Floating-point
Literals

• A floating-point literal is a numeric literal that has


either a fractional form or an exponent form.
For example: -2.0
0.0000234
-0.22E-5
Literals: Characters

• A character literal is created by enclosing a single

character inside single quotation marks.

For example: 'a', 'm', 'F', '2', '}' etc.


Literals: Escape
Sequences

• Sometimes, it is necessary to use characters that

cannot be typed or has special meaning in C

programming.

For example: newline(enter), tab, question

mark etc.

• In order to use these characters, escape sequences


Literals: Escape
Sequences
Literals: String Literals
• A string literal is a sequence of characters enclosed in

double-quote marks.

For example: "good" //string constant

““ //null string constant

" " //string constant of six

white space

"x" //string constant having a

single character.
Constants

• If you want to define a variable whose value cannot


be changed, you can use the const keyword.
• This will create a constant.
For example: const double PI = 3.14;
• You can also define a constant using
the #define preprocessor directive.
Format Specifiers for I/O

• %d for int
• %f for float
• %lf for double
• %c for char
THE END

You might also like