C Language Basic Chapter 4 Inputs and Outputs, Tayyab8632, Malik8632, 03445064252
C Language Basic Chapter 4 Inputs and Outputs, Tayyab8632, Malik8632, 03445064252
C Language Basic Chapter 4 Inputs and Outputs, Tayyab8632, Malik8632, 03445064252
• scanf function
• gets function
• getch function
• getche function
Similarly, the statements that are used to get data from the program
and send it to an output device are called output statements. The
following functions are used in C to send data to an output device:
• printf function
• puts function
The scanf and printf are most commonly used functions. These are
used to input and output values, respectively, these functions are defined
in the stdio.h header file. This header file must be included in the
preprocessor directive if these functions are to be used in the program. It
is included in the beginning of the program as shown below:
#include <stdio.h>
control_string:
It consists of text, the format specifies and the escape sequence. It
is written within double quotes.
• The text specifies the message that is to be printed along
with the values.
• A format specifier specifies the format according to which a
value is to be printed.
List of arguments:
It consists of a list of variables, constants or arithmetic expressions,
separated by commas, whose values are to be printed.
The values are printed according to the corresponding format
specifier specified in the control string. The format specifier and the
arguments are given such that the first specifier applies to the first
argument, the second specifier to the second argument, and so on.
The arguments in the printf function are optional. When the printf
function is used to print only a message, the arguments are omitted.
For example to print a message on the screen, the printf function is
written as:
printf ("this is a program in C");
Program:
Write a program to print a message on screen by using the printf function.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf ("C++ is based on the C language and");
printf ("both were developed at bell Lab. USA");
getch();
}
Flag Use
+ A plus or minus sign is always used with the signed
output values
- It specifies that the output value is left justified in the
output field. If this flag is not used, the values are
output as right-justified.
# If this flag is used then:
• The octal values are precedent by 0.
• The hexadecimal values are preceded by 0x or
0X.
• The floating point values contain a decimal
point.
Blank It specifies that zero or a positive value is to preceded
by a blank rather than a positive sign.
if the value in the variable ch is B, then the above statement will print:
The value of ch = B
The character 'A' is represented by the ASCII value 65. The conversion
character will first convert the integer value 65 into its equivalent ASCII
code (i.e. A) and then print it:
Program:
Write a program to declare and initialize the values to variables x, y & z
of char type and then print their contents on screen by using printf
function.
#include<stdio.h>
#include<conio.h>
main()
{
char x='D', y='B', z='C';
clrscr ();
printf ("Data in x = %c \n", x);
printf ("Data in y = %c \n", y);
printf ("Data in z = %c \n", z);
getch ();
}
Note: the 'n' has been used in string to print the data of each statement on
a new line.
Output
Data in x = D
Data in y = B
Data in z = C
1 2 3 4 5 6 7 8 9 1 11 1 1 1 1 1 1 1 1 20
0 2 3 4 5 6 7 8 9
S h o r t S t r i n g \0
Q6: Describe how the string type values are printed using
formatted output?
Strings
The conversion character 's' is used in the format specifier to output
string type values using the printf () function.
For example, if name is a string type variable that contains the
name of a person, the string field specifier is used:
If the variable name contains the value "Ahmed Nisar", then the
above printf function will print:
names and city and print these variables on the screen by using the printf
function.
#include<stdio.h>
#include<conio.h>
mian()
{
char name[13] = "Marriam Ahmed";
char city[25] = "Lahore";
clrscr ();
printf ("Name: %s \n", name);
printf ("City: %s \n", city);
getch ();
}
Output:
Name: Marriam Ahmed
City: Lahore
If the values of the variables are a = 10 and b = 20, then the printf
function will print:
A = 10, B = 20
Program
Wire a program to declare and initialize data into an integer type variable
x and print the value of x in:
Decimal format.
The ASCII character of the integer value.
The hexadecimal value of the integer value.
The Octal value of the integer value.
#include<stdio.h>
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
ملک طیب عارف
Chapter 4: Input/Output
#include<conio.h>
main()
{
int x = 97;
clrscr ();
printf ("The decimal value of x = %d \n", x);
printf ("The ASCII value of x = %d \n", x);
printf ("The Hexadecimal value of x = %d \n", x);
printf ("The Octal value of x = %d \n", x);
getch ();
}
Output
The decimal value of x = 97
The ASCII value of x = a
The Hexadecimal value of x = 61
The Octal value of x = 141
Program:
Write a program to declare and initialize data to a variable x of float type
and print its value on the screen using the control characters %f, %e, %g
& %G.
#include<stdio.h>
#include<conio.h>
main()
{
float x = 6714.987696f;
clrscr ();
printf ("Value of x using control character f = %f \n", x);
printf ("Value of x using control character e = %e \n", x);
printf ("Value of x using control character E = %E \n", x);
printf ("Value of x using control character g = %g \n", x);
printf ("Value of x using control character G = %G \n", x);
getch();
}
Output
Value of x using control character f = 6714.987793
Value of x using control character e = 6.714988e+03
Value of x using control character E = 6.714988E+03
Value of x using control character g = 6714.99
Value of x using control character G = 6714.99
In case of the floating type data, the number of decimal places can
also be specified. The syntax for a floating type value is:
w.d
w
specifies the field width including the decimal point and the
fractional part. Its use is optional.
d
specifies the number of decimal places (after the decimal point).
The value is rounded.
For example, to print the value a = 65.566 with only one decimal
place, the printf function is written as:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 6
A= 65.6
Program:
Write a program to declare and initialize data to string type variables
name and city and print their contents on the screen in left-justified and in
right-justified format with field width 30.
#include<stdio.h>
#include<conio.h>
main()
{
char name[25] = "Marriam Ahmed Qureshi";
char city[25] = "Samanabad, Lahore";
clrscr ();
printf ("Left-justified\n");
printf ("%-30s \n", name);
printf ("%-30s \n", city);
printf ("Right-justified\n");
printf ("%30s \n", name);
printf ("%30s \n",city);
getch ();
}
Output
Left-justified
Marriam Ahmed Qureshi
Samanabad, Lahore
Right-justified
Marriam Ahmed Qureshi
Samanabad, Lahore
Program:
Write a program to declare and initialize the data into float type variables
x, y &z and print their values on the screen in left justified as well as in
right justified format having field width of 20 each and with only one
printf ("Right-justified\n");
printf ("%20.1f \n", x);
printf ("%20.1f \n", y);
printf ("%20.1f \n", z);
getch ();
}
Output
Left-justified
71.22.378.0
19.0
7569.0
Right-justified
7122378.0
19.0
7569.0
Program:
Write a program to implement the escape sequence character to print
"Pak" in different forms. Use "\n" in each string to insert a new line.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf ("\aPak\n");
printf ("\"Pak\");
printf ("\'Pak\'\n");
printf ("P\ta\t\k\n");
printf ("\\Pak\\");
getch();
}
Output
Pak
"Pak"
'Pak'
P a k
\Pak\
Program:
Write a program to exchange the values of two variables and to print their
actual and exchanged values.
#include<stdio.h>
#include<conio.h>
main()
{
int a, b, t;
a = 19;
b = 78;
clrscr();
printf("Value of a & b before exchange are %d and %d \n", a, b);
t = a;
a = b;
b = t;
printf ("value of a & b after exchange are %d and %d \n" a, b);
getch ();
}
Output
Value of a & b before exchange are 19 and 78
Value of a & b after exchange are 78 and 19
Program:
Write a program to computer the distance covered by a car in 50 seconds.
The initial velocity is 10m/sec and acceleration is 5 m/sec2. use the
formulas s=vit+1/2at2.
#include<stdio.h>
#include<conio.h>
main()
{
int t, vi, a;
double s;
t = 50;
vi = 10;
a = 5;
s = vi*t+0.5*a*t*t;
clrscr();
printf ("Distance covered = %f m", s);
getch();
}
Output:
Distance covered = 6750.00000 m
Program:
Write a program to input two numbers. Calculate the sum of the numbers
and print on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
int n, m, s;
clrscr ();
printf ("Enter first number?");
scanf ("%d", &n);
printf ("Enter second number?");
scanf ("%d", &m);
s = n+m;
printf ("Sum of %d & %d is = %d", n, m, s);
getch();
}
Output
Enter first number? 12
Enter second number? 23
Sum of 12 & 23 is = 35
Program:
Write a program to input a number. Calculate the cube of the number and
print the result on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
int n, res;
clrscr();
printf ("Enter any number?");
scanf ("%d", &n);
res = n*n*n;
printf ("The cube of %d is = %d", n, res);
getch ();
}
Output:
Enter any number ? 9
The cube of 9 is = 729
Program:
Write a program to input the marks obtained by a student in three
subjects. Calculate the total marks and their average and print the results
on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
float total, s1, s2, s3, avg;
clrscr();
printf ("Enter marks of first subject?");
scanf ("%f", &s1);
printf ("Enter marks of seconf subject?");
scanf ("%f", &s2);
printf ("Enter marks of third subject?");
scanf Function
The scanf function is used to get values into variables from the
keyboard during execution of the program. The value is input 9into a
variable in a specified format. Its syntax is:
control_string:
Specifies the format specifier. It is written within double quotes. A
format specifier specifies the format according to which a value is to be
entered. The control_string in scanf is different from the control string in
printf. Unlike printf control string, strings cannot be given here.
List of variables
Consists of a list of variables, separated by commas, into which the
values are to be entered. In the list of variables, the memory locations of
the variables that correspond to the specifiers in the control string, are
given, the memory location of a variable is indicated with the address
operator. The ampersand (&) sign is used to specify the address operator.
It is placed before the variable name.
For example to input a value from the keyboard into a variable age,
the scanf function is written as:
Values into more than one variables can be input with one scanf
function. The variables may be of different or same data type. For
example, to input values into three integer variables a, b and c, the scanf
function is written as:
• Type value for the first variable and press Enter key. The program
will prompt to get the value for the second variable. Repeat the
process until values in all variables have been entered.
• Type the values of all the variables separated by at least one space
and press Enter key.
Program:
Write a program to input the radius of a sphere. Compute its volume and
surface area (Formula for surface area = 4πR3 and π=3.14).
#include<stdio.h>
#include<conio.h>
main()
{
float R, V, area;
clrscr();
printf ("Enter radius of shphere?");
scanf("%f", &R);
area = 4.0*3.14*R*R;
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
ملک طیب عارف
Chapter 4: Input/Output
V = (4.0/3.0)*3.14*R*R*R;
printf ("Volume of sphere = %f \n", V);
printf ("Area of sphere = %f \n", area);
getch();
}
Output:
Enter radius of sphere? 9.87
Volume of sphere = 4025.500000
Area of sphere = 1223.556274
gets (strvar);
strvar
Represents the string type variable into which data is to be entered.
puts (string);
Program:
Write a program to input a string by using "gets" function and then print it
on the screen by using the "puts" function.
#include<stdio.h>
#include<conio.h>
main()
{
char str[20];
clrscr();
puts ("Enter a string?");
gets(str);
puts("The string is:");
puts(str);
getch();
}
Output
Enter a string?
Marriam Ahmed
The string is:
Marriam Ahmed
[var=] getch();
The use of [var=] is optional. It is used to store the value of the character
entered from the keyboard.
[var =] getche();
The use of [var =] is optional. It is used to store the value of the character
entered from the keyboard.
clrscr ();
Program:
Write a program to clear the screen of the computer monitor.
#include<stdio.h>
main()
{
clrscr ();
}
Where
x:
The x co-ordinate (or column) on the screen. Its value may be from
0 to 79.
y:
The y co-ordinate (or row) on the screen. Its value may be from 0
to 24.
For example, to print output on the screen starting from row 6 and
column 10, the statement are written as:
Program:
Write a program to print "Welcome" in four corners of the screen and
print "Pakistan" at the center of the screen by using the gotoxy function.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr ();
gotoxy (0, 0);
printf ("Welcome");
gotoxy (70, 0);
printf ("Welcome");
gotoxy (0, 24);
printf ("Welcome");
gotoxy (70, 24);
printf ("Welcome");
gotoxy (35, 12);
printf ("Pakistan");
getch ();
}
Program:
Write a program to input the age of a person in years. Convert the age
into months and days and print the result in the center of the screen. (1
year = 365 days approx.)
#include<stdio.h>
#include<conio.h>
main()
{
long int age, mon, days;
clrscr ();
printf ("Enter age in years?");
scanf ("%1d", &age);
mon = age*12;
days = age*365;
gotoxy (35, 12);
printf ("Age in months = %1d", mon);
gotoxy (35, 13);
printf ("Age in days = %1d", days);
getch ();
}
Output:
Enter age in years ? 4
Age in months = 48
Age in days = 1460
Reference:
CM ASLAM
T A QURESHI
ARSHAD JAVED