Day 2 - (Managing IO and Operators)
Day 2 - (Managing IO and Operators)
Day 2 - (Managing IO and Operators)
Function Test
isalnum(c) Returns zero if it is neither an alphabet nor a number 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.
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
1 c - Character
8 o - Signed octal
9 s - String of characters
13 p - Pointer address
14 n - Nothing printed
15 % - Character
Example:
#include<stdio.h>
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);
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. 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
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
> 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
&& Called Logical AND operator. If both the operands are (A && B)
non-zero, then the condition becomes true. is false.
d) Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
= 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;
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;
g) Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and
^ is as follows −
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
& Binary AND Operator copies a bit to the result if it exists (A & B) = 12,
in both operands. i.e., 0000 1100
~ Binary One's Complement Operator is unary and has the (~A ) = ~(60),
effect of 'flipping' bits. i.e,. -0111101
h) Special Operators
Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.
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;