Académique Documents
Professionnel Documents
Culture Documents
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
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.
Remarks
Valid
Valid Invalid Invalid valid valid
7
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
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
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
13
whole numbers. It takes two bytes in memory. It can store integer values from 0 to 65,535.
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
16
17
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
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.
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.
#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