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

Module 1 Chap3

The document provides an overview of Input and Output (I/O) functions in C, categorizing them into formatted and unformatted I/O functions. It details the usage of functions like scanf() for input and printf() for output, including syntax and examples for various data types. Additionally, it covers specific cases such as reading mixed data types, formatted output specifications, and examples of simple programs using these I/O functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 1 Chap3

The document provides an overview of Input and Output (I/O) functions in C, categorizing them into formatted and unformatted I/O functions. It details the usage of functions like scanf() for input and printf() for output, including syntax and examples for various data types. Additionally, it covers specific cases such as reading mixed data types, formatted output specifications, and examples of simple programs using these I/O functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Module 1

Chapter 3
Input and Output(I/O)
Input/Output (I/O) Functions
I/O functions in C are categorized as:

1. Formatted I/O Functions


E.g. scanf(), printf()

2. Unformatted I/O functions


E.g. getchar(), putchar()
Formatted Input
• Function used for formatted Input: scanf

General format(Syntax):
scanf("control string",arg1,arg2,…,argn);

• Control string(format string): specifies order in


which data to be entered and the type of the data
expected.
• arg1,arg2,…,argn : variables where data is stored.
E.g: scanf(" %c %d %f", &num,&ch,&fno);
Inputting Integers
• General format of control string: %wd
w ==> Specifies width of the data to read
d ==> Indicated we want to read an integer.

❖E.g. scanf("%4d", &num1)


Reads four digits of user entered number

❖E.g: scanf("%d", &num1)


Read maximum possible digits(value) from the
number entered.
#include<stdio.h>
int main()
{
int a;
printf("Enter 4 digit number:");
scanf("%4d”,&a); //should not give white space at the end of control string
printf("The number is: %d",a);
return 0;
}
Output:
Enter 4 digit number:6781
The number is: 6781

#include<stdio.h>
int main()
{
int a;
printf("Enter values:");
scanf("%d", &a); //should not give white space at the end of control string
printf("The number is: %d",a);
return 0;
}
Output:
Enter values:32700
The number is: 32700

#include<stdio.h>
int main()
{
int a,b;
printf("Enter values:");
scanf("%d %d", &a,&b);//
printf("The number is: %d",a);
return 0;
}
Inputting real number: float and
double types
float a,b,c;
scanf("%f %f %f",&a,&b,&c);

For double data type, control string is "%lf"


Formatted Input: char
General format: %c
char intial;
scanf("%c",&intial);
Formatted Input: Strings
General format: %ws
E.g: %s , %5s
char str[10];
scanf("%s",str); // Read till first space or \n found
scanf("%5s",str); // Read first 5 characters

scanf("%[^\n]",str);

// Read until \n is found.


//May not work well with all compilers.
Reading Mixed Data Types
int a;
char b;
float c;
scanf("%d %c %f",&a, &b,&c);
Formatted Output
Most used output function: printf()

❑ General Syntax:
printf(“Control string", arg1,arg2,arg3,…,argn);

❑ E.g.:
int a = 10,b=5,sum;
sum = a+b;
printf("The sum of %d and %d is %d",a,b,sum);

❑ Output:
The sum of 10 and 5 is 15
printf("hello world"); ==> hello world

printf(" "); ==>prints a blank space

printf("\n"); ==>prints a new line

printf("hello\nworld"); ==>hello
world

printf("a=%d \t b=%d",a,b); ==>a=10 b=5


(tab inserted)
Outputting Integers:
• General format specification: %wd
• w==> Specify minimum field width for the
output.
• That many(w) spaces is reserved on screen for
the data displayed.
• “%d” ==>for int
• "%ld" ==> For long int
• "%hd" ==> For short int
Outputting Integers contd..
Output of Real Numbers
General format specifier
%w.pf or %w.pe

w ==> width specification


p ==> precision specification

❑ Default precision is 6.
printf("%f",2.3) ==>2.300000
printf("%.3f",2.3) ==> 2.300
Output of single character
Output of strings
Unformatted I/O
1. Reading a character
This means taking character input from the user.
General Syntax:
variable_name = getchar();
E.g.
char c;
c = getchar();
Program: Reading a character
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
// getchar function waits for user to type a Character
c = getchar();
printf("Character %c is entered",c);
return 0;
}

Output:
Enter a character:A
Character A is entered
Unformatted I/O
2. Writing a character
Outputting a character on to standard output
device

Function: putchar(variable_name);
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character entered is:");
putchar(c);
return 0;
}
Output:
Enter a character:A
Character entered is:A
Area of triangle
#include<stdio.h>
int main()
{
float b ,h, area;
printf(“Enter the height of the triangle”);
scanf(“%f”,&h);
printf(“Enter the breadth of the triangle”);
scanf(“%f”,&b);
area = (b*h) / 2 ;
printf(“Area of Triangle is: %f",area);
return 0;
}
Square of a Number
#include<stdio.h>
int main()
{
int num,square;
printf("Please Enter any integer Value : ");
scanf("%d", &num);
square = num * num;
printf("square of %d is = %d", num, square);
return 0;
}
Simple Interest
# include <stdio.h>
int main()
{
int principal, rate, time, interest;
printf("Enter the principal: ");
scanf("%d", &principal);
printf("Enter the rate: ");
scanf("%d", &rate);
printf("Enter the time: ");
scanf("%d", &time);
interest = principal * rate * time / 100;
Printf(“The Simple interest is %d”, interest);
return 0;
}
Compound Interest
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
printf("Enter principle : ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
CI = principle* (pow((1 + rate / 100), time));
printf("Compound Interest = %f", CI);
return 0;
}
Cube of number
#include <stdio.h>
int main()
{
int n,volumecube;
printf(“Enter a number”);
scanf(“%d”,&n);
volumecube= n*n*n;
printf(“Volume of Cube of %d is %d",n,volumecube);
return 0;
}
Square root of a number
#include <stdio.h>
#include <math.h>
int main()
{
int num, root;
printf("Enter any number to find square root: ");
scanf("%d", &num);
root = sqrt(num);
printf("Square root of %d = %d", num, root);
return 0;
}
Program: Check the type of
character entered
Include header file: #include<ctype.h>
Use functions: isalpha() and isdigit()
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
if(isalpha(c)!=0)
printf("Entered character is a letter.\n");
else if(isdigit(c)!=0)
printf("Entered character is a digit.\n");
else
printf("Entered character is not alpha-numeric\n");
return 0;
}
Output:
Enter a character: A
Entered character is a letter.
Enter a character:9
Entered character is a digit.
Enter a character:#
Entered character is not alpha-numeric

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy