2. Revision
•It is possible to utilize an uppercase keyword as an identifier.
•Find out whether following are valid identifier. If not why?
◦ 12,245
◦ 36.0
◦ 10 20 30
◦ 123-45-6789
◦ 0900
PROGRAMMING IN C
4. Average of Three Numbers
step 1: Get the input of three real numbers and store in n1,n2, n3
step 2: Calculate Total <- n1 + n2 + n3
step 3: Calculate average <- Total / 3
step 4: Print Total and average
PROGRAMMING IN C
7. Input operations
• Input operations are used to read user input from the keyboard
• scanf() function is used to read multiple data values of different data types from the keyboard
Syntax:
scanf(“control string”, arg1, arg2, ………. argn);
Control string (format string) specifies the field format
Arguments arg1, arg2, …, argn specify the address of locations where
the data is stored
Inputting integer numbers:
%d
Percentage sign (%) indicates conversion specification, a data type character ( or type specifier)
Example:
scanf(“%d %d”, &number1, &number2);
PROGRAMMING IN C
8. Input operations(cont…)
Inputting real numbers: %f
Example:
scanf(“%f %f”, &number1, &number2);
This statement will read the data: 50.35 23.0345
Inputting character strings: %s or %c
Example:
scanf(“ %s ”, name);
This statement will read the data: SASTRA
PROGRAMMING IN C
10. Home Work
1)Area of Rectangle
2)Area of Triangle
3)Farenheit to celcius conversion
4)Celcius to Farenheit conversion
PROGRAMMING IN C
11. Array
An array is an identifier that refers to a collection of data items that all have the same name.
The data items must all be of the same type
The individual array elements are distinguished from one another by the value that is assigned
to a subscript.
Example
x is a 10-element array.
◦ The first element is referred to as x [ 0J ,the second as x [ 1J ,and so on.
◦ The last element will be x [9J .
If the array contains n elements, the subscript will be an integer quantity whose values range
from 0 to n-1 .
PROGRAMMING IN C
12. Array (Cont….)
n-character string will require an (n+l )-element array, because of the null character (O) that is
automatically placed at the end of the string.
PROGRAMMING IN C
13. Declarations
A declaration associates a group of variables with a specific data type.
All variables must be declared before they can appear in executable statements
It consists of a data type, followed by one or more variable names, ending with a semicolon
PROGRAMMING IN C
15. EXPRESSIONS
An expression represents a single data item, such as a number or a character.
It may consist of a single entity, such as a constant, a variable, an array element or a reference
to a function.
It may also consist of some combination of such entities, interconnected by one or more
operators.
It can also represent logical conditions that are either true or false
true and false are represented by the integer values 1 and 0, respectively.
Examples are shown there
PROGRAMMING IN C
16. STATEMENTS
A statement causes the computer to carry out some action.
Three different classes
◦ expression statements
◦ compound statements and
◦ control statements
Expression Statement
o consists of an expression followed by a semicolon.
o Several expression statements are shown below.
o a = 3;
o c = a + b ;
o ++i;
o p r i n t f ("Area = %f area) ;
PROGRAMMING IN C
17. STATEMENTS(cont..)
Compound statement
◦ A compound statement consists of several individual statements enclosed within a pair of braces { }.
◦ a compound statement does not end with a semicolon
◦ A typical compound statement is shown below.
{
p i = 3.141593;
circumference = 2. * p i * radius;
area = p i * radius * radius;
}
Control Statement
Control statements are used to create special program features, such as logical tests, loops and branches.
PROGRAMMING IN C
19. Symbolic Constants
A symbolic constant is a name that substitutes for a sequence of characters.
The characters may represent a numeric constant, a character constant or a string constant.
Thus, a symbolic constant allows a name to appear in place of those constant
When a program is compiled, each occurrence of a symbolic constant is replaced by its
corresponding character sequence.
It usually defined at the beginning of a program
typically written in uppercase letters
does not end with a semicolon
PROGRAMMING IN C
20. Symbolic Constants(contd…)
Examples
#define TAXRATE 0.23
#define P I 3.141593
#define TRUE 1
#define FALSE 0
#define FRIEND "Susan“
Scenario 1
area = PI * radius * radius; statement will become area = 3.141593 * radius * radius;
PROGRAMMING IN C
21. Symbolic Constants(contd…)
Scenario2
#define CONSTANT 6.023E23
……
….
p r i n t f ( "CONSTANT = %f “,CONSTANT) ;
......
...... p r i n t f statement would become p r i n t f ( "CONSTANT = %f 6.023E23) ; ,
during the compilation process.
Advantage
readily identified than the information that they represent
easier to change the value of a single symbolic constant than to change every
occurrence of some numerical constant that may appear in several places within the
program.
PROGRAMMING IN C