AOP Unit 2 Chapter 1
AOP Unit 2 Chapter 1
PROGRAMMING
Dr. Salini Suresh
Associate Professor ,DSCASC
Unit 2 Chapter 1 : C Programming
Getting Started, Variables and Arithmetic expressions.
Input and Output: Standard input and output, formatted output- printf,
variable length argument list, formatted inputscanf.
The Elements of C :
• Character set
• C Tokens
• Character set : The character set denotes any alphabet, digit or special symbol used to
represent information in any language. In C the character set is as follows:-
• Digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• C tokens are the basic buildings blocks in C language which are constructed together to
write a C program.
• Each and every smallest individual units in a C program are known as C tokens.
• C tokens are of six types. They are,
• Keywords (eg: int, while),
• Identifiers (eg: main, total),
• Constants (eg: 10, 20),
• Strings (eg: “total”, “hello”),
• Special symbols (eg: (), {}),
• Operators (eg: +, /,-,*)
C TOKENS
Sample Program :
Eg for identifier: Area, area, AREA are three different identifiers. Ex: _time, num1, num_2
Invalid identifier : 4num (digit should not be first char) , int (keyword), $dollar ($spl symbol)
num 1 (space not allowed)
• Keywords –
• They are also called ‘Reserved words’. These are the predefined words with special meanings which cannot be
changed.
• The keywords cannot be used as variable names.
• There are 32 keywords available in C language.
They are :-
auto default float register static volatile
break double for return switch while
case do goto short typedef
char else if signed union
const enum int sizeof unsigned
Continue extern long struct void
•
• Constants: does not change during the execution of a program.
It is the location in memory, referenced by an identifier that contains a data value that cannot be
changed.
• supports several types of constants. They are
• Numeric constants
• Integer constants Ex: 426, +782, -9000, -675
• Float constants(real) Ex: 426.0, 4.1, 4.1e8, 3.2e-5
• Character constants
• Single character constants Ex: ch = ‘A’ ; (This will assign character A to variable ‘ch’)
• String constants Ex:“The best way to learn programs is through practice”
• VARIABLES
Each variable represents the name of a memory location in which a value can be stored. A
variable value can be changed during program execution. Example,
monthlyhra
pop_e_89
si_int
si
• Rules to name the variables:
• Same as identifier
VARIABLES
• Eg. : If we have two variables X and Y & we store the values 120 and 200 in these variables &
• It is stored at memory location 65222 and 65224 respectively.
Data types
• Data types are used to inform the type of value that can be stored in a variable.
• Before using the variables, each variable must be declared to inform the types of data it can
hold in the beginning of the program.
• Syntax:
Types :
int
Intergers are whole numbers without a decimal point or any other character.
They can be preceeded by either ‘+’ or ‘-’
Example:
1828,
-3452
char choice=‘y’;
= 2 8 - 1 bits
= 255
= -128 to 127
Ex : float a = 23.76;
float b=-32.9931;
double means that the variable is a double precision floating point number.
Used to hold large real number
occupies 8 bytes of memory spaces (64 bits) with 14 digit precision
-308
The minimum and maximum value that can be
+308
stored in a double is 1.7 x 10 to 1.7 x 10
Quantifiers or modifiers are keywords used to alter the basic datatype on size and sign
Signed and unsigned are used to specify whether the variables can store both positive and negative numbers or
only positive numbers
• Declaration of variable informs the compiler to reserve enough space for the variable
in the memory.
• Datatype specifies what type of value the variable can hold and the number of bytes
needs to be reserved for the variable.
• Syntax
datatype variablename;
Example int a;
int c,d,e;
float temp;
Operators
Expressions
&
Type conversion
Operator
• An operator is a symbol used to indicate a specific
operation on variables in a program.
• Example :
A + B
operator
operand
Operators
• C language is very rich in operators.
operators precedence
int x; output:
x= ( 7 + 3) * 5; x = 50
int x; output:
x= 7 / 3 * 5; x = 10
Modulus operator ( % )
• This operator has same priority as that of multiplication and division
• Example:
7 % 10 = 7
7%1 = 0
7%2 = 1
7%7 =0
Exercise
int y;
y = 10 % 4 * 3;
Output:
6
int y; Output::
y = 3 * 10 % 4; 2
Important
• Modulus operator ( % ) : It produces remainder of an integer
division. This operator cannot be used with floating point
numbers.
int main( ){
float f_1=3.2, f_2=1.1, f_3 ;
f_3 = f_1 % f_2;
printf(“ %f”, f_3);
return 0; }
• Example:
- 57 - 2.933 -x
-( a * b) 8 * ( - ( a+b))
Increment operator ( ++ )
• The increment ( ++ ) operator adds 1 to its operand.
n = n +1 ; => ++ n ;
Sum = x; x=x+1;
x=x+1; Sum=x;
b = 10
a = 11
Here first value of a(i.e., 10) is
assigned to b and then value of a is
incremented. So b = 10 and a = 11 is
printed
a = 10
a = 11
Here in the first printf statement
a value gets printed after that its
value gets incremented, which is
shown in second printf statement
Decrement operator
• The decrement ( - - ) operator subtracts 1 from its operand.
j=j-1; => -- j;
sum = x; x=x-1;
x=x-1; sum=x;
Precedence of Arithmetic operators
Highest : ++ --
- (unary minus)
* / %
Lowest + -
0 && 0
False ( 0 )
Example 2
1 && 1
True ( 1 )
Logical OR
• The result of a logical or operation will be true if either operand is
true or if both operands are true.
• The Logical NOT ( ! ) is a unary operator. It negates the value of the logical expression or
operand.
• If value of X = 0 !X=?
!X=1
• ! ( 5 > 3) = ??
-> 0 -> false
True
( x = = 10 ) && ( y > 20 )
True
( x==10) || ( y < 20)
True
( x ==10) &&( ! ( y < 20) )
Precedence & Associativity highest
!(logical NOT) ++ -- sizeof( ) Right to left P
* (multiplication) / ( division) %( Left to right R
modulus) E
C
+ - (binary ) Left to right
E
< <= > >= Left to right D
==(equal to) !=( Not equal to ) Left to right E
&&(AND) Left to right N
C
|| (OR) Left to right E
? : (conditional ) Right to left
= += - = * = /= %= Right to left
, Left to right lowest
• Suppose that
j = 7, an integer variable
f = 5.5, a float variable
c = ‘e’
Interpret the value of the following expressions:
j + f <= 10 0
j >= 6 && c = = ‘w’ 1
f < 11 && j > 100 0
!0 && 0 | | 0 0
!(0 && 0) | | 0 1
Assignment operator
Ex: a=20;
b=(2+5)*3;
Two cases of assignment
• Multiple assignment:
int j=k=m=0;
▪ Compound assignment:
j= j+10; this expression can be written as
j + = 10;
similarly
m= m-100; is equivalent to m - = 100;
Conditional Operators (?:)
Ex : (total>=35?printf( “PASS”):printf(“FAIL”));
x -> 00000101
y-> 00000011
X&y-> 00000001
• If x=5 and y=3 then bitwise OR(|) operation x|y is shown below
x -> 00000101
y-> 00000011
X|y-> 00000111
• If x=5 and y=3 then bitwise Exclusive OR (XOR ^) operation x ^ y is
shown below
x -> 00000101
y-> 00000011
x^y-> 00000110
• Bitwise compliment (~)
• If x=5 then bitwise compliment (~) operation produces one’s
compliment of that number is shown below
x -> 00000101
~x-> 11111010
• Right shift operator (>>)
• Moves the bits to the right, it discards the far right bit and assign 0 to the
leftmost bit
x>>n;
a=90;
the binary representation of a is
a -> 01011010
a=a>>3;
a-> 00001011;
• Left shift operator (>>)
• Moves the bits to the left, it discards the far left bit and assign 0 to the
rightmost bit
x<<n;
a=10;
the binary representation of a is
a -> 000010101
a=a<<3;
a-> 01010000;
Type conversion
• Used to convert a value of one datatype to another
• Two types:
• Implicit type conversion
• Explicit type conversion
• Implicit type conversion:
• Known as automatic type conversion
• Type conversion which are automatically carried out by
computer itself is known as implicit type conversion
• Example:
int a;
float b=5.0;
a=b/2; now a=2 of type int
Type conversion
• Implicit type conversion
• When the variable or constant of lower type is converted to
higher type is called promotion.
int i=5;
float f;
f=i; // i is lower type and f is higher type and value of f=5.0
scanf()
• it is a formatted input function to read input through standard input device
• stores them at the address of the variable in the memory.
• It is a buffered function
i.e. data is supplied to the program only when enter key is pressed.
scanf()
Format specifiers – Format specifiers always starts with the percentage[%] sign
followed by a character to represent different data types. The list of format specifiers are:-
• %d - int
• %f - float
• %c - single character
• %s - string
• %lf - double
• %u - unsigned integer
Output function
printf()
Formatted output function- This is a function which transfers the data in various formats
to the output device (monitor).
2. Format specifier like %d, %c, %f etc along with optional field width
3. Escape sequences.
Output functions
printf()
Escape sequences - it begins with back slash(\) followed by one or more character.
\a System Alarm
\n New line
\t Horizontal Tab
\” Double Quotes
\’ Single quotes
\0 Null (End Of String)
\\ Back Slash Character Itself.
Output function
printf()
Example:
Syntax:
printf()
printf(“%5d”, a); 2 5 4
printf(“%-5d”, a); 2 5 4
printf(“%2d”, a); 2 5 4
printf(“%-7.2f”, a); 8 6 . 1 2
printf(“%7.3f”, a); 8 6 . 1 2 3
Output function
printf()
printf(“%s”, a); C O M P U T E R S C I E N C E
Let a=‘A’ printf(“%4c”, a);
A
printf(“%-3c”, a);
A
let a= “COMPUTER”
printf(“%s”, a); C O M P U T E R
printf(“%5s”, a); C O M P U T E Rwhen space is less, it will ignore width
printf(“%12s”, a); C O M P U T E R
printf(“%12.6s”, a); C O M P U T
printf(“%-12s”, a); C O M P U T E R
Unformatted Input function
getchar()
• This is a function which accepts only a single character from the keyboard and
assigns that to the variable.
• It is a buffered function i.e. data is supplied to the program only when enter key is
pressed.
Ex: getchar()
Syntax: variable=getchar();
Ex.1: int ch; Ex.2: char ch;
ch=getchar(); ch=getchar();
In Ex:1 The function getchar() accepts the ASCII value of the character pressed i.e. if A is
pressed, the value 65 is assigned to the variable ch as it is declared as int
getch()
C provides single character input functions getch() declared in “conio.h” header file
i.e. this function supplies the data to the program immediately without waiting for the enter
key to be pressed.
Syntax : getch()
Example: char val;
val = getch();
Unformatted Input function
getche()
getche() function is also a unformatted input function and declared in “conio.h” header file.
It reads a single character from the keyboard and returns it immediately without even waiting for
enter key.
Syntax : getche()
Example: char val;
val = getche();
Unformatted output function
2. putch(): The putch() function displays single character through the standard output device like
monitor..
• Syntax : putchar(variable_name);
putch(variable_name);
putchar(choice);
putchar(‘A’);
putch(choice);
putch(‘A’);
Variable length arguments
Variable length arguments is a programming construct that allows programmers to
pass n number of arguments to a function.
also known as variable length argument as var-args.
< stdarg.h> -> header file defines the libraries to handle routines with
variable numbers of arguments
va_list arguments; // declares a list that can be used to store a variable number of arguments.
double sum = 0.0;
int i;
return sum/num;
}
int main() {
printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}