Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BCSE102L C Input Output, Operators

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

BCSE102L STRUCTURED

AND OBJECT ORIENTED


PROGRAMMING
Formatted Input Output
Operators
Dr. R. Jothi SCOPE
I/O statements
Input/Output in C
• C has no built-in statements for input or output.

• A library of functions is supplied to perform these operations. The I/O


library functions are listed the “header” file <stdio.h>.

• You do not need to memorize them, just be familiar with them.


Input/Output in C
scanf ( ) ;
• This function provides for formatted input from the
keyboard. The syntax is:
scanf ( “format” , &var1, &var2, …) ;
• The “format” is a listing of the data types of the variables to
be input and the & in front of each variable name tells the
system WHERE to store the value that is input. It provides
the address for the variable.
• Example:
float a; int b;
scanf (“%f%d”, &a, &b);
Input - scanf()

scanf(“control string”, argument)

e.g. scanf(“%d”, &number1)

Control string Data type meaning


%d int integer
%f float float
%lf double double
%c char Character
%s string Character array
Input/Output in C
getchar ( ) ;

• This function reads exactly one character from the keyboard.


• Example:

char ch;
ch = getchar ( ) ;
Input/Output in C
putchar (char) ;

• This function provides for printing exactly one character to the screen.
• Example:

char ch;
ch = getchar ( ) ; /* input a character from kbd*/
putchar (ch) ; /* display it on the screen */
Formatted Output with printf
printf ( ) ;
• This function provides for formatted output to the screen. The syntax
is:
printf ( “format”, var1, var2, … ) ;
• The “format” includes a listing of the data types of the variables to be
output and, optionally, some text and control character(s).
• Example:
float a ; int b ;
scanf ( “%f%d”, &a, &b ) ;
printf ( “You entered %f and %d \n”, a, b ) ;
Formatted Output with printf
• Format Conversion Specifiers:
%d -- displays a decimal (base 10) integer
%l -- used with other specifiers to indicate a "long"
%e -- displays a floating point value in exponential
notation
%f -- displays a floating point value
%g -- displays a number in either "e" or "f" format
%c -- displays a single character
%s -- displays a string of characters
Printing expressions directly
Escape Sequence
• \a - alert (bell sound)
• \n – new line
• \t - horizontal tab
• \b – backspace
• \” - quotation mark
• \\ - backslash
What does the following code print?
#include <stdio.h>
int main()
{ int a=10; float b=4.5f;
printf("\"score\" in course %d is %f", a, b);
return 0;
}

"score" in course 10 is 4.500000


What does the following code print?
#include <stdio.h>
int main()
{ int a=10; float b=4.5f;
printf("\"score\" in course %d is \\\t%f", a, b);
return 0;
}
Operators
Expression
• combination of operands and operators
• Examples
• c = a + b;
• (x > y ) && (x > z)
• a, b, c, x, y and z are operands;
• >, &&, = and + are operators
Types of Operators
• Arithmetic Operators
• + (Plus), - (Minus), * (Multiplication), /(Division)
• Logical
• && (AND),|| (OR), ! (not)
• Relational
• <, >, <=, >=,==
• Conditional operator (ternary)
•?
Some more Arithmetic Operators
• Prefix Increment : ++a
• example:
• int a=5;
• b=++a; // value of b=6; a=6;

• Postfix Increment: a++


• example
• int a=5;
• b=a++; //value of b=5; a=6;

24
Contd…
• Modulus (remainder): %
• example:
• 12%5 = 2;

• Assignment by addition: +=
• example:
• int a=4;
• a+=1; //(means a=a+1) value of a becomes 5

Can use -, /, *, % also

25
Contd…
• Comparision Operators: <, > , <=, >= , !=, ==, !,
&&, || .
• example:
• int a=4, b=5;
• a<b returns a true(non zero number) value.

• Bitwise Operators: <<, >>, ~, &, | ,^ .


• example
• int a=8;
• a= a>>1; // value of a becomes 4

26
Logical Operators
• There are three kinds of logical operators.
• &&: and
• ||: or
• !: not
• Logical expression is an expression which uses one or more logical
operators, e.g.,
• (temperature > 90.0 && humidity > 0.90)
• !(n <= 0 || n >= 100).
The Truth Table of Logical Operators
Op 1 Op 2 Op 1 && Op2 Op 1 || Op2
1 1 1 1
1 0 0 1
0 1 0 1
0 0 0 0

Op 1 ! Op 1
1 0
0 1
Here 1 is any non zero value
Ternary operator ?
Expression1?Expression2:Expression3;
largest = (first > second) ? first :
second;
Ternary operator Example

#include <stdio.h>

int main()
{
int avg=78;
char grade;
grade= (avg>60)?'a':'b';
int attendance=88, bonus=5;;
int ffcs_score;
ffcs_score=grade=='a'?(attendance>75?9:7):(bonus>=5?8:4);
printf("%d",ffcs_score);
return 0;

}
Operator Precedence
• Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c ?
• All operators have precedence over each other
• *, / have more precedence over +, - .
• If both *, / are used, associativity comes into picture.
(more on this later)
• example :
• 5+4*3 = 5+12= 17.

The precedence of operators determines which operator is


executed first if there is more than one operator in an
expression.
31
Associativity
• The associativity of operators determines the direction in which an
expression is evaluated
• Eg
Y = a/b*c ; first a+b and then (a/b)*c
Y=a/(b*c); here ( ) takes precedence, so first b*c and then a/(b*c)
Output?

4 4
Output?

c=12 a=11
Output?

14
Output?

11 10 12 11 10 12
12 12
Outut?
#include <stdio.h>
int main()
{
int a=10;
printf("%d %d %d %d",++a, a++,a++,a);
printf("\n%d", a);
return 0;
}
Output?
#include <stdio.h>
#include <stdio.h>
int main() int main()
{ {
int a=1,d; int a=1,d;
d=++a+a;
d=++a+2;
printf("%d", d);
printf("%d", d); return 0;
return 0; }
}
234
Precedence
Table

40

You might also like