0% found this document useful (0 votes)
12 views

SCS101 Lecture 5

Programing

Uploaded by

Marcel Oderoh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SCS101 Lecture 5

Programing

Uploaded by

Marcel Oderoh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LECTURE 5

EXPRESSIONS

An expression is a sequence of operands and operators.


Operands are values reacted upon e.g x,y e.t.c
Operators are symbols that tell the computer to perform certain mathematical or logical
operations.
Types of operators and precedence

a) Arithmetic operators-they carry out mathematical operations


Symbol Meaning
* Multiplication
/ Division
% Modulus or Remainder
+ Addition
- Substraction
Order of precedence also called math hierarchy or order of operators determines exactly how
C computes formulas.
C always performs multiplication,division,and modulus first,and then addition and
substraction.Same as BODMAS
b) Relational operators-their task is to compare data.

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

False True False

False False False

Logical OR(||)Truth table

Result
True True True
True False True

False True True

False False False

Logical NOT(!)Truth table

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.

Operator Example Equivalent


+= bonus+=500 bonus=bonus+500
-= Budget budget=budget-50
*= salary*=1.2 salary=salary*1.2

/= factor/=.50 factor=factor/.50
%= daynum%=7 daynum%7

e) Special operators-they are used to increment or decrement


Operator Example Description Equivalent
statements
++ i++ Postfix i=i+1;i+=1;

++ ++i Prefix i=i+1;i+=1

__ 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.

Defining conversion characters


Conversion characters tell printf() exactly how the data(following the characters)are to be
interpreted.

Conversion Character Output


%s String of characters
%c character
%d Decimal integer
%f Floating point numbers
%e Exponential notation
%g Use the shorter of %f or %e
%u Unsigned integer
%o Octal integer
%x Hexadecimal integer
%% Prints a percent sign(%)
NB: You can insert an l (lowercase l) or L before the integer and floating point conversion
characters(such as %ld and %lf)to indicate that a long integer or long double floating point is to
be printed.

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.

Character conversion characters

You must use the %c conversion character any time you print single characters.
e.g printf(“%c %c %c”,’A’,’B’,’C’);

Output on screen is:


ABC

Integer and floating point conversion characters %d and %f

When you want to print a numeric constant or variable,you must include the proper conversion
character inside the printf() control string.

e.g printf(“I am a student in stage II,I am %d years old,and I make %f\n”,20,50000.75);

NB: /n is an escape sequence character

output on the screen:


am a student in stage II,I am 20 years old,and I make 50000.75

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;

printf("Here are the initials\n");


printf("%c%c%c\n\n",first,middle,last);
printf("The age and number of dependents are:\n");
printf("%d %d \n", age,dependents);
printf("The salary and bonus are:\n");
printf("%f %f",salary,bonus);
return 0;
}
The output from this program is
Here are the initials
EWC
The age and number of dependents are:
32 2
The salary and bonus are:
25000.00 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 ;
}

This program produces the following table.


Parrots Rams Kings
3 5 2
2 5 1
2 6 4

Conversion character modifiers


You insert a modifying number inside numeric conversions to tell C how many print positions to
use.
e.g printf(“%d”,456) it prints the number 456,using three positions(the length of the data).

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

Using scanf() for input

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.

NB:Always put an ampersand(&)before variable names inside a scanf().


Never put an ampersand (&) before an array name inside a scanf()
Example 3

/*Prompt for a sales amount and print the sales tax*/


#include<stdio.h>
main()
{
float total_sale;
float tax;

printf(“what is the total amount of the sale?”);


scanf(“%f”,total_sale);
tax=total_sale* .07;

printf(“The sales tax for %.2f is %.2f”,total_sale,tax);

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

You might also like