You are on page 1of 25

Placement Training – Day 2

(Managing Input Output Operations)


1. MANAGING I/O
I/O operations are useful for a program to interact with users. stdlib is the standard C library
for input-output operations. While dealing with input-output operations in C, two important
streams play their role. These are:
1. Standard Input (stdin)
2. Standard Output (stdout)
Standard input or stdin is used for taking input from devices such as the keyboard as a data
stream. Standard output or stdout is used for giving output to a device such as a monitor. For
using I/O functionality, programmers must include stdio header-file within the program.
a) Reading a character in C
The easiest and simplest of all I/O operations are taking a character as input by reading that
character from standard input (keyboard). The int getchar(void) function reads the next
available character from the screen and returns it as an integer. This function reads only single
character at a time. This function is alternate to scanf() function. The getchar function accepts
any character keyed in. This includes RETURN and TAB.
Syntax:
var_name = getchar();
Example:
#include <stdio.h>
int main()
{
char c;
c=getchar();
printf("%c",c);
printf("\n%d",c);
return 0;
}
Output:
a
a
97

Ramya Devi M | Hindusthan College of Engineering and Technology


b) Writing a character in C
Similar to getchar() there is another function which is used to write characters, but one at a
time. The int putchar(int c) function puts the passed character on the screen and returns the
same character. This function puts only single character at a time.
Syntax:
putchar(variable_name);
Example:
#include <stdio.h>
int main()
{
char c;
c=getchar();
putchar(c);
return 0;
}
Output:
a
a
c) Built-in functions in <ctype.h>

Function Test

isalnum(c) Returns zero if it is neither an alphabet nor a number else returns non-zero
value.

isalpha(c) Returns zero if it is not an alphabet else returns non-zero value.

isdigit(c) Returns zero if it is not a digit else returns non-zero value.

islower(c) Returns zero if it is not an lowercase alphabet else returns non-zero value.

isprint(c) Returns zero if it is not an printable character else returns non-zero value.

ispunct(c) Is c a punctuation mark?

Ramya Devi M | Hindusthan College of Engineering and Technology


Example:
#include <stdio.h>
#include <ctype.h>
int main()
{
char c,d,e;
d='\n';
e='4';
c=getchar();
printf("isalpha(%c) %d",c,isalpha(c));
printf("\nisalpha(%c) %d",d,isalpha(d));
printf("\nisalnum(%c) %d",c,isalnum(c));
printf("\nisalnum(%c) %d",d,isalnum(d));
printf("\nisdigit(%c) %d",e,isdigit(e));
printf("\nislower(%c) %d",c,isdigit(c));
printf("\nisprint(%c) %d",d,isdigit(d));
return 0;
}
Output:
a
isalpha(a) 1024
isalpha(
)0
isalnum(a) 8
isalnum(
)0
isdigit(4) 2048
islower(a) 0
isprint(
)0

Ramya Devi M | Hindusthan College of Engineering and Technology


d) Formatted input
The function scanf() is used for formatted input from standard input and provides many of
the conversion facilities of the function printf(). List of commonly used C data types and
their format specifiers:
Data Type Format Specifier
int %d
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
signed char %c
unsigned char %c
long double %Lf
Syntax:
int scanf(char *format, &arg1, &arg2, …);
The function scnaf() reads and converts characters from the standards inpu t depending on
the format specification string and stores the input in memory locations represented by the
other arguments (num1, num2,….).
Rules for scanf:
➢ Each variable to be read must have a field specification.
➢ For each field specification, there must be a variable address of proper type.
➢ Any non-whitespace character used in the format string must have a matching
character in the user input.
➢ Never end the format string with whitespace. It is a fatal error.
Example:
scanf(“ %c %d”,&Name, &Roll No);

Ramya Devi M | Hindusthan College of Engineering and Technology


Note: the data names are listed as &Name and &Roll No instead of Name and Roll No
respectively. This is how data names are specified in a scnaf() function. In case of string
type data names, the data name is not preceded by the character &.
Example
#include <stdio.h>
int main()
{
int a;
float b;
char ch;
double d;
scanf("%c",&ch);
scanf("%d",&a);
scanf("%f",&b);
scanf("%lf",&d);
printf("a=%d\n",a);
printf("b=%f\n",b);
printf("c=%c\n",ch);
printf("d=%lf",d);
return 0;
}
Output:
r
90
4.5
34.56
a=90
b=4.500000
c=r
d=34.560000
Reading fixed width integers:
The field specifier %wd is used to read fixed width integers. Here w is the width of data need
to be read and store into a particular variable. Any unread data in a line is considered as input
to the next scanf() call. Say for example 65345867 is the input to scanf(“%3d%2d”,&x,&y);

Ramya Devi M | Hindusthan College of Engineering and Technology


then first three digits 653 is fed to x, 45 is fed to y and remaining part 867 is skipped from
reading.

Example:
#include <stdio.h>
int main()
{
int x,y;
scanf("%3d%2d",&x,&y);
printf("%d\n%d",x,y);
return 0;
}
Output:
4589
458
9
Using flag to skip reading in scanf():
An input field can be skipped by placing * in place of field width.
Example:
#include<stdio.h>
int main()
{
int x,y;
scanf("%d%*d%d",&x,&y);
printf("x=%d\ny=%d",x,y);
return 0;
}
Output:
12
23
34
x=12

Ramya Devi M | Hindusthan College of Engineering and Technology


y=34
e) Formatted output
The function printf() is used for formatted output to standard output based on a format
specification. The format specification string, along with the data to be output, are the
parameters to the printf() function.
Syntax:
int printf(char *format, arg1, arg2, …);
In this syntax format is the format specification string. This string contains, for each variable
to be output, a specification beginning with the symbol % followed by a character called the
conversion character.

S. No. Specifier & Output

1 c - Character

2 d or I - Signed decimal integer

3 e - Scientific notation (mantissa/exponent) using e character

4 E - Scientific notation (mantissa/exponent) using E character

5 f - Decimal floating point

6 g - Uses the shorter of %e or %f

7 G - Uses the shorter of %E or %f

8 o - Signed octal

9 s - String of characters

10 u - Unsigned decimal integer

11 x - Unsigned hexadecimal integer

12 X - Unsigned hexadecimal integer (capital letters)

13 p - Pointer address

14 n - Nothing printed

15 % - Character

Example:
#include<stdio.h>

Ramya Devi M | Hindusthan College of Engineering and Technology


int main()
{
int a,b,c,d;
float x,y;
char ch;
a=100;
b=0144;
c=0x64a;
d=0b1100100;
x=5.6;
y=9.3e-2;
ch='a';
printf("a= %d\n",a);
printf("b= %o\n",b);
printf("c= %X\n",c);
printf("d= %d\n",d);
printf("x= %f\n",x);
printf("y= %e\n",y);
printf("ch= %c\n",ch);
printf("db= %lf\n",db);
return 0;
}
Output:
a= 100
b= 144
c= 64A
d= 100
x= 5.600000
y= 9.300000e-02
ch= a
db= 4.500000

Ramya Devi M | Hindusthan College of Engineering and Technology


S. No. Flags & Description

1 -
Left-justify within the given field width; Right justification is the default (see
width sub-specifier).

2 +
Forces to precede the result with a plus or minus sign (+ or -) even for positive
numbers. By default, only negative numbers are preceded with a -ve sign.

3 (space)
If no sign is going to be written, a blank space is inserted before the value.

4 #
Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively
for values different than zero. Used with e, E and f, it forces the written output to
contain a decimal point even if no digits would follow. By default, if no digits
follow, no decimal point is written. Used with g or G the result is the same as with
e or E but trailing zeros are not removed.

5 0
Left-pads the number with zeroes (0) instead of spaces, where padding is
specified (see width sub-specifier).

Example
#include<stdio.h>
int main()
{
int a,b;
float x;
char ch;
double db;
a=88;
b=0xFF12;
x=5.6;
ch='a';
db=4.5;
printf("a= %5d\n",a);
printf("a= %-5d\n",a);
printf("a= %05d\n",a);
printf("b= %#x\n",b);

Ramya Devi M | Hindusthan College of Engineering and Technology


printf("b= %x\n",b);
printf("ch= %-6c\n",ch);
return 0;
}
Output:
a= 88
a= 88
a= 00088
b= 0xff12
b= ff12
ch= a
Example for floating point values:
#include<stdio.h>
int main()
{
float f = 5.4;
printf("%0.2f\n",f);
printf("%5.2f\n",f);
printf("%-5.2f\n",f);
printf("%0.2e\n",f);
printf("%15.2e\n",f);
printf("%-5.2e\n",f);
return 0;
}
Output:
5.40
5.40
5.40
5.40e+00
5.40e+00
5.40e+00

Ramya Devi M | Hindusthan College of Engineering and Technology


f) Unformatted Functions
Unformatted console input/output functions are used to read a single input from the user at
console and it also allows us to display the value in the output to the user at the console.
Some of the most important formatted console input/output functions are:

Functions Description
getch() Reads a single character from the user at the console, without echoing it.
getche() Reads a single character from the user at the console, and echoing it.
Reads a single character from the user at the console, and echoing it, but
getchar()
needs an Enter key to be pressed at the end.
gets() Reads a single string entered by the user at the console.
puts() Displays a single string's value at the console.
putch() Displays a single character value at the console.
putchar() Displays a single character value at the console.
g) Example programs
1) What is the output of this program?
#include <stdio.h>
int main()
{
printf("variable! %d", x);
return 0;
}
Compile time error: It will give compile time error since x is not declared.

2) What is the output of this program?


#include <stdio.h>
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
3: A C program can have same function name and same variable name.

3) What is the output of this program? if input a is 1 and b is 2


#include <stdio.h>

Ramya Devi M | Hindusthan College of Engineering and Technology


int main()
{
int a, b;
printf("%d", scanf("%d %d",&a,&b));
return 0;
}
2: In C, scanf returns the number of inputs it has successfully read.

4) What is the output of this program?


#include <stdio.h>
int main()
{
int a;
a=printf("Example");
printf("%d",a);
return 0;
}
Example7: In C, printf() returns the number of characters successfully written on the
output.

5) What is the output of this program?


#include <stdio.h>
int main()
{
int a;
printf("%d", printf("Example!"));
return 0;
}
Example!8

6) What is the output of this program?


#include <stdio.h>
# define scanf "%s Example for printf and scanf"
int main()

Ramya Devi M | Hindusthan College of Engineering and Technology


{
printf(scanf, scanf);
return 0;
}
%s Example for printf and scanf Example for printf and scanf: printf statement will
become printf("%s Example for printf and scanf", "%s Example for printf and scanf");

7) What is the output of this program?


#include <stdio.h>
int main()
{
printf("%d", main);
return 0;
}
4195638: Gives Address of function main.

8) What is the output of this program?


#include <stdio.h>
int main()
{
int a;
a = 12, 34, 56;
printf("%d",a);
return 0;
}
12: Associativity of comma operator is from left to right, but = operator has higher
precedence than comma operator.Therefore the statement i = 1, 2, 3 is treated as i = 1

9) What is the output of C program?


int main()
{
int a=123;
printf("*%06d*",a);
return 0;

Ramya Devi M | Hindusthan College of Engineering and Technology


}
*000123* : printf("*%06d*",a); prints * then width will be allocated as 6 in which
empty boxes will be filled with 0 then the value at last * will be printed.
* 0 0 0 1 2 3 *

10) What is the output of C program?


int main()
{
int a=6543;
printf("*%5d,*%-5d*",a,a);
return 0;
}
* 6543,*6543 *

2. OPERATORS
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators
➢ Arithmetic operator
➢ Relational operator
➢ Logical operator
➢ Assignment operator
➢ Increment or decrement operator
➢ Conditional operator
➢ Bitwise operator
➢ Special operators
a) Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

Ramya Devi M | Hindusthan College of Engineering and Technology


* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer B%A=0


division.

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9

b) Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then

Operator Description Example

== Checks if the values of two operands are equal or not. If (A == B)


yes, then the condition becomes true. is not true.

!= Checks if the values of two operands are equal or not. If (A != B)


the values are not equal, then the condition becomes true. is true.

> Checks if the value of left operand is greater than the (A > B)
value of right operand. If yes, then the condition becomes is not true.
true.

< Checks if the value of left operand is less than the value of (A < B)
right operand. If yes, then the condition becomes true. is true.

>= Checks if the value of left operand is greater than or equal (A >= B)
to the value of right operand. If yes, then the condition is not true.
becomes true.

<= Checks if the value of left operand is less than or equal to (A <= B)
the value of right operand. If yes, then the condition is true.
becomes true.

c) Logical Operators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then

Operator Description Example

&& Called Logical AND operator. If both the operands are (A && B)
non-zero, then the condition becomes true. is false.

Ramya Devi M | Hindusthan College of Engineering and Technology


|| Called Logical OR Operator. If any of the two operands is (A || B)
non-zero, then the condition becomes true. is true.

! Called Logical NOT Operator. It is used to reverse the !(A && B)


logical state of its operand. If a condition is true, then is true.
Logical NOT operator will make it false.

d) Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

Operator Example Same as

= a= b a= b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Example:
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;

Ramya Devi M | Hindusthan College of Engineering and Technology


printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);
return 0;
}
Output:
c= 5
c = 10
c= 5
c = 25
c= 5
c= 0

e) Increment and Decrement operators:


C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement
-- decreases the value by 1. These two operators are unary operators, meaning they only operate
on a single operand.
Example:
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("a++ = %d \n", a++);
printf("b-- = %d \n", b--);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
printf("c++ = %f \n", c++);
printf("d-- = %f \n", d--);
return 0;
}

Ramya Devi M | Hindusthan College of Engineering and Technology


Output:
++a = 11
--b = 99
a++ = 11
b-- = 99
++c = 11.500000
--d = 99.500000
c++ = 11.500000
d-- = 99.500000

f) Conditional operator (? :)
The conditional operator is also known as a ternary operator. The conditional statements are
the decision-making statements which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'. As conditional operator works on three operands,
so it is also known as the ternary operator. The behavior of the conditional operator is similar
to the 'if-else' statement as 'if-else' statement is also a decision-making statement.
Syntax:
Expression1? expression2: expression3;

Meaning of the above syntax.


➢ In the above syntax, the expression1 is a Boolean condition that can be either true or
false value.
➢ If the expression1 results into a true value, then the expression2 will execute.
➢ The expression2 is said to be true only when it returns a non-zero value.
➢ If the expression1 returns false value then the expression3 will execute.

Ramya Devi M | Hindusthan College of Engineering and Technology


➢ The expression3 is said to be false only when it returns zero value.
Example:
#include <stdio.h>
int main()
{
int age;
scanf("%d",&age);
(age>=18)? (printf("Eligible for voting")) : (printf("Not eligible for voting"));
return 0;
}
Output:
23
Eligible for voting

g) Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and
^ is as follows −

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60
and variable 'B' holds 13, then

Ramya Devi M | Hindusthan College of Engineering and Technology


Operator Description Example

& Binary AND Operator copies a bit to the result if it exists (A & B) = 12,
in both operands. i.e., 0000 1100

| Binary OR Operator copies a bit if it exists in either (A | B) = 61,


operand. i.e., 0011 1101

^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49,


operand but not both. i.e., 0011 0001

~ Binary One's Complement Operator is unary and has the (~A ) = ~(60),
effect of 'flipping' bits. i.e,. -0111101

<< Binary Left Shift Operator. The left operands value is


A << 2 = 240
moved left by the number of bits specified by the right
i.e., 1111 0000
operand.

>> Binary Right Shift Operator. The left operands value is


A >> 2 = 15 i.e.,
moved right by the number of bits specified by the right
0000 1111
operand.

h) Special Operators
Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.

Operator Description Example

sizeof(a), where a is integer, will return


sizeof() Returns the size of a variable.
4.

Returns the address of a &a; returns the actual address of the


&
variable. variable.

* Pointer to a variable. *a;

i) Example programs
1) What is the output of the program?
#include <stdio.h>
int main()
{
int a = 10 + 4.867;
printf("%d",a);
return 0;

Ramya Devi M | Hindusthan College of Engineering and Technology


}
14: a is an int variable. So 10+4.867 = 14.867 is truncated to 14 and assigned to a.
2) What is the output of the program?
#include <stdio.h>
int main()
{
int a = 3.5 + 4.5;
printf("%d",a);
return 0;
}
8: 3.5 + 4.5 = 8.0 is a real number. So it is converted to downgraded to int value. So a = 8.
3) What is the output of the program?
#include <stdio.h>
int main()
{
float a = 3.5 + 4.5;
printf("%f",a);
return 0;
}
8.000000
4) What is the output of the program?
#include <stdio.h>
int main()
{
int a = 5/2;
int b = 5.0/2;
int c = 5 / 2.0;
int d = 5.0/2.0;
printf("%d ",a);
printf("%d ",b);
printf("%d ",c);
printf("%d ",d);
return 0;
}

Ramya Devi M | Hindusthan College of Engineering and Technology


2222
5) What is the output of the program?
#include <stdio.h>
int main()
{
int var = 3.5;;
printf("%f", var);
return 0;
}
0.000000
6) Can we use modulo operator (%) with float and int data types?
Modulo Division operator % in C language can be used only with integer variables or
constants.
7) What is the output of the program?
#include <stdio.h>
int main()
{
int a=0, b=1, c=2, d;
d = a++ || b++ || c++;
printf("%d %d %d %d",a,b,c,d);
return 0;
}
1221
8) What is the output of the program?
#include <stdio.h>
int main()
{
int a = - -9;
printf("%d",a);
return 0;
}
9
9) What is the output of the program?
#include <stdio.h>

Ramya Devi M | Hindusthan College of Engineering and Technology


int main()
{
int a = 27, i, j;
i = a>10;
j=!i;
printf("%d %d %d",a,i,j);
return 0;
}
27 1 0
10) What is the output of the program?
#include <stdio.h>
int main()
{
int a,b,c,d;
a=6;
b=45;
c=a,b;
d=(a,b);
printf("%d %d %d %d",a,b,c,d);
return 0;
}
6 45 6 45: The comma operator evaluates both of its operands and produces the value of the
second. It also has lower precedence than assignment. Hence c = a, b is equivalent to c = a,
while d = (a, b) is equivalent to d = b.
11) What is the output of the program?
#include <stdio.h>
int main()
{
int i = -10;
int k = i % 4;
printf("%d\n", k);
return 0;
}
-2

Ramya Devi M | Hindusthan College of Engineering and Technology


12) What is the output of the program?
#include <stdio.h>
int main()
{
int i = 8;
printf("%d %d %d\n",i,++i,i++);
return 0;
}
10 10 8
13) What is the output of the program?
#include <stdio.h>
int main()
{
int a=14, b=23, c=32, d;
d=(a=c,b+=a,c=a+b+c);
printf("%d %d %d %d",a,b,c,d);
return 0;
}
32 55 119 119
14) What is the output of the program?
#include <stdio.h>
int main()
{
int a=2, b, c;
b=2*(a++);
c=2*(++a);
printf("%d %d %d",a,b,c);
return 0;
}
448
15) What is the output of the program?
#include <stdio.h>
int main()
{

Ramya Devi M | Hindusthan College of Engineering and Technology


int a = 20, b = 15, c = 5;
int d;
d = a == (b + c);
printf("%d", d);
return 0;
}
1

Ramya Devi M | Hindusthan College of Engineering and Technology

You might also like