0% found this document useful (0 votes)
20 views15 pages

CSC 183 Chap-5

This document covers input and output operations in C programming, including reading and writing characters, formatted input for various data types, and formatted output using printf(). It explains the use of functions like getchar() and putchar(), as well as format specifiers for integers, floats, and strings. The content is structured as a lecture outline for a programming course.

Uploaded by

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

CSC 183 Chap-5

This document covers input and output operations in C programming, including reading and writing characters, formatted input for various data types, and formatted output using printf(). It explains the use of functions like getchar() and putchar(), as well as format specifiers for integers, floats, and strings. The content is structured as a lecture outline for a programming course.

Uploaded by

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

CSC – 183

Programming C
Chapter - 5

Managing Input and Output Operations

31 January 2023 CSC-183 1


• 7+8-10*2/4
• Priority level = */ upper level
• + - lower level
• 4+5*2 = 4+10=14 🡪9*2=18 XXXXX
• Start with upper level operator

31 January 2023 CSC-183 2


Today’s Outline:
• Reading A Character
• Writing A Character
• Formatted Input
– Integer
– Character Strings
• Reading Mixed Data Types
• Formatted Output
– Integer
– Character

31 January 2023 CSC-183 3


Reading A Character
• Reading a character means input a character from the
“standard input” like keyboard.

• To input a single character we use getchar() function.

variable_name = getchar();
char name;
name = getchar();

• Example:
• Dummy getchar() ?

31 January 2023 CSC-183 4


Writing A Character
• Show a character on output monitor.

• To show a single character we use putchar() function.


putchar(variable_name);
char name;
name = getchar();

• Example:
Answer = ‘Y’;
putchar(answer);
• dfsd

31 January 2023 CSC-183 5


Formatted Input
• Formatted input refers to an input data that has been
arranged in a particular format.
o i.e: 3.75 123 John
o scanf(“%f %d %s”,&cgpa,&id,name);
o scanf(“control string”,&var1,&var2,……varn);
o For string does not need & before variable name
o Control string = %d %f %lf
o Also called format specifier.
%c for character
%d for integer
%f for float
%lf for double

31 January 2023 CSC-183 6


Formatted Input (Integer)
• %wd w = field width

scanf(“%2d %5d”,&num1,&num2);
input: 50 31426 (num1 = 50, num2=31426)
input: 31426 50 (num1 = 31, num=426, 50
unread)

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


Input: 123 456 789
123 to a, 456 skipped (because of *), 789 to b

31 January 2023 CSC-183 7


Formatted Input (Character Strings)
• %ws or %w
• To input character and string.
char name[30];
scanf(“%15c”,name);
scanf(“%s”,name);

char name[30];
scanf(“%15c”,name);
scanf(“%s”,name);

31 January 2023 CSC-183 8


Reading Mixed Type Data

char code,name[20];

int id;

float cgpa;

scanf(“%d %c %f %s”, &id, &code, &cgpa, name);

printf(“%d %c %f %s”, id, code, cgpa, name);

31 January 2023 CSC-183 9


Formatted Output
• We use printf() function to show formatted output.

printf(“control string”,var1,var2, …., varn);

Control string = %d %f %lf

Also called format specifier.


%c for character
%d for integer
%f for float
%lf for double

31 January 2023 CSC-183 10


Formatted Output (Integer)
• Control string: %wd
w = width
Format Output
1 2 3 4
printf(“%d”,1234)
1 2 3 4
printf(“%6d”,1234)
1 2 3 4
printf(“%2d”,1234)
1 2 3 4
printf(“%-6d”,1234)
0 0 1 2 3 4
printf(“%06d”,1234)
31 January 2023 CSC-183 11
Formatted Output (Float)
• Control string: %w.pf wd
w = width , p = precision, y = 5.2255
Format Output
printf(“%6.4f”,y) 5 . 2 2 5 5

printf(“%6.2f”,y) 5 . 2 3

printf(“%-6.2f”,y) 5 . 2 3

printf(“%f”,y) 5 . 2 2 5 5

31 January 2023 CSC-183 12


Formatted Output (Strings)
• Control string: %w.ps
w = width , p = 1st p characters, msg[12] = “HELLO WORLD”
Format Output
printf(“%s”,msg) H E L L O WO R L D

H E L L O WO R L D
printf(“%12s”,msg)

H E L L O WO
printf(“%12.8s”,msg)

H E L L O
printf(“%.5s”,msg)
H E L L O WO
printf(“%-12.8s”,msg)
H E L L O WO R L D
printf(“%5s”,msg)
31 January 2023 CSC-183 13
31 January 2023 CSC-183 14
Thank You All

31 January 2023 CSC-183 15

You might also like