variables & constants
TRANSCRIPT
-
7/30/2019 Variables & Constants
1/28
1
VARIABLES &
CONSTANTS
Programming With C++
-
7/30/2019 Variables & Constants
2/28
2
Rules For Writing Variables: First character of a variable name may be Letters or
underscores. i
CSE_5a
a_very_long_name_that_isnt_very_useful
fahrenheit
First character of a variable cannot be a digit.
5a_CSE is not valid!
Case sensitive
CSE_5a is different from cse_5a
-
7/30/2019 Variables & Constants
3/28
3
VARIABLES: A quantity whose value may change during
execution of the program is called variable.
A named area in the computer memory, intended to
contain values of a certain kind.
They contain the data your program works with.
They can be used to store data to be used
elsewhere in the program.
In short they are the only way to manipulate data.
-
7/30/2019 Variables & Constants
4/28
4
Variables in memory:
5
int my_int = 5;
double my_double = 3.5;
3.5
my_int
my_double
-
7/30/2019 Variables & Constants
5/28
5
Variables in memory:
5
Whenever we write the variable name (my_int), we ask to
read the value of that variable.
3.5
my_int
my_double
-
7/30/2019 Variables & Constants
6/28
6
Rules For Writing Variables:
Blank Spaces are not allowed in a variablename.
Special characters, such as arithmeticoperators,#,^ cannot be used in a variablename.
Reserved C words cannot be used as variablenames.
A variable name declared for one data type,cannot be used to declare another data type.
-
7/30/2019 Variables & Constants
7/28
7
Valid and invalid variable
names:
Variable name Valid/ Invalid Remarks
Ahmed Valid
perform Valid
double Invalid C reserve word
switch Invalid C reserve word
_large valid
marriam valid
-
7/30/2019 Variables & Constants
8/28
8
Valid and invalid variable
names:
int invalid C reserve word
FLOAT Valid Keyword float is
in lower case
3taq invalid Starts withnumeric value
unsigned invalid C reserve word
x-y invalidSpecial characters
not allowed
Taq ahd invalid Space is not allowed
char invalid C reserve word
-
7/30/2019 Variables & Constants
9/28
9
Data types in C
Classification of a particular typeof information.
The actual values used in a program are called
Data, there are four basic types of data mostly
used in C.
1. int Integer
2. float Real values
3. double Large real values
4. char Characters
-
7/30/2019 Variables & Constants
10/28
10
Data types in C
Char: a single byte character.
int : an integer number usually 2bytes.
float : a single precision real number usually4 bytes.
double: a double precision real numberusually 8 bytes.
char
int
float
double
-
7/30/2019 Variables & Constants
11/28
11
Various data types in C:
Type Size in bytes
int 16bits (2bytes)
long int 32bits (4bytes)
unsigned int 16bits (2 bytes)
unsigned long int 32bits (4bytes)
float 32bits (4bytes)
long float 64bits (8bytes)double 64bits (8bytes)
long double 80bits (10bytes)
char 8bits (1byte)
-
7/30/2019 Variables & Constants
12/28
12
Integer Type Data:
An integer is a whole number, i.e a number without
a fraction or decimal point. It may have a positive
or a negative value. For example 601, 250, -6 and
501 are integers. Depending upon the maximumand minimum values, there are four types of
integer type data in C. these are
1. int
2. long int3. unsigned int
4. unsigned long int
-
7/30/2019 Variables & Constants
13/28
13
The int: the range ofint data type depends uponthe computer system being used and it takes 2 to 4bytes.
In MS DOS, an integer type variable takes two bytesin the memory and the range of values stored is from
-32768 to 32767.
The maximum and minimum values for integer data
types are specified in integer.h header file.
-
7/30/2019 Variables & Constants
14/28
14
The long int:A long int type data takes fourbytes in memory. It can store integer values from
-2147483648 to 2147483648.
The unsigned int :Itcan store positive wholenumbers. It takes two bytes in memory. It can store
integer values from 0 to 65,535.
The long unsigned int :long unsigned inttype data takes 4 bytes in memory. It can store
integer values from 0 to 4294967295.
-
7/30/2019 Variables & Constants
15/28
15
Real Type Data:
Real numbers consist of an integer and a non
integer part. These are also called floating pointnumbers.
Data consisting of real numbers is called float typedata.
Depending upon the maximum and minimum
values, there are two types of real type data.
These are:1. float
2. double
-
7/30/2019 Variables & Constants
16/28
16
float type data:The float type data isrepresented in decimal or exponential form. It may
be signed or unsigned. For example 23.26, 16.21,
0.56, -9.87 are floating type data. The float type data takes 4 bytes in the memory and
it can store real values from 3.4*10^-38 to3.4*10^+38.
Values for float data types are specified in float.hheader file.
-
7/30/2019 Variables & Constants
17/28
17
double type data:The double type data is also areal type data. It is used to store large real values.
A double type variable takes 8 bytes in the memoryand it can store real values from 1.7*10^-308 to1.7*10^+308.
long double type data:are used to store verylarge real data values. A long double variable takes
10 bytes in the memory, and can store real valuesfrom 3.4*10^-4932 to 1.1*10^+4932.
-
7/30/2019 Variables & Constants
18/28
18
Character type data:
Character type data consists of alphabetic
characters, numeric digits and special characters.
A single character takes 8 bits or 1 byte in the
memory.
The values for character data types are specified in
integer.h header file.
Arithmetic operations can also be performed on char
type variables.
-
7/30/2019 Variables & Constants
19/28
19
Declaring variables in C:
Specifying the name & data type of avariable is called Declaration of the variable.
When a variable is declared, its value isundefined.
The syntax to declare a variable in C is:
type list of variables;
Example:double cm, inches;
-
7/30/2019 Variables & Constants
20/28
20
Type:specifies data type of variables. Forexample int is used to declare an integer typevariable and float is used to declare float type data.
List of variables: specifies a list of variablesseparated by commas. In a single statement more
than one variables of same data type can be
declared.
-
7/30/2019 Variables & Constants
21/28
21
Example: variable declarations
int i;
char c;
float f1, f2, f3;
float height;
double distance;
int xyz, a, b, c;
-
7/30/2019 Variables & Constants
22/28
22
Initialization of variables:
Assigning a known value to a variable at the
time of its declaration is called initializing of
the variable. For example to declare variables a, b, c of
integer type and assigning values a=10 and
b=60 the statements are written as:
int a=10, b=60, c;
float num = 67.3;
-
7/30/2019 Variables & Constants
23/28
23
Constants:
A quantity that cannot change its value
during execution of the program is called
constant.
There are two types of constants in C.
these are:
1. Literal Constant
2. Symbolic Constant
-
7/30/2019 Variables & Constants
24/28
24
Literal Constant:
A literal Constant is a value that is typed
directly in a program. It appears in the
program wherever it is needed.
There are four types of Literal Constants
are as follows:
1. Integer constants
2. Floating point constants3. Character constants
4. String constants
-
7/30/2019 Variables & Constants
25/28
25
Symbolic Constant: A symbolic constant is an identifier that represent a constant
value.
For example the value of(pi) is frequently used in engineeringcalculations. It represents a constant value approximately equal to
3.141593. instead of typing the numeric value each time, this valueis assigned to an identifier and that identifier is written wheneverthe value of(pi) is to be used. This identifier represents a constantvalue and is called Symbolic Constant.
There are two ways to declare symbolic constant. These are:
1. using the define directive
2. using the const qualifier
-
7/30/2019 Variables & Constants
26/28
26
define Directive:It is a preprocessor directive. It is used to assign a
constantquantity to an identifier. This directive canbe used anywhere in the program. its syntax is:
#define identifier constant
Example:#define Pi 3.141593f
-
7/30/2019 Variables & Constants
27/28
27
Identifier:specifiesthe identifiername towhich the constant
value is to be assigned.
Constant:specifies theconstant value that isto be assigned to the
identifier.
#define statement doesnot end with semicolon.
The identifier specified
in the define directive isnot variable & It doesnot have any data type.
It is a convention in Cto write identifiers in
capital letters, this isdone to distinguishthem from variables.
-
7/30/2019 Variables & Constants
28/28
28
const Qualifier: The const qualifier is used to define a variable with a
value that is fixed.
The value is assigned to the variable at the time of itsdeclaration.
In the following statement, pi has been declared as
floating point variable and a fixed value has been
assigned to it.const float pi = 3.141593f;