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

C Input Output Functions

This document discusses various input/output functions in C programming including getchar(), scanf(), putchar(), and printf(). It provides examples of how to use each function to read input from and display output to the keyboard/terminal. The document also covers control structures like if, if-else, nested if-else, switch, and break statements along with examples of how to use them to make decisions based on conditional expressions.

Uploaded by

Masudul Huq Robi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

C Input Output Functions

This document discusses various input/output functions in C programming including getchar(), scanf(), putchar(), and printf(). It provides examples of how to use each function to read input from and display output to the keyboard/terminal. The document also covers control structures like if, if-else, nested if-else, switch, and break statements along with examples of how to use them to make decisions based on conditional expressions.

Uploaded by

Masudul Huq Robi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Fundamental C Programming

Class-4

1
Input Output Functions

2
Input a Character: getchar()
• Format: getchar()
• Purpose of function: To read a single character from
the terminal(keyboard).
• Example:
# include <stdio.h >
main()
{ char c;
c=getchar();
putchar(c);
}

3
Input Using Format: scanf()
• Format:
scanf( “format-specifier”,&var1,&var2,etc.);
• Purpose of function: To read data values from the
keyboard.
• Example:
# include <stdio.h >
main()
{ int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d,%d,%d",a,b,c);
}

4
Output a Character: putchar()
Format:
putchar(c);
The c must be an integer or character variable.
• Purpose of function: To write a single character to the
terminal(screen).
• Example:
# include <stdio.h >
main(){
char a=‘B’;
putchar(a);
}

5
Output Using Format: printf
• Format:
printf(format descriptor, var1, var2, …);
• Purpose: Sends output to standard out, which for now
we can think of as the terminal screen.
• Example:
# include <stdio.h >
main()
{ char c='a'; int i=97;
printf("%c,%d\n",c,c);
printf("%c,%d\n",i,i);
}

6
printf Examples
• Easiest to start with some examples
– printf(“%s\n”, “hello world”);
• Translated: “print hello world as a string followed by a newline
character”
– printf(“%d\t%d\n”, j, k);
• Translated: “print the value of the variable j as an integer followed
by a tab followed by the value of the variable k as an integer
followed by a new line.”
– printf(“%f : %f : %f\n”, x, y, z);
• English: “print the value of the floating point variable x, followed
by a space, then a colon, then a space, etc.

7
printf Format
Some special characters are not visible directly
in the output stream. These all begin with an
escape character (ie \);
– \n newline
– \t horizontal tab
– \a alert bell
– \v vertical tab

8
Input Function: scanf
• Takes input from keyboard
• Basic syntax
– scanf( format-specifier, &var1, &var2, etc.);
– Format-specifier is identical to printf
– We do not need to understand everything here, just
enough to do some basic I/O
• Examples
– int a; scanf(“%d”,&a);
– double x; scanf(“%f”,&x);
• Blocks program until user enters input!

9
Format Characters
(Format characters that are used very often)
%d, %ld decimal notation, integer types.
%c single character
%f, %lf decimal notation, real type, include float
or double types.
%e scientific notation, real type, include float
or double types
m width of data, it is a whole number.
.n the number of characters after the “ . ”
%s

10
Example
#include<stdio.h>
main()
{
float a;
scanf(“%f”,&a);
printf(“%f\n“, a);
printf(“%3.2f\n“, a);
printf(“%e\n",a);
}

11
Control Statement

12
Control Structures
• The C Conditional Control Structures are used to
select and execute a statement or sequence of
statements based on the value of a conditional
expression.
– The “if” statement without an “else” ption
– The “if” statement with the “else” option
– Nested “if…else…” statements
– A “switch” statement
– “switch” & “break” statements

13
if
• The if statement is used to express decisions.
Formally, the syntax is:
– if ( expression)
a statement;
or
– if (expression)
{
statements; //multiple statements
}

14
if
• Function:
– When the value of expression is non-zero(true), i.e.
the condition is met, the statement / statements is
/are executed.
– Conversely, when the value is zero(false), i.e. the
condition is not met, the statement / statements in
if-clause is/are not executed. This time, the next
program statement following the structure of the “if
-clause” is executed.

15
Example of ‘if’
• Input any two whole numbers, and then output which one is
greater
#include<stdio.h>
main()
{
float a,b,t;
scanf("%f,%f",&a,&b);
if (a>b)
printf(“%f is greater than %f”,a,b );
if (b>a)
printf(“%f is greater than %f”,a,b );
}

16
if-else
• The structure of “”if… else…” statement is:
if (expression)
statement 1;
else
statement 2;
or
if (expression){
statements; //multiple statements
}
else {
statements; //multiple statements
}

17
if-else
• Fuction:
– When the value of the expression is
nonzero(true), the condition is met, then the
statement /s in the true-clause( i.e. statement
1) is/are executed.
– Conversely, when the value is zero(false),
the condition is not met, then the
statement/s in the false-clause (i.e. statement
2) is/are executed.
18
Example of “if-else”
• Input any two whole numbers, and then output
which one is greater
main()
{ int x ,y ;
scanf (“%d” , &x);
if (x<=0) y=0;
else y=1;
printf(“x=%d, y=%d\n”, x, y );
}

19
Nested “if-else”
• Generally, the structure of nested “if…else…”
statements is following:
if(expression 1)
statement 1;
else if (expression 2)
statement 2;
else if (expression 3)
statement 3;
else if (expression 4)
statement 4;
else statement 5;

20
Example of Nested “if-else”
main()
{ int x, y ;
scanf (“%d”, &x);
if (x<0)
y = -1 ;
else if (x==0)
y=0;
else y = 1 ;
printf (“x=%d, y=%d\n”, x, y) ;
}

21
switch
• When an “if-else” is nested several levels, the
statement is not good statement. In this case we can use
another statement to replace the nested ifclause. This
another statement is “switch statement”.
• The structure of a switch statement:
switch (expression)
{case const-expr 1: statement 1;
case const-expr 2: statement 2;
case const-expr n: statement n;
default : statement n+1;
}

22
switch
• A switch statement starts with the keyword switch
followed by an expression in parentheses, followed
by brackets enclosing a series of labeled cases
ending in an optional default case.
• To use a switch statement, all the possible choices
must have simple identifying codes. There must be
single characters in quotes, integers, or
enumerations constants.
• When a switch is executed, the expression is
evaluated and its value compared to the case labels.
If any case label matches, control will go to the
statement following that label.
23
Switch & break
• Control then proceeds to execute the
following statement and through all statements
after that until it reaches the bottom of the
switch.
• In order to cause control to skip around all the
following case and go directly to the end of the
switch statement, normally, each group of
statements ends with a “break” statement:
case const-expr 1: statement 1; break;
case const-expr n: statement n; break;
default : statement n+1;
24
Example of Switch
Example: Input the score (hundred-mark system
),
output corresponding grade.
The regular is: Score Grade
90_____100 A
80_____89 B
70_____79 C
60_____69 D
Less than 60 E

25
Example of Switch
main()
{ float score;
printf(“please input student score:”);
scanf(“%f”,&score);
if (score<60) printf(“E\n”);
else
switch ((int)(score/10)) /*Explicit typecast*/
{ case 10:
case 9 : printf(“A\n”); break;
case 8 : printf(“B\n”); break;
case 7 : printf(“C\n”); break;
case 6 : printf(“D\n”); break;
default : printf(“error\n”);
}
}

26

You might also like