0% found this document useful (0 votes)
52 views10 pages

Unit 2 - I Input and Output v1.5

This document discusses input and output functions in C programming. It covers unformatted I/O functions like getchar() and putchar() for reading and writing single characters. It also covers formatted I/O functions like scanf() and printf() for reading and writing data in predefined formats. Specific formatting is described for reading and writing integers, floats, strings, and mixed data types. Guidelines are provided for detecting errors in user input.

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)
52 views10 pages

Unit 2 - I Input and Output v1.5

This document discusses input and output functions in C programming. It covers unformatted I/O functions like getchar() and putchar() for reading and writing single characters. It also covers formatted I/O functions like scanf() and printf() for reading and writing data in predefined formats. Specific formatting is described for reading and writing integers, floats, strings, and mixed data types. Guidelines are provided for detecting errors in user input.

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/ 10

TM's

Lecture Notes in C Programming (LNCP)


UNIT 2 Part 1
Input and Output(I/O)
Ver. 1.5
TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

Table of Contents
Input/Output(I/O) Functions 3

Unformatted I/O 3

1. Reading a character 3

2. Writing a character 4

Formatted I/O 5

3. Formatted Input 5
3.1 Inputting Integers 5
3.2 Inputting real number: float and double types 6
3.3 Formatted Input: Strings 6
3.4 Reading Mixed Data Types 6

4. Formatted Output 7
4.1 Outputting Integers: 8
4.2 Output of Real Numbers 8
4.3 Output of single character 9
4.4 Output of strings 9

5. Guidelines to improve readability of output: 10

2 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

​Input/Output(I/O) Functions
I/O functions in C are categorized as:
1. Unformatted I/O functions
E.g. getchar(), putchar()
2. Formatted I/O Functions
E.g. scanf(), printf()

​Unformatted I/O
​1. Reading a character
This means taking character input from the user.
❑ General Syntax:
variable_name = getchar();
❑ E.g.
char c;
c = getchar();

Program: Reading a character


#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");

/* getchar function waits for user to type a


Character*/
c = getchar();
printf("Character %c is entered",c);
return 0;
}
Output:
Enter a character:A
Character A is entered

Check the type of character entered


❑ Include header file: #include<ctype.h>
❑ Use functions: isalpha() and isdigit()
❑ Reading a character(prg302)

Program: Check the type of character entered

3 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
if(isalpha(c)!=0)
printf("Entered character is a letter.\n");
else if(isdigit(c)!=0)
printf("Entered character is a digit.\n");
else
printf("Entered character is not alpha-numeric\n");

return 0;
}
Output:
Enter a character:A
Entered character is a letter.

Enter a character:9
Entered character is a digit.

Enter a character:#
Entered character is not alpha-numeric

​2. Writing a character


Outputting a character on to standard output device
❑ Function: putchar(variable_name);
Program : prg303
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character entered is:");
putchar(c);
return 0;
}
Output:
Enter a character:A
Character entered is:A

4 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

● Changing character case(prg304)


Changing case of character entered.(Upper & lower case)
Program:
int main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Output: ");
if(islower(c))
putchar(toupper(c));
else
putchar(tolower(c));
return 0;
}
Output:
Enter a character:A
Output: a

Enter a character:a
Output: A

​Formatted I/O
​3. Formatted Input
● Formatted Input refers to input data expected/inputted in predefined format in the program.
E.g.: Entered data: 15.75 123 John
● This data is read to corresponding type variables.
● Function used for formatted Input: scanf
● 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);

​3.1 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)
❑ Read maximum possible digits(value) from the number entered.

5 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

Program: Formatted Input


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

Output:
Enter 6 digit number:678123
The number is: 67812
❑ Formatted Input
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 the user is skipped.
❑ Formatted Input

​3.2 Inputting real number: float and double types


float a,b,c;
scanf("%f %f %f",&a,&b,&c);

❑ For double data type, control string is "%lf"

​3.3 Formatted Input: Strings


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.

​3.4 Reading Mixed Data Types


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

6 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

Program: Detecting errors in input

❑ scanf function returns the number of input data successfully read.


#include<stdio.h>
int main()
{
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");
return 0;
}

Output:
56 trtrtt
Inputs = 2
Input Error

​4. Formatted Output


❑ Most used output function: printf()
❑ General Syntax:
printf(“Control string", arg1,arg2,arg3,…,argn);
❑ E.g.:
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.

Examples:
printf("hello world"); ==> hello world
printf(“ "); ==>prints a blank space
printf("\n"); ==>prints a new line
printf("hello\nworld"); ==>hello
world

7 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

printf("a=%d \t b=%d",10,5); ==>a=10 b=5(tab


inserted)

​4.1 Outputting Integers:


❑ General format specification: %wd
❑ w==> Specify minimum field width for the output.
❑ That many(w) spaces is reserved on screen for the data displayed.
❑ Output of Integer Numbers
Examples: output

1. printf("%d",9876);
2. printf("%6d",9876);
3. printf("%2d",9876);
4. printf("%-6d",9876);
5. printf("%06d",9876);

"%ld" ==> For long int


"%hd" ==> For short int

​4.2 Output of Real Numbers


❑ 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

8 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

Output of Real Numbers


Examples: float y = 98.7654; output
1.printf("%7.4f",y);
2.printf("%7.2f",y);
3.printf("%-7.2f",y);
4.printf("%f",y);
5.printf("%10.2e",y);
6.printf("%11.4e",-y);

​4.3 Output of single character


❑ General format string: %wc
❑ w==> width c==> character output
Examples: char ch = ‘H’; output
1.printf("%c", ch );
2.printf("%6c",ch);
3.printf("%-4c",ch);

​4.4 Output of strings


❑ General format string : %w.ps
❑ (E.g. "%5.2s""%s")
char str[20] = "HELLO WORLD";
printf("%s",str);
printf("%15s",str);
printf("%15.5s",str);
printf("%.5s",str);
printf("%-15.5s",str);

Mixed Data Output


❑ printf("%d %c %s %f", a,b,c,d);
❑ Commonly used format strings

9 (For non-commercial educational use only)


TM’s C Programming Lecture Notes Ver. 1.5 Input and Output(I/O)

❑ Enhancing readability of output

​5. Guidelines to improve readability of output:


❑ 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.

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");

Sample Questions:
1. Differentiate between formatted input and output in C.
2. What are the formatted input/output functions in C. Explain with examples.
3. What are the unformatted input/output functions in C. Explain with examples.
4. Predict the output of following statements:
char str = "India is my country";
printf("%s",str);
printf("%13s",str);
printf("%14.5s",str);
printf("%.6s",str);
printf("%-15.5s",str)

5. Predict the output of following statements:


float y =243.45576
printf("%8.3f",y);
printf("%9.2f",y);
printf("%-7.2f",y);
printf("%f",y);
printf("%11.2e",y);
printf("%11.4e",-y);

10 (For non-commercial educational use only)

You might also like