Topic 3 - Inputand Output Functions
Topic 3 - Inputand Output Functions
Example
/* program that accepts a single character and displays it on the screen
#include<stdio.h>
void main()
{
charc1;
c1 = getchar();
putchar(c1);
}
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
The scanf()
This function accepts all types of data from the standard input device.
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 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.
scanf(“%d”,&i);
char code;
int num;
scanf(“%c %d”, &code, &num);
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”
Examples
1. scanf(%3d %3d %3d”,&a,&b,&c);
(a) for the input 123456789
a = 123
b = 456
c = 789
5. scanf(%c%c%c”,&c1,&c2,&c3);
printf()
This function is used to output any type of data
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.
Examples
1. float a = 2.5;
char item[] = “book”;
int I = 257;
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
Example
float a = 25.742;
int n = 123;
6
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
output: 745.25b235.43
3. float x = 745.82;
printf(“%4.0f\n”, x);
output: 745
1. printf(“%12s\n”, line);
2. printf(“%30s\n”, line);
3. printf(“%45s\n”, line);
4. printf(“%7s\n”, line);
5. printf(“%10.7s\n”, line);
Flag characters
They are used to alter the output
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);
}
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);
}
#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
#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]);
}
#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.