Vous êtes sur la page 1sur 28

VARIABLES & CONSTANTS

Programming With C++

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! CSE_5a is different from cse_5a

Case sensitive

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.

Variables in memory:
int my_int = 5; double my_double = 3.5;

my_int

my_double

3.5

Variables in memory:

Whenever we write the variable name (my_int), we ask to read the value of that variable.

my_int

my_double

3.5

Rules For Writing Variables:


Blank Spaces are not allowed in a variable name. Special characters, such as arithmetic operators,#,^ cannot be used in a variable name. Reserved C words cannot be used as variable names. A variable name declared for one data type, cannot be used to declare another data type.

Valid and invalid variable names:


Variable name Valid/ Invalid Ahmed
perform double switch _large marriam

Remarks

Valid
Valid Invalid Invalid valid valid
7

C reserve word C reserve word

Valid and invalid variable names:


int FLOAT invalid Valid invalid C reserve word Keyword float is in lower case Starts with numeric value

3taq

unsigned x-y
Taq ahd char

invalid invalid
invalid invalid

C reserve word
Special characters not allowed
Space is not allowed

C reserve word
8

Data types in C

1.
2. 3. 4.

Classification of a particular type of information. The actual values used in a program are called Data, there are four basic types of data mostly used in C. int Integer float Real values double Large real values char Characters

Data types in C

Char: a single byte character. int : an integer number usually 2bytes. float : a single precision real number usually 4 bytes. double : a double precision real number usually 8 bytes.
char
int

float
double
10

Various data types in C:


Type
int long int

Size in bytes
16bits (2bytes) 32bits (4bytes)

unsigned int
unsigned long int float long float double long double char

16bits (2 bytes)
32bits (4bytes) 32bits (4bytes) 64bits (8bytes) 64bits (8bytes) 80bits (10bytes) 8bits (1byte)
11

Integer Type Data:

1. 2.

3.
4.

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 maximum and minimum values, there are four types of integer type data in C. these are int long int unsigned int unsigned long int
12

The int: the range of int data type depends upon


the computer system being used and it takes 2 to 4 bytes. In MS DOS, an integer type variable takes two bytes in 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.

13

The long int : A long int type data takes four


bytes in memory. It can store integer values from -2147483648 to 2147483648.

whole numbers. It takes two bytes in memory. It can store integer values from 0 to 65,535.

The unsigned int : It can store positive

The long unsigned int : long unsigned int


type data takes 4 bytes in memory. It can store integer values from 0 to 4294967295.
14

Real Type Data:

1.
2.

Real numbers consist of an integer and a non integer part. These are also called floating point numbers. Data consisting of real numbers is called float type data. Depending upon the maximum and minimum values, there are two types of real type data. These are: float double
15

float type data: The float type data is


represented 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 to 3.4*10^+38. Values for float data types are specified in float.h header file.

16

double type data: The double type data is also a


real type data. It is used to store large real values. A double type variable takes 8 bytes in the memory and it can store real values from 1.7*10^-308 to 1.7*10^+308.

long double type data: are used to store very


large real data values. A long double variable takes 10 bytes in the memory, and can store real values from 3.4*10^-4932 to 1.1*10^+4932.

17

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.

18

Declaring variables in C:

Specifying the name & data type of a variable is called Declaration of the variable. When a variable is declared, its value is undefined. The syntax to declare a variable in C is: type list of variables; Example: double cm, inches;

19

Type: specifies data type of variables. For example int is used to declare an integer type variable and float is used to declare float type data.
List of variables: specifies a list of variables separated by commas. In a single statement more than one variables of same data type can be declared.

20

Example: variable declarations

int i; char c; float f1, f2, f3; float height; double distance; int xyz, a, b, c;

21

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;
22

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:
Literal Constant Symbolic Constant

1. 2.

23

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:
Integer constants Floating point constants Character constants String constants
24

1. 2.

3.
4.

Symbolic Constant:

A symbolic constant is an identifier that represent a constant value. For example the value of (pi) is frequently used in engineering calculations. It represents a constant value approximately equal to 3.141593. instead of typing the numeric value each time, this value is assigned to an identifier and that identifier is written whenever the value of (pi) is to be used. This identifier represents a constant value and is called Symbolic Constant. There are two ways to declare symbolic constant. These are:

1.

2.

using the define directive using the const qualifier

25

define Directive:
It is a preprocessor directive. It is used to assign a constant quantity to an identifier. This directive can be used anywhere in the program. its syntax is: #define identifier constant Example:

#define Pi 3.141593f

26

Identifier: specifies
the identifier name to which the constant value is to be assigned.

Constant: specifies the constant value that is to be assigned to the identifier.

#define statement does not end with semicolon. The identifier specified in the define directive is not variable & It does not have any data type. It is a convention in C to write identifiers in capital letters, this is done to distinguish them from variables.

27

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 its declaration. 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 ;

28

Vous aimerez peut-être aussi