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

Topic 3 - Inputand Output Functions

1. The document discusses input and output functions in C including getchar() and putchar() for single characters, gets() and puts() for strings, scanf() and printf() for multiple data types. 2. scanf() is used to input multiple data types from standard input using format specifiers. printf() outputs data to standard output using format specifiers to handle different data types. 3. Additional format specifiers control precision, field width, justification and leading zeros for both input and output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Topic 3 - Inputand Output Functions

1. The document discusses input and output functions in C including getchar() and putchar() for single characters, gets() and puts() for strings, scanf() and printf() for multiple data types. 2. scanf() is used to input multiple data types from standard input using format specifiers. printf() outputs data to standard output using format specifiers to handle different data types. 3. Additional format specifiers control precision, field width, justification and leading zeros for both input and output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

1

INPUT AND OUT PUT FUNCTIONS

(a) INPUTTING AND OUTPUTTING A SINGLE CHARACTER

The getchar() function


Used to input a single character.
This function accepts one character from the standard input device i.e, the keyboard
The general form is
variable = getchar();

The puchar() function


Used to output single character.
This function outputs a single character to the standard output device, i.e, the monitor.
The general form is
Putchar(variable);

Example
/* program that accepts a single character and displays it on the screen
#include<stdio.h>
void main()
{
charc1;
c1 = getchar();
putchar(c1);
}

(b) INPUTTING AND OUTPUTTING STRINGS

The gets() function


Used to input a string trailing the new line character

The puts() function


Used to display a string

Example
#include<stdio.h>
void main()
{
char line[80];
printf(“type in a line\n”);
gets(line);
puts(line);
}
output: type in a line
What we type: Catholic university of eastern Africa
2

What is displayed: Catholic university of eastern Africa


(c) MULTIPLE DATA INPUT

The scanf()
This function accepts all types of data from the standard input device.

The general form is

scanf(“control string”, ad1, ad2,ad3, ….,adn);

where,

-the control string contains several groups of characters which describe, the data type of
the variables: v1, v2, v3,,…,vn that are to be read in.

-the first character of each group in control string is %. It is followed by a conversion


character, which describes to what data type v1, v2, ….vn belong.

-the groups are separated by blank spaces. The addresses ad1, ad2, ….,and of the variables
vr1, vr2,….vrn are obtained by operating the address fetching operator & on the variables.

Example 1: To read in integer i

scanf(“%d”,&i);

Example 2: To read in a character variable code and an integer

char code;
int num;
scanf(“%c %d”, &code, &num);

Conversion characters used for input

Conversion character meaning


c data item is a single character
d data item is a decimal integer
e data item is a floating point value e.g.
f data item is a floating point value e.g. 250.0
g data item is a floating point value.
h data item is a short integer.
i data item is a decimal, hexadecimal or octal integer
o data item is an octal integer
s data item is a string followed by space.
u data item is an unsigned decimal integer
x data item is a hexadecimal integer.
[] string that matches with characters within […]
3

Inputting strings
1. char name[40];
scanf(“%s”, name);
for the input “Joseph p.k.”,the data assigned to the name is Joseph

2. char name[40];
scanf(“%[abcdefghijklmnopqrstuvwxyz]”, name);
for the input “joseph p k”, the data assigned to the name joseph p k
for the input “joseph p k”
name[] = “joseph”

3. read in the sting data until the new line character is encountered:
scanf(“%[^\n]”, name);
for the input joseph p k
name[] = “joseph”
you can type anything
name[] = “you can type anything”

specifying field width


Arrays with conversion character, the width of the data can be specified. The width
specified indicates maximum field width.

Examples
1. scanf(%3d %3d %3d”,&a,&b,&c);
(a) for the input 123456789
a = 123
b = 456
c = 789

(b) for the input 1 2 3


a=1
b=2
c=3

(c) for the input 1 2 3 4 5 6


a = 123
b=4
c = 56

(d) for the input 1 2 3 4 5 6 7 8


a = 123
b=4
c = 567

4. scanf(%hd %lf %ld”,&i,&x,&k);


4

5. scanf(%c%c%c”,&c1,&c2,&c3);

for the input a b c


c1 = a
c2 = b
c3 = c

for the input a b c


c1 = a
c2 = b
c3 = b

Multiple data output

printf()
This function is used to output any type of data

The general form is

printf(“control string”, v1, v2,….,vn);

where,

-the control string contains a group of characters which describe the data types of the
variables v1,v2,….,vn, which are to be printed out.
-each group of characters start with a % sign. This sign is followed by a conversion
character which indicates the data type of the variable to be displayed.

Conversion character Meaning


C the data item is displayed as a s ingle character.
D the data item is displayed as a signed decimal
integer.
E the data item is displayed as a floating point
number in exponent form e.g, 2.5e+01
g the data item is displayed as a float either in e
form or f form. Trailing os won’t appear. also
trailing pts will not appear.
f the data item is displayed as a floating point no.
in fractional form,
i the data item is displayed as a signed decimal
integer.
o the data item is displayed as an octal integer
without leading zeros
s the data item is displayed as a sting with the null
character /o as the end of the string.
5

u the data item is displayed as an unsigned decimal


integer.
x the data item is displayed as a hexadecimal
integer without the leading ox.

Examples
1. float a = 2.5;
char item[] = “book”;
int I = 257;

printf(“%d%s%f\n”,I, item, a);

result 257book2.50000
2. printf(“b%db%sb%f\n”,i, item, a);
result: b257bbookb2.50000
(if blanks are left they will appear in the result)

3. x = 0.005;
y = 255.0;
printf(“%fb%f\n”,x,y);
result: 0.005000b255.000000

4. printf(“%eb%e\n”,x,y);
result: 5.000000l-003b2.550000e+002

5. x = 2.53e23;
y = 747.35
printf(“%gb%g\n”,x,y);
result: 2.53e+23b 747.35

Handling strings with s


char line[] = “ this is a demonstration for output”;
printf(“%s\n”,line);

Field width for output


Minimum field width can be specified for output.
If the data to be displayed is equal to the field specified, the data is displayed in the
specified form with right justification.
If the data to be displayed is less than the field specified, empty blanks will be added to
the left side of the data.
If the data to be displayed out requires more field width than that specified, the specified
width is ignored and the data is displayed by adding the required width.

Example
float a = 25.742;
int n = 123;
6

(a) printf(“%2fb%6fb%12f\n”,a, a, a);


Result: 25.742000b25.742000bbbb25.742000

(b) printf(“%2db%3db%7d\n”,n, n, n);


Result:123b123bbbbb123

Precision
Refers to the number of digits in the fraction. Precision can be specified for floating point
type data and string data.

Examples
Precision for floating point value
1. float x = 745.247;
printf(“%6.2fb%6.2f\n”, x, x, x);

output: 745.247000b745.247bbbb745.247

2. float x = 745.247, y = 235.434;


printf(“%6.2fb%6.2f\n”, x, y);

output: 745.25b235.43

3. float x = 745.82;
printf(“%4.0f\n”, x);

output: 745

Precision for strings


char line[] = “this is for demonstration”;

1. printf(“%12s\n”, line);

output: this is for demonsration

2. printf(“%30s\n”, line);

output: bbbbb this is for demonstration

3. printf(“%45s\n”, line);

output: this is for demonsration

4. printf(“%7s\n”, line);

output: this is for demonsration


7

5. printf(“%10.7s\n”, line);

output: bbb this is for demonsration

Flag characters
They are used to alter the output

Flag character meaning


- data item is left justified
+ a sign + or- will precede the signed numerical value.
0 causes leading zeros to appear instead of leading blanks. It is
applicable for right justified data item.
“ a blank space will precede each positive signed numerical
data. if + and “ are used, + overrides.
# causes octal and hexadecimal integers to be printed with
leading o for octal and leading ox for hexadecimal numbers.
It causes a decimal point to appear in all floating point values
even if the value is a whole number. Also prevents truncation
of trailing zeros in g type conversion.
Example
1. i =123, j = 4567;
printf(“:%4db%4d\n”, i,j);
printf(“:%-4db%-4d\n”, i,j);
Output : b123b4567
: 123bb4567
2. char line[] = “this is a demo”
printf(“:%4db%4d\n”, i,j);
printf(“:%-4db%-4d\n”, i,j);
Output :1234567890123456789
: this is a demo
:this is a demo

3. int i =747,j = -356;


printf(“:%5d%5d\n”, i,j);
printf(“:%+5d%+5d\n”, i,j);
printf(“:%Ǿ5d%Ǿ5d\n”, i,j);
Output :bb747bb-356
:b+747bb-356
:00747b-0356
4. floatx =12.0, y = 7.43;
printf(“:%g%g\n”, x,y);
printf(“:%#g %#g \n”,x, y);
Output : b12b7.43
: 12.b7.4300
8

Printing messages in the output


Any character that cannot be recognized will be displayeed as it is
e.g., int i = 25;
printf(“the value of i = %d”, i);

Output: The value of i = 25

Simple programs
1. Write a program to input three integers and find their average
#incude<stdio.h>
Void main()
{
Int a, b ,c;
Float avg;
Printf(“type in the 3 integers”);
Scanf(%d%d%d”,&a ,&b ,&c);
Avg = (a+b+c)/3.0; or avg = float(a+b+c)/3;
Printf(:average = %f\n”, avg);
}

2. Write a program to compute compound interest


#incude<stdio.h>
#include<math.h>
Void main()
{
Int n;
Float x,r,p,tot;
Printf(“type in the principal amount:”);
Scanf(%f”,&p);
Printf(“type in the rate of intrest and years:”);
Scanf(%f%d”,&r ,&n);
X =1+r\100.0;
tot= p ×pow(x,n);
Printf(“tolal value after %d years = %9.2f\n”,n,tot);
}
9

3. A milkman buys pure milk at the rate of ksh 8/ litre. He then adds 1 litre of
water for every 4 litres of pure milk and sells the water milk at the rate of ksh
9.50/ litre.write a program to compute the gain for the milk vender for a purchase
of x litres of pure milk.

#incude<stdio.h>
void main()
{
int x;
float pmrate, wmrate, gain;
printf(“enter the anount of pure milk in litres:”);
scanf(%d”,&x);
printf(“enter the rate of pure milk and water milk:”);
scanf(%f%f”,&pmrate ,&wmrate);
gain =(x+x/4.0)*wmrate-(pmrate*x);
printf(“the gain is %f”, gain);
}

Examples on scanf and printf

#incude<stdio.h>
void main()
{
int c = 3000;
float a=2.5 ,b = 0.0005;
printf(“%f %f %f\n”. a, b, c);
printf(“%8f % 8f% 8f\n”. a, b, c);
printf(“%8.3f %8.3f %8.3f\n”. a, b, c);
printf(“%e %e %e\n”. a, b, c);
printf(“%12.4e %12.4e %12.4e\n”. a, b, c);
printf(“%-8f %-8f %-8f\n”. a, b, c);
printf(“%+8f %+8f %+8f\n”. a, b, c);
printf(“%#8f %#8f %#8f\n”. a, b, c);
printf(“%g %g %g\n”. a, b, c);
printf(“%#g %#g %#g\n”. a, b, c);
}
10

Program to arrange a set of n floating point numbers in descending order.

#include<stdio.h>
void main()
{
float x[50];
int i,j, n;
float temp;
printf(“type in the numbers of values to be arranged:”);
scanf(“d”,&n);
for (i=0;i<n; i++)
{
printf(“type in no.%d x value:”, i);
scanf(“%f”,&x[i]);
}
for(i = 1; i<=n-1; ++i)
{
for(j = i+1; j<=n; ++j)
if(x[j]> x[i])
{
temp = x[j];
x[j] = x[i];
x[i] = temp;
}
}
printf(“the sorted values\n”);
for(I = 1; i<n; ++i)
ptintf(“%f\n”, x[i]);
}

Note: Arrays in functions.


Arrays can be used as parameters or formal arguments in functions.
In function definitions, the name of the array with an empty bracket is used.
In the function call, rarely the name of the array is used.
When the function is called the address of the actual argument i.e., array is passed on to
the formal argument. This is known as passing the parameter by reference
Therefore, when a function alters an array, the changes will take place in the calling section
of the program.
11

#include<stdio.h>
#define max 50;
void main()
{
float x[max];
int i, n;
void nsort(x, n);
printf(“How many numbers?:”);
scanf(“d”,&n);
for (i=1;i<n; i++)
{
printf(“type in no.%d x value:”, i);
scanf(“%f”,&x[i]);
}
nsort(n, n);
printf(“ sorted numbers\n”);
for(i = 0; i<n; ++i)
printf(“%f\n”, x[i]);
}

void nsort(y, k)
float y[];
int k
{
float temp:
int I, j;
for(I = 0; i<k-10; ++i]
{
for(j = i+1; j<k; ++j)
if(y[j]>y[i])
{
temp = t[j];
y[j] = y[i];
y[i] = temp;
}
Return;
}

Program for displaying names and marks for students in some subject.

You might also like