You are on page 1of 16

VARIABLES,

LITERALS AND
CONSTANTS IN
C++
What is C++ 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).
EXAMPLE:
int age;
int a; int sum;
int age;
DATA Variable
TYPE
age=18;
name

int sum;
sum=12+5;
EXAMPLE:
int age, id_number, school_name;
int sum, a=9, b=10, c=56;
int a; sum=a+b;

float =

DATA Variable
TYPE name
RULES OF NAMING A VARIABLE

• A variable name can only have alphabets,


numbers, and the underscore “_”.
• A variable name cannot begin with a number.
• Variable names cannot begin with an uppercase
character.
• A variable name cannot be a keyword. For
example, int is a keyword that is used to denote
integers.
• A variable name can start with an underscore.
However, it’s not a good practice.
REMEMBER: YOU SHOULD TRY TO GIVE
MEANINGFUL NAMES TO VARIABLES:

• first_name is better than fn.


• age is better than a.
What is C++ Literals?

are used for representing fixed values.


• Integers
• Floating-point
C++ Literals Literals
• Characters
• Escape Sequences
• String Literals
INTEGERS

• Can only accept decimal (base 10), octal (base


8), hexadecimal (16).

• Example:
• DECIMAL:
5, 87, 34, 0
• OCTAL:
232, 987, 078
FLOATING-POINT LITERALS

• Fractional Form or an exponent form.

• Example:
-2.0
0.00034204
0.3475
CHARACTERS

• created by enclosing a single character inside a


single quotation marks.

• EXAMPLE:
• ‘a’, ‘F’, ‘y’, ‘2’
CHARACTERS

• created by enclosing a single character inside a


single quotation marks.

• EXAMPLE:
• ‘a’, ‘F’, ‘y’, ‘2’
STRING LITERALS
• Sequence of characters enclosed in double-
quote marks.

• EXAMPLE:
• “good thing” - string constant
- null string constant
• “” - string constant of six white space
• ““ - string constant having a single
• “x” character
- prints string with a newline
• “Earth is round\n” 
ESCAPE SEQUENCE LITERALS
• Use characters that cannot be typed or has
special meaning.

• EXAMPLE:
What is C++ Constants?

Variables whose value cannot be changed.

const int NUM1 = 34;


NUM1 = 18 // ERROR! NUM1 is constant.
String name = “Student”
Int age = 17
Float grade = 1.00
Char grade_level = ‘8’

You might also like