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

M2 Input-Output Operations

This document discusses input and output functions in C programming. It covers formatted functions like scanf() and printf() for input and output. It also covers unformatted functions like getchar(), putchar(), gets() and puts() for character and string input/output. Examples are provided for using each function properly. Formatting options for output functions like field width, precision and padding are also briefly introduced.

Uploaded by

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

M2 Input-Output Operations

This document discusses input and output functions in C programming. It covers formatted functions like scanf() and printf() for input and output. It also covers unformatted functions like getchar(), putchar(), gets() and puts() for character and string input/output. Examples are provided for using each function properly. Formatting options for output functions like field width, precision and padding are also briefly introduced.

Uploaded by

Nicoles Java
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

C Programming for Problem Solving:


21CPS23

Dr. Arunakumari B. N.
Assistant Professor,
Dept. of Computer Science and Engineering,
Email ID: [email protected]

7/13/2022 M1: Introduction to C Programming 1


Managing Input / Output Functions
Input Output function

Formatted I/O Functions Un-Formatted I/O Functions


Input functions Output
Input Output
Functions
functions Functions
getchar() putchar()
scanf() printf()
getch() putch()

getche()

gets() puts()
Formatted Input / Output Functions
Formatted Input / Output Functions classified into two types
1. scanf()
2. printf()

scanf():
› It is a formatted input function used to read one or multiple inputs from the
user.

› It reads input values from the user and stores in the specified variable address.

› Syntax : scanf(“control string" , variable address list);

› scanf() has two sections


› Control string: it consists of format specifier enclosed in double quotes. The
format specifier should be prefixed with % and indicates the type of value to be
read as input.

› Variable address list: each variable is prefixed with address operator(&) and
variables are separated by comma
Formatted Input / Output Functions Cont..
scanf():
› Examples:

‣ int a; float b; char c; char s[20];

‣ scanf(“%d%f”, &a, &b);

‣ scanf(“%c”, &c);

‣ scanf(“%s”, s);

Rules to be followed using scanf() function


› The number of format specifier should be equal to the number of variables

‣ scanf(“%d%f%c”, &a, &b); ×


‣ scanf(“%d%f”, &a, &b, &c); ×
‣ scanf(“%d%f%c”, &a, &b, &c);
Formatted Input / Output Functions Cont..
Rules to be followed using scanf() function

› The format specifier should match with the datatype of corresponding variables.
‣ int a; float x; char c; char s[12];

‣ scanf(“%d%f%c”, &x, &c, &a);


×
‣ scanf(“%d%f%c%s”, &a, &x, &c, &s);
×
‣ scanf(“%d%f%c%s”, &a, &x, &c, s);

› Different delimiters (space comma etc ) can be used between different format
specifier then the input should also be separated by the same delimiters.

‣ scanf(“%d#%d$%d”, &a, &b, &c); input: 10#20$ 30

› Best practice is not use space any delimiter between format specifier.

› scanf() reads until a whitespace character is found in a numeric specification.


Formatted Input / Output Functions Cont..
Formatted Input / Output Functions classified into two types
1. scanf()
2. printf()

printf():
› It is a formatted output function used to display one or multiple values on the
output screen to the user.

› Syntax : printf(“control string" , variable list);

› printf() has two sections

› control string: which includes different format specifiers prefixed with %


and also include backslash constant.

› Variable list: contains variables, expression, constant and variables are


separated by comma

Examples:
› printf(“%s”, str);
Formatted Input / Output Functions
Examples:
› printf(“%s”, str);

› printf(“%d\n %f\n %c\n”, a, b, c);

› printf(“%d”, a+x);

› printf(“Hello”);

2. write c program to add two numbers

#include<stdio.h>
int main()
{
int sum, a=10, b=20;
sum = a + b;
printf(“sum=%d \n", sum);
}
Unformatted Input / Output Functions Cont..
The getchar() function reads one character from the user and store it in the specified
variable waits until user presses ENTER Key . Display the character on the screen.

#include <stdio.h>
int main()
{
char c;
printf("Enter a character : ");
c = getchar();
printf("\nEntered character : %c ", c);
}

Output:
Enter a character : y
Entered character : y
Unformatted Input / Output Functions Cont..
putchar() function display only one character on screen.

#include <stdio.h>
int main()
{
char c = 'K';
putchar(c);
}

Output:
K
Note: Here, variable c is assigned to a character 'K'. The variable c is
displayed by the putchar(). Use Single quotation mark ' ' for a character.
Unformatted Input / Output Functions Cont..
The getch() function reads single alphanumeric character input from the user and doesn’t
wait for the user to type ENTER key. But, that the entered character will not be displayed.

#include <stdio.h>
#include <conio.h> // console input output
int main()
{
printf("\nHello, press any alphanumeric character to exit ");
getch();
}

Output:
Hello, press any alphanumeric character to exit

Note:
The above program will run until you press one of many alphanumeric characters.
The key pressed by you will not be displayed.
Unformatted Input / Output Functions Cont..
The putch() function display single alphanumeric character on the screen.

#include <stdio.h> //header file section


#include <conio.h> // console input output
int main()
{
char c;
printf("Press any key to continue\n ");
c = getch();
printf("input : ");
putch(c);
}
Output:
Press any key to continue
input : d
Note: The getch() function will not echo a character. The putch() function displays
the input you pressed.
Unformatted Input / Output Functions Cont..
getche() function reads single alphanumeric character from the user input and
store in the specified variable. And doesn’t wait for the user to type enter key.
Here, character you entered will be echoed to the user.

#include <stdio.h> //header file section


#include <conio.h> // console input output
main()
{
printf("\nHello, press any alphanumeric character or symbol to exit \n ");
getche();
}

Output:
Hello, press any alphanumeric character or symbol to exit
k

Note: The above program will run until you press one of many alphanumeric
characters. The key pressed by you will be echoed.
Unformatted Input / Output Functions Cont..
The gets() function can read a full string even blank spaces presents in a string.
The gets() function is used to get any string from the user.

#include <stdio.h> //header file section


int main()
{
char c[25];
printf("Enter a string : ");
gets(c);
printf("\n%s is awesome ",c);
}

Output:
Enter a string : bmsit college is awesome
bmsit college is awesome

Note: gets() reads input until it encounters newline or end of file, gets()does not stop
reading strings when it encounters white space instead it takes white space as a string
Unformatted Input / Output Functions Cont..
The puts() function display the character array or string on the screen.

#include <stdio.h> //header file section


int main()
{
char c[25];
printf("Enter your Name : ");
gets(c);
puts(c);
}

Output:
Enter your Name: computer science
Computer science

Note: puts() function is similar to printf() function but we cannot print other
characters using puts() function
Formatting Output Functions

2 3

7 5 6
Formatting Output Functions
Formatting Output Functions

2 3 . 3 5 6 0

9 8 3 6 . 3 5 9
Formatting Output Functions

5 6 9 4 . 3 5 8 0

B M S I T C O L L E G E

You might also like