SCS101 Lecture 5
SCS101 Lecture 5
EXPRESSIONS
Operator Description
== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to
c) Logical operators-They are used to test more than one condition and make a decision.
They always perform true or false results.
Operator Meaning
&& AND
|| OR
! NOT
Logical AND (&&) Truth table
Result
True True True
True False False
Result
True True True
True False True
NOT False=True
NOT True=False
It gives the opposite of the results
d) Assignment operators-its used to assign values to variables,a lso used to assign results of
expressions to a variable.
/= factor/=.50 factor=factor/.50
%= daynum%=7 daynum%7
__ i- - Postfix i=i-1;i-=1;
__ - -i Prefix i=i-1;i-=1;
Notice that the ++ and - - can go on either side of the modified variable.if the ++ or - - appears
on the left,its known as prefix operator.If it appears on the right,it’s a postfix operator.
NB: Whether you use prefix or postfix it doesn’t matter.
INPUT AND OUTPUT
Understanding printf()
printf() sends data to the standard output device,which is generally the screen.
Format:
printf(control_string[,one or more values])
printf() always requires a control_string.
Printing strings
printf(“This is stage 2”); is the same as
printf(“%s”,”This is stage 2”);
The %s informs the printf() function that a string follows.
NB:C does not need the %s to print strings,the %s is redundant.
You must use the %c conversion character any time you print single characters.
e.g printf(“%c %c %c”,’A’,’B’,’C’);
When you want to print a numeric constant or variable,you must include the proper conversion
character inside the printf() control string.
Example 1
#include<stdio.h>
main()
{
char first='E';
char middle='W';
char last='C';
int age=32;
int dependents;
float salary=25000.00;
float bonus=575.25;
If you need to print a table of numbers,you can use the \t tab character.
Example 2
#include<stdio.h>
main()
{
printf(“Parrots\tRams\tKings\n”);
printf(“%d\t%d\t%d\n”,3,5,2);
printf( “%d\t%d\t%d\n”,2,5,1) ;
printf( “%d\t%d\t%d\n”,2,6,4) ;
return 0 ;
}
However if you insert a number before the d in the conversion character,you can control exactly
how many positions print.
The following printf() prints the number 456 in five positions(with two leading spaces)
Printf(“%5d”,456);
If you put a minus sign before the width specifier,C left justifies the number inside the width.
The width specifier is more important when you want to print floating point numbers.
Format:
%width.decimalsf
The floating point conversion character,%6.2f tells C to print a floating point number within six
positions,including the decimal point and the fractional part.It also informs C to print two
decimal places.If C has to round the fractional part,it does so
e.g printf(%6.2f,134.568767);
produces this output:
134.57
Without the format modifier,C would print
134.568767
The scanf() function is one way to get input from the keyboard.
Format:
Scanf(control_string,one or more values);
Never include the newline character(\n)in a scanf() control string.The scanf()function knows the
input is finished when the user presses enter.
Always put a beginning space inside every scanf() control string.
return 0;
}
output
What is the total amount of the sale? 10.00
The sales tax for 10.00 is 0.70
TYPECASTING
You can override c defaults conversions by specifying your own temporary type change.This is
called typecasting
When you typecast you temporarily change variable data type from its declared data type to a
new one.
e.g
int age;
double age_factor;
int factor;
age_factor=(double)age*factor//temporarily change age from int to double