BCSE102L C Input Output, Operators
BCSE102L C Input Output, Operators
BCSE102L C Input Output, Operators
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;
}
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
25
Contd…
• Comparision Operators: <, > , <=, >= , !=, ==, !,
&&, || .
• example:
• int a=4, b=5;
• a<b returns a true(non zero number) value.
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.
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