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

Unit 2 - I Input and Output

The document discusses input and output functions in C programming. It covers unformatted I/O like getchar() and putchar() as well as formatted I/O functions like scanf() and printf() for reading and writing various data types including integers, floats, characters and strings.

Uploaded by

Thomas Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Unit 2 - I Input and Output

The document discusses input and output functions in C programming. It covers unformatted I/O like getchar() and putchar() as well as formatted I/O functions like scanf() and printf() for reading and writing various data types including integers, floats, characters and strings.

Uploaded by

Thomas Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Input and Output

C PROGRAMMING
UNIT 2
2

I/O in C
1. Unformatted I/O (getchar(),putchar()..)
2. Formatted I/O (printf(), scanf() )
Reading a character : getchar() 3

❑ General Syntax
variable_name = getchar();
❑ E.g.
char c;
c = getchar();
Reading a character(prg301) 4

Program: prg301
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character %c is entered",c);
return 0;
}
Functions related to character data 5

❑ Check the type of character entered


❑ Include header file: #include<ctype.h>
❑ Use functions: isalpha() and isdigit()
Reading a character(prg302)
❑ Check the type of character entered (Program: prg302) 6
#include<stdio.h>
#include<ctype.h>
void main()
{
char c;
printf("Enter a character:");
c = getchar();
if(isalpha(c)!=0)
printf("Enter character is a letter.\n");
else if(isdigit(c)!=0)
printf("Enter character is a digit.\n");
else
printf("Enter character is not alpha numeric\n");
}
Writing a character (prg303) 7

❑ Function: putchar(variable_name);
#include<stdio.h>
void main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character entered is:");
putchar(c);
}
Changing character case (prg304) 8
❑ Changing case of character entered.(Upper & lower case)
Program: prg304
void main()
{
char c;
printf("Enter a character:");
c = getchar();
if(islower(c))
putchar(toupper(c));
else
putchar(tolower(c));
}
Formatted Input 9

❑ Refers to input data arranged in particular format.


❑ E.g.: 15.75 123 John
❑ Read to corresponding type variables.
Formatted Input: scanf 10

❑ General format:
scanf("control string",arg1,arg2,…,argn);
❑ Control string(format string): specifies order in
which data to be entered and the type of the data
expected.
❑ arg1,arg2,…,argn : variables where data is stored.
❑ E.g: scanf("%d %c %f", &num,&ch,&fno);
Formatted Input: Integers 11

Inputting Integers
❑ General format of control string: %wd
❑ w ➔ Specifies width of the data to read
❑ d ➔ Indicated we want to read an integer.
❑ E.g. scanf("%5d", &num1)
❑ Reads five digits of user entered number

❑ E.g: scanf("%d", &num1)


❑ Reads maximum possible from number entered.
Formatted Input:Integers(prg305) 12

void main()
{
int a;
printf("Enter 6 digit number:");
scanf("%5d", &a);
printf("The number is: %d",a);
}

Output:
Enter 6 digit number:678123
The number is: 67812
Formatted Input : Integers 13

Skipping a number from reading


❑ E.g. scanf("%d %*d %d", &a, &b) ;
❑ If user entered: 10 50 100
❑ Data read as: a 🡸 10, b🡸 100
❑ 50 entered by user is skipped.
Formatted Input: Real numbers 14

Inputting real number: float and double types

Common usage:
float a,b,c;
scanf("%f %f %f",&a,&b,&c);
❑ For double data type, control string is "%lf"
Formatted Input: Real numbers 15

Inputting float
❑ General format of control string: %wf for float, %wlf for
double
❑ w ➔ Specifies width of the data to read incl. decimal point.
❑ f ➔ Indicated we want to read a float.
❑ E.g. scanf("%5f", &num1)
❑ Reads first five characters of user entered number incl. decimal point

❑ E.g: scanf("%f", &num1)


❑ Reads maximum possible characters from number entered.
Formatted Input: Strings 16

❑ General format: %ws


❑ (E.g: %s , %5s)
❑ char str[10];
❑ scanf("%s",str); // Read till first space or \n found
❑ scanf("%5s",str); // Read first 5 characters
❑ scanf("%[^\n]",str); // Read until \n is found. May not
work well with all compilers.
❑ %[^x] // Read until first x appear in entered string,
excl. x.
Formatted Input 17

Reading Mixed Data Types

int a;
char b;
float c;
scanf("%d %c %f",&a, &b,&c);
Formatted Input(prg306) 18

Detecting errors in input


❑ scanf function returns number of input data successfully read.
int a; char b; float c;
int d = scanf("%d %c %f",&a, &b, &c);
printf("Inputs = %d\n",d);
if(d!=3)
printf("Input Error");
else
printf("No Input Error");
Output:
56 t rtrtt
Inputs = 2
Input Error
Formatted Output 19

❑ Most used output function: printf()


❑ General Syntax:
printf(“Control string", arg1,arg2,arg3,…,argn);
❑ E.g.: c
int a = 10,b=5,sum;
sum = a+b;
printf("The sum of %d and %d is %d. \n",a,b,sum);
❑ Output:
The sum of 10 and 5 is 15.
Formatted Output 20

Examples:
printf("hello world"); 🡺 hello world
printf(" "); 🡺 //prints a blank space
printf("\n"); 🡺 //prints a new line
printf("hello\nworld"); 🡺 hello
world
printf("a=%d \t b=%d",10,5); 🡺 a=10 b=5
(tab inserted)
Formatted Output 21

Outputting Integers:
❑ General format specification: %wd
❑ w ⇒ Specify minimum field width for the output.
❑ That much(w) space is reserved on screen for the data
displayed.
Output of Integer Numbers 22

❑ Examples:
output
1. printf("%d",9876); 9 8 7 6

2. printf("%6d",9876); 9 8 7 6

3.printf("%2d",9876); 9 8 7 6

4.printf("%-6d",9876); 9 8 7 6

5. printf("%06d",9876); 0 0 9 8 7 6

"%ld" ➔ For long int


"%hd" ➔ For short int
Output of Real Numbers 23

❑ General format specifier


%w.pf or %w.pe
(e.g 1.234e3)
❑ w🡸 width specification
❑ p 🡸 precision specification
❑ Default precision is 6.
❑ printf("%f",2.3) 🡸 2.300000
❑ printf("%.3f",2.3) 🡸 2.300
Output of Real Numbers 24

❑ Examples: float y = 98.7654;


output
1.printf("%7.4f",y); 9 8 . 7 6 5 4

2.printf("%7.2f",y); 9 8 . 7 7

3.printf("%-7.2f",y); 9 8 . 7 7

4.printf("%.4f",y); 9 8 . 7 6 5 4

5.printf("%10.2e",y); 9 . 8 8 e + 0 0 1

6.printf("%11.4e",-y); - 9 . 8 7 6 5 e + 0 1
Output of single character 25

❑ General format string: %wc


❑ W ⇒ width c ⇒ character
output
Output of Single Character 26

❑ Examples:
Output
char ch = ‘H’; H
printf("%c", ch ); H
printf("%6c",ch); H
printf("%-4c",ch);
Output of strings 27

❑ General format string : %w.ps


❑ (E.g. "%5.2s""%s")
char str[20] = "HELLO WORLD";
printf("%s",str); H E L L O W O R L D
printf("%15s",str); H E L L O W O R L D

printf("%15.5s",str); H E L L O

printf("%.5s",str); H E L L O

printf("%-15.5s",str); H E L L O
Mixed Data Output 28

❑ printf("%d %c %s %f", a,b,c,d);


❑ Commonly used format strings
Enhancing readability of output 29

Guidelines to improve output readability


❑ Provide enough blank space between two data
items.
❑ Introduce appropriate headings and variable
names.
❑ Print special messages whenever peculiar
conditions occur in output.
❑ Introduce blank lines between important sections
of output.
Enhancing readability of output(prg307a) 30

printf("\nOUTPUT RESULTS\n");
printf("==============\n");
printf("NAME \t AGE \t ID\n");
printf("Ram \t 35 \t 10434\n");
printf("Mohan \t 36 \t 104\n");
printf("Hari \t 25 \t 12\n");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy