0% found this document useful (0 votes)
63 views12 pages

Foundations of Programming: ENGI 3891

The document discusses variables, constants, and data types in programming. It defines a variable as a named and typed piece of storage that can be assigned values and manipulated. Constants are expressions with fixed values known at compile time. The document outlines different ways to declare and initialize variables in C++, including declaring the type before use. It also discusses special characters, typed constant expressions, and preprocessor definitions for naming constant values.

Uploaded by

vigneshwar R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views12 pages

Foundations of Programming: ENGI 3891

The document discusses variables, constants, and data types in programming. It defines a variable as a named and typed piece of storage that can be assigned values and manipulated. Constants are expressions with fixed values known at compile time. The document outlines different ways to declare and initialize variables in C++, including declaring the type before use. It also discusses special characters, typed constant expressions, and preprocessor definitions for naming constant values.

Uploaded by

vigneshwar R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

FOUNDATIONS OF PROGRAMMING

ENGI 3891

Lecture 0:
Lecture 3 :
Variables and Constants

ENGI 3891
SO FAR..
Learning Outcome.
• Abstracts and Paradigms
• Data types
• Computations and Expressions

ENGI 3891 www.mun.ca


LECTURE 3
Variables

Constants

Sample program

ENGI 3891 www.mun.ca


VARIABLES
• A variable is a logically named, typed, structured piece of storage

• Name allows us to refer to the stored structure

• Type allows us to know structure

• Variables can be assigned new values

• Program can manipulate them!!

ENGI 3891 www.mun.ca


DECLARATION OF VARIABLES
C++ is a strongly-typed language and requires every variable to be
declared with its type before its first use.
This informs the compiler the size to reserve in memory for the variable
and how to interpret its value.
The syntax to declare a new variable in C++,
Data_type Variable_Name;

Eg :

int x;

REMEMBER C++ is case sensitive.

ENGI 3891 www.mun.ca


INITIALIZATION OF VARIABLES
When the variables is declared, they have an unsettled value until they are assigned a
value for the first time. But it is possible for a variable to have a specific value from the
moment it is declared. This is called the initialization of the variable.

In C++, there are three ways to initialize variables and they are all equivalent.

The first one, known as c-like initialization

Syntax : type identifier = initial_value;

A second method, known as constructor initialization (introduced by the C++ language)

Syntax : type identifier (initial_value);

Finally, a third method, known as uniform initialization (introduced by the C++ 11),

Syntax > type identifier {initial_value};

ENGI 3891 www.mun.ca


CONSTANT AND LITERAL
Constants are expressions with a fixed value.
A literal is a fixed, explicit value that is known at compile time
–Can be used to initialize variables
–Can be used to initialize constants
–Can be used in expressions
It may be int, char, bool, floating-point.

ENGI 3891 www.mun.ca


SPECIAL CHARACTERS
•Some characters are not printable
•Some characters have special meaning to the language
•For these, we need escape sequences
– All start with backslash \

– Some predefined: \n newline

– Any by \x where x is a number

ENGI 3891 www.mun.ca


SPECIAL CHARACTERS

Can use as single character:


cout << '\n'; cout << “\tHello!\n”;

Generalized escape sequence:


\12 = \014 = x0c = newline in decimal, octal, hex

ENGI 3891 www.mun.ca


TYPED CONSTANT EXPRESSIONS
Sometimes, it is just convenient to give a name to a constant value:

Pi = 3.14159

Task : Enter r value and calculate area of the circle


Remember A = π * r * r
PREPROCESSOR DEFINITIONS (#DEFINE)
Another mechanism to name constant values is the use of preprocessor
definitions. They have the following form:
#define identifier replacement

After this directive, any occurrence of identifier in the code is interpreted as


replacement, where replacement is any sequence of characters (until the
end of the line). This replacement is performed by the preprocessor, and
happens before the program is compiled, thus causing a sort of blind
replacement: the validity of the types or syntax involved is not checked in
any way.
WHAT NEXT
Examples :

You might also like