Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3 Input Output Statements

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

C – PROGRAMMING

Unit: 3
Input/output statements
1.1. Header files
1.2. Formatted input/output
1.3. Character input/output
1.4. Program using input/output
==============================================================

C Header Files

What is a header file?


A header file is a source file that has the .h extension. Header files contain the function prototypes or
function declaration, whereas the source code contains the constants, macros, system-wide global
variables. Whenever we require the definition of a function, then we simply include that header file in
which function is declared.

There are two types of header files defined in a program:

1) System defined header file: The header file which is predefined is known as a system defined header
file.
2) User-defined header file: The header file which is defined by the user is known as a user-defined header
file.

Include Syntax
Both the user and the system header files are included using the preprocessing directive #include. It has
the following two forms −

#include <file>

This form is used for system header files. It searches for a file named 'file' in a standard list of system directories.

#include "file"

This form is used for header files of your own program. It searches for a file named 'file' in the directory containing
the current file.

Some of the header files are given below:


#include<stdio.h>

It is used for performing input and output operations with the help of using printf() and scanf() function.

#include<string.h>

It is used for performing string related functionalities like strlen(), strcmp(), etc.

PAGE \* MERGEFORMAT 1 | Page


C – PROGRAMMING

#include<iostream>

It is used to perform input and output operations with the help of using cin and cout objects.

#include<math.h>

This header file contains some predefined math functions that perform mathematical operations, such as
sqrt(), log2(), pow(), etc.

Formatted input/output
 C provides standard functions scanf() and printf(), for performing formatted input and output .These
functions accept, as parameters, a format specification string and a list of variables.

 printf is used to print the contents that are inside the parenthesis of the printf statement.

 Scanf is used to read and store the input given by a user to a variable. For scanf variable is necessary to
store the information given by the user

The simple program to add two numbers is as follows:


#include<stdio.h>

Voidmain ()
{

int a,b,c;

printf(“enter values for a and b”);

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

C=a+b;

Printf(“the sum is %d”,c);

Here we can see how the printf and scanf function are used in a program

Character Input/output
SINGLE CHARACTER INPUT-- THE getchar FUNCTION

Single characters can be entered into the computer using the C library function getchar. The function does
not require any arguments,though a pair of empty parentheses must follow the word getchar.

Syntax:
character variable= getchar();
PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING

Usage char ch;

Ch=getchar();

SINGLE CHARACTER OUTPUT-- THE putchar FUNCTION

Single character output can be displayed using the C library function putchar. The character being
transmitted will normally be represented as a character- type variable. It must be expressed as an
argument to the function, following the word putchar.

Syntax:
putchar(character variable);

Usage char ch;

………..

putchar(c);

Following program accepts a single character from the keyboard and displays it.
#include<stdio.h>

#include<conio.h>

void main()

char ch;

clrscr();

printf("\n Enter any character of your choice: -");

ch = getchar();

printf ("\n the character u entered was ");

putchar(ch);

getch();

Let's understand the usage of above header files through an example.

// C program to understand the usage of header file.

#include <stdio.h>
#include <string.h>
#include <math.h>
PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING

int main()
{
char str1[10] = "hello";
char str2[10] = "javatpoint";
long int a = pow(3, 3);
printf("The value of a is %d", a);
int length = strlen(str2);
printf("\nThe length of the string str2 is : %d", length);
return 0;
}

In the above code, we have observed the usage of header files like string.h in which strlen() function is defined, and
pow() function is defined in math.h header file.

Output

printf() and scanf() in C


The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt
library functions, defined in stdio.h (header file).

printf() function
The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list);

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function
The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);

Program to print cube of given number

Let's see a simple example of c language that gets input from the user and prints the cube of the given
number.

PAGE \* MERGEFORMAT 1 | Page


C – PROGRAMMING

#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
Output
enter a number:5
cube of number is:125

Program to print sum of 2 numbers


Let's see a simple example of input and output in C language that prints addition of 2 numbers.

#include<stdio.h>
int main()
{
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
Output
enter first number:9
enter second number:9
sum of 2 numbers:18

Program using input/output


#include<stdio.h>

void main()

int i;

printf("Please enter a value...");


PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING

scanf("%d", &i);

printf( "\nYou entered: %d", i);

gets() and puts()


gets()
It reads a string (group of characters) from the standard input and store in the given variable.

It reads a whole line of input until a new line.

Syntax: gets(variable);

Example: char name[25];

gets(name); // it will ask the user for a name and save the name into name.

puts()
This function is used to display text in the monitor which is stored in the variable. But variable is always string data
type.

Syntax: puts(varible); or puts (“string”);

Example:

char name[25]; = { “Ram” };

puts(name);

Write a program to enter full name and print it on screen.


#include<stdio.h>

# include<conio.h>

void main()

char name[100];

puts(“Enter your full name”);

gets(name);

puts(“Your name is : “);

puts(name);

getch();
PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING

Exercise 1
Very short questions:
1) What is header file?
2) How many types of header files?
3) Which preprocessing directive is used in header file?
4) What is the use of #include<stdio.h> header file?
5) Why we use printf() function in C?
6) Write a use of scanf()?
7) Which function is use for single character input?
8) Which function is use for single character output?
9) Write correct syntax of printf() function.
10) Write correct syntax of scanf() function.

Short questions:
1) What is header file? How many types of header files are available in C?
2) How can you take input integer and single character from user?
3) Write any two different header files available in C.
4) Write a simple program to add two numbers take input from user’s keyword.
5) Write two difference between getchar() and putchar() function.
6) Distinguish between scanf() and printf() function.
7) How can you take input single character through keyboard? Write an example.
8) What is the main difference between gets()and puts() functions?
9) Write simple use of gets() and puts() functions.
10) What is gets() function? How it is different from puts() function?

Long questions:
1) What are input/output functions of C language? Explain with examples.
2) Find out square root of a number using C program.
3) Differentiate between printf() and scanf() statements.
4) Enter two numbers and print sum, product, difference and division of numbers using C program.
5) Write a program to input name, age and salary and print them on screen.

Lab Works 1: Input / Output Functions


1) Write a program to find sum of two input numbers.
2) Write a program to find square of a number.
3) Input three numbers and print sum and average using C program.
4) Write a C program to calculate and print simple interest (SI) and net amount (A). [ SI = PTR / 100 and A = SI + P ]
5) Write a program to enter full name and print it on screen.
6) Write a program to enter first letter of your name and print it on screen.
7) Write a program to input two numbers and print remainder and quotient.
8) Write a program to calculate area of a circle.
9) Find out square root of a number using C program.
10) Write a program to enter number of days and convert it into years, months and days.
11) Write a program to display area and perimeter of a rectangle.

PAGE \* MERGEFORMAT 1 | Page


C – PROGRAMMING

12) Write a program to convert temperature in Centigrade (C) into Fahrenheit (F). [ F = 1.8C + 32 ]
13) Write a C program to input name, age and salary and print them on screen.
14) Write a program to calculate circumference of a circle having radius r. the radius r should be taken from user.
[C=2 πr ]
15) Write a C program that asks the name of student and marks obtained by him / her in five subjects. Display the
total marks obtained by student.
16) Write a program to read height and base of a triangle and find area of it.
17) Write a program to read Indian currency (INR) and convert into Nepalese currency (NPR). [ hint: use 100 INR =
160 NPR ]
18) Write a program to read cost price (CP) and selling price (SP) of a mobile for a shop and calculate profit or loss
percentage.
19) Write a C program to read two integers and find quotient value after dividing first integer by second integer.
Comment on the result.
20) Enter two numbers and print sum, product, difference and division of both numbers using C program.

BEST OF LUCK !!!

PAGE \* MERGEFORMAT 1 | Page

You might also like