C Operators & I/O Functions
In this lesson we are going to discuss the following topics:
- ARITHMETIC OPERATORS
- UNARY OPERATORS
- RELATIONAL & LOGICAL OPERATORS
- ASSIGNMENT OPERATORS
- CONDITIONAL OPERATOR
- PRECEDENCE OF OPERATORS
- GETCHAR
- PUTCHAR
- SCANF
- PRINTF & ESCAPE SEQUENCE
- GETS
- PUTS
Learn C Program |
Let's see the brief information
There are 5 arithmetic operators in C. They are:
Operators Use
+ addition
- subtraction
* multiplication
/ division
% remainder after integer division (also known as modulus operator)
Example:
If integer variable x = 10 & y = 3
Expression Value
x + y 13
x - y 7
x * y 30
x / y 3
x % y 1
We are explaining some small program
Program-1
#include<stdio.h>
/* Example of Arithmetic Operators */
void main()
{ int a=2, b=3;
printf("a + b = %d\n",a+b);
printf("a - b = %d\n",a-b);
printf("a * b = %d\n",a*b);
printf("a / b = %d\n",a/b); }
Program-2
#include<stdio.h>
/* Example of Arithmetic Operators */
void main()
{ int a=6, b=12, c=6, avg=(a+b+c)/3;
printf("The Average of %d, %d, And %d Is : %d\n",a,b,c,avg);
}
Program-3
#include<stdio.h>
/* Example of find - Distance covered by a vehicle, if speed & time are given */
void main()
{ int speed=10, time=3, distance=speed*time;
printf("A Vehicle Travelling At %d KPH For %d Hours \n", speed, time);
printf("Will Cover %d Kilometeres, \n", distance); }
Program-4
#include<stdio.h>
/* Example of Priority of the Arithmetic Operators */
void main()
{ int a=5, b=10, c=15, ans=0;
ans = a+b/c;
printf("The Value Of Ans Is : %d\n",ans);
ans = b*c-a;
printf("The Value Of Ans Is : %d\n",ans);
ans = a*b/c;
printf("The Value Of Ans Is : %d\n",ans); ans = (a+b)*b/a;
printf("The Value Of Ans Is : %d\n",ans); }
Program-5
#include<stdio.h>
/* Example of Priority of the Arithmetic Operators */
void main()
{ int a, b=15, c=8, d=3, e=32 f=5;
a = a * c + e / d + f;
printf("The Value Of a = %d\n",a);
a = a * c + e / ( d + f);
printf("The Value Of a = %d\n",a);
a = a * (c + e) / d + f;
printf("The Value Of a = %d\n",a);
a = b * (c + e) / (d + f);
printf("The Value Of a = %d\n",a); }
Example: If floating-point variable x =12.5 & y =2.0
Expression Value
x + y 14.5
x - y 10.5
x * y 25.0
x / y 6.25
Example: If integer variable x =11 & y = -3
Expression Value
x + y 8
x - y 14
x * y -33
x / y -3
x % y 2
Example: If floating-point variable x = -0.66 & y = 4.50
Expression Value
x + y 3.84
x - y -5.16
x * y -2.97
x / y -0.146667
Example:
The algebraic formula The C arithmetic expression
a -[ ( b/ c) x d ] a - b / c * d
( a - b ) / (c x d ) ( a - b ) / (c * d ) i = 8, j = 15 & k = 4 w = 2 * ((i % 5) * (4 + (j-3) / (k+2)));
Evaluation:
w = 2 * ((8 % 5) * (4 + (15 - 3) / (4 + 2)))
w = 2 * (3 * (4 + (12/6)))
w = 2 * (3 * (4 + 2)) or u = i % 5;
w = 2 * (3 * 6) v = 4 + (j -3) / (k + 2);
w = 2 * 18 w = 2 (u * v);
w = 36
Unary Operators
There are 4 type of unary operators:
Operators use
- unary minus
++ increment operator
-- decrement operator
sizeof returns the size of operand
- A unary operators is that, which act upon a single operand to produce a new value.
- The 1st unary operator is unary minus, where a minus sign precedes a numerical constant, a variable or an expression.
- The unary minus operator is different from the arithmetic operator which denotes subtraction ( - ), and requires 2 separate operands.
-743 -0X7FFF -0.2 - (a + b)
- The 2nd unary operator is increment operator ++ , which causes its operand to be increased by one.
- The 3rd unary operator is decrement operator -- , which causes its operand to be decreased by one.
- The operand used with each of these operators must be a single variable.
Example: Expression Meaning New Value of i
if int i = 5; ++i i = i + 1 6
if int i = 5; ++i i = i + 1 4
- The increment and decrement operators can be utilized in 2 different ways, depending on whether the operator is written before or after the operand.
- If the operator precedes the operand ( e.g., ++i), then the operand will be altered in value before it is utilized for its intended purpose.
- If the operator follows the operands ( e.g., ++i), then the value of the operand will be altered after it is utilized.
Program-1
#include<stdio.h>
/* Example of increment, then display */
main()
{ int i =1;
printf("i = %d\n", i);
printf("i = %d\n", ++i);
printf("i = %d\n", i);
}
Program-2
#include<stdio.h>
/* Example of display, then increment */
main()
{ int i =1;
printf("i = %d\n", i);
printf("i = %d\n", ++i);
printf("i = %d\n", i);
}
The 4th unary operator is sizeof operator, which returns the size of its operand, in bytes.
Program-3
#include<stdio.h>
/* Example of sizeof() */
void main()
{ printf("The Size Of char = %d\n" , sizeof(char));
printf("The Size Of int = %d\n" , sizeof(int));
printf("The Size Of short = %d\n" , sizeof(short));
printf("The Size Of float = %d\n" , sizeof(float));
printf("The Size Of long = %d\n" , sizeof(long));
printf("The Size Of double = %d\n" , sizeof(double));
}
Program-4
#include<stdio.h>
/* Example of sizeof an array */
main()
{ char a1[ ] = "Computer";
printf("Number of characters = %d\n" , sizeof(a1));
}
Unary operators have a higher precedence than arithmetic operators.
Example:
int x = 10, y =20;
The value of - x + y will be - 10 + 20 = 10
But The value of -(x + y) will be -(10 + 20) = 30
Relational & Logical Operators
Operators Use
< less than
<= less than or equal to
> greater than
>= greater than or equal to
= = equal to
!= not equal to
&& and
|| or
! not
- These operators are used to form logical expressions representing conditions that are either true (int 1) or false (int 0)
int a = 1, b = 2, c=3;
Expression Meaning Value
a < b true 1
(a+b) >=c true 1
(a+b) >=c true 1
(b+c) > (a+5) false 0
c != 3 false 0
b = = 2 true 1
Example:
int i = 7;
float f = 5.5;
char c = 'w;
Expression Meaning Value
(i >= 6) && (c = = 'w') true 1
(i >= 6) || (c = = 119) false 0
(i < 11) && (i > 100) false 0
(c != 'p') || ((i + f) <= 10) true 1 Example:
int i = 7;
float f = 5.5;
Expression Meaning Value
f > 5 true 1
!(f > 5) false 0
i <=3 false 0
!(i <= 3) true 1 i > (f + 1) true 1
!(i > (f +1)) false 0 Assignment Operators
There are 6 assignment operators in C :
Operators use
= assign the value of an expression to an identifier
+= add and then assign
-= subtract and then assign
+= multiply and then assign
/= divide and then assign
%= evaluate remainder and then assign
Example:
int i ;
int j =5;
Expression Value
i = 3.3 3
i = 3.4 -3
i = j 5
i = j /2 2 i = 2 * (j / 2) 4
i = 'x' 120 i = '0' 48
Example:
Conditional Operator
int i ; i = j =5
int j ; Value 5 will be assigned to both i & j
Example :
int i = 5;
int j = 7;
float f = 5.5;
float g = -3.25;
Expression Meaning Value
i += 5 i = i + 5 10
f -= g f = f - g 8.75
j *= (i-3) j = j * (i-3) 14
f / =3 f = f / 3 1.833333
i %= (j -2) i = i % (j -2) 0
Program-1
#include<stdio.h>
/* Example on Assignment operator with after/before increment */
void main()
{ int a = 8, b=2, sum1;
int a = 8, b=2, sum1;
sum1= a+(++b); /* Increment Then Add */
printf("sum1 = %d\n" , sum1);
sum2=x+(y++); /* Add Then Increment */
printf("sum2 = %d\n", sum2);
}
- It can be used instead of if -else
- A conditional operator is written in the form:
expression1 ? expression2 : expression3
Evaluate:
If expression1 is true, expression2 is evaluated
If expression1 is false, expression3 is evaluated
Only one of the either expression2 or expression3 is evaluated when determining the value of a conditional expression.
Example:
flag = (i < 0) ? 0 : 100;
If the value of i is negative, then 0 will be assigned to flag
If the value of i is not negative, then 100 will be assigned to flag
Program-1
#include<stdio.h>
/* Example of ? : operator */
void main()
{ int num;
printf("\nEnter A Number :");
scanf( %d", &num);
sum2=x+(y++); /* Add Then Increment */
(num % 2) ? printf("\nYou Entered ODD"):printf("\nYou Entered EVEN");
} Precedence of Operators
Operator names Operator Evaluation
unary operators - ++ -- | sizeof R to L
arithmetic multiply, * / % L to R
divide & remainder
arithmetic add and + - L to R
subtract
relational operator < <= > .= L to R
equality operators = = | = L to R
logical and && L to R
logical or || L to R
conditional operator ? : R to L
assignment operator = += -= *= /+ %= R to L
Example:
int a, b, c;
c +=(a > 0 && a<=10) ? ++a : a/b;
Is evaluated as follows:
- if ( a > 0 && a<= 10) is true ++a is evaluated
- if ( a > 0 && a<= 10) is false a/b is evaluated
- the value of c is incremented (+=) by the value of conditional expression.
Program-1
#include<stdio.h>
/* Example of = =, !=, ? : , &&, and || */
void main()
{ int year;
printf("\nEnter A Year :");
scanf( %d", &year);
(year % 4 = = 0 && year % 100 !=0 || year % 400 = = 0)
? printf("\nLeap Year") : printf("\nNot A Leap Year");
} I/O Functions:
- An input/output function can be accessed from anywhere within a program simply by writing the function name, followed by a list of arguments enclosed in parentheses.
- The argument represent data items that are sent to the function.
- Some I/O functions do not require arguments, but parentheses must appear.
- Functions that return data items may appear within expression.
- Functions that do not return data items are separate statements.
- The hear file required by the standard I/O library functions is called stdio.h
- This file is entered into the program by an #include statement at the beginning of the program.
getchar( )
Purpose:
- Enter a single character from the standard input device.
- It returns a single character from a standard input device ( mainly a keyboard).
- This function does not require any arguments.
character variable = getchar ();
Program-1
#include<stdio.h>
/* Get a character from the user and display it back| */
void main()
{ char c;
c = getchar( );
printf("C contains : %s", c);
} Program-2
#include<stdio.h>
/* Get a character from the user and display it's ASCII Code */
void main()
{ char aski;
printf("\nEnter A Character : ");
aski = getchar( );
printf("\nThe ASCII Code For %s Is %d", aski,aski);
} Putchar
Purpose:
- Sends a single character to the standard output device
- It transmits a single character to a standard output device (mainly a Monitor)
- The character being transmitted, should be represented as a character-type variable.
- It must be expressed as an argument to the function.
Usage: putchar (character variable)
Program-1
#include<stdio.h>
/* Display a character */
void main()
{ putchar('A');
}
Program-2
#include<stdio.h>
/* Get a character from the user and display it back */
main()
{ char c;
c = getchar( );
putchar(c);
}
Program-2
#include<stdio.h>
/* Convert text to upper-case, and display */
main()
{ char text[80];
int counter, flag;
for(counter = 0; (text[counter] = getchar( )) ! = '\n'; ++counter);
flag = counter;
for(counter = 0; counter < flag; ++counter)
putchar(toupper(text[counter]));
}
- The 1st for statement creates a loop which causes the individual characters to be read into the computer and assigned to the array text[ ]
- The loop begins with a value of counter equal to zero.
- A character is then read into the computer from the standard input device and assigned to text[0]
- The value of counter is incremented.
- The process is repeated for the nest array elements till a newline character '\n' is encountered.
- The value of counter is assigned to flag.
- The 2nd 'for' displays the contents of text on the screen after converting them to upper-case.
scanf()
Purpose:
- Enter data items from the standard input device
- This function can be used to enter any combination of numerical values, singlecharacters and strings.
- The function returns the number of data items that have been entered.
Usage:
scanf(control string, arg1, arg2, .....,argn)
- control-string refers to a string containing certain required formating information.
- arg1, arg2,..., argn are arguments that represent the individual input data items.
Explanation:
- The control string comprises individual groups of characters, with one character group for each input data item.
- Each character group must begin with a percent sign ( % )
- A single character group will consist of the percent sign, followed by a conversion character which indicates the type of the corresponding data item.
- Table of conversion character for Data Input:
Conversion
Character Meaning
%c Single character
%d Decimal integer
%e Floating-point value
%f Floating-point value
%g Floating-point value
%h Short integer
%i Decimal, hexadeciamal or octal integer
%o Octal integer
%s String of character
%u Unsigned decimal integer
%x Hexadecimal integer
A prefix may precede certain conversion characters
Prefix Meaning
h short integer or short unsigned integer
l long integer, long unsigned integer or double
L long double
- Each variable name must be preceded by an ampersand (&)
- The arguments are actually pointers which indicate where the data items are stored in the computer's memory.
- array names should not begin with an ampersand.
Program-1
#include<stdio.h>
/* Example of scanf( ) using conversion character and ampersand character */
void main()
{ char text[20];
int partno;
float cost;
scanf("%s %d %f", text, &partno, &cost);
printf("The Value of text is : %s\n",text);
printf("The Value of partno is : %d\n",partno);
printf("The Value of partno is : %d\n",partno);
printf("The Value of cost is : %f\n",cost);
}
- FIELD means consecutive nonwhitespace characters that compose a data item collectively.
- The number of such character for a data item can be limited by specifying maximum field width.
- Unsigned integer indicating the field width should be placed between the percent sign ( % ) and the conversion character.
- The leftover character that extend beyond the specified field width will be ignored, or may be incorrectly interpreted as the components of the next data item.
Program-1
#include<stdio.h>
/* Example of scanf( ) with limited field width */
void main()
{ int a, b, c;
scanf("%3d, %3d, %3d, &a, &b, &c);
printf("a = %d \t b = %d \t c = %d \n", a,b,c);
}
Run 1:
User's Input : 1 2 3
Program's Output : a = 1 b = 2 c = 3
Run 2:
User's Input : 123 456 789
Program's Output : a = 123 b = 456 c = 789
Run 3:
User's Input : 123456789
Program's Output : a = 123 b = 456 c = 789
Run 4:
User's Input : 1234 5678 9
Program's Output : a = 123 b = 4 c = 567
printf( )
Purpose:
- Send data items to the standard output device.
- This function can be used to output any combination of numerical values, single characters and strings.
- This function moves data from the computer's memory to the standard output device.
Thank you for visit my site. If you find this article informative, please share the post to your friends.
******************
Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).
- Learn how to write the Turbo C program step-by-step | Lesson -1
- Important Terminology of Computer Science
- Revolution in Information Technology
- How to delete WhatsApp data from Google drive
- Most useful website list for job seekers
- Transfer data from PC to phone | now you can UNDO Gmail's sent mail on smartphone too
- Do you know how to delete Facebook account
- What is TCP/IP Reference Model
- How to get the right results in Google search
- 14 Way to make money online
Thanks a lot for reading.
Neel Kamal
Learn how to write the Turbo C program step-by-step | Lesson -2
Reviewed by Neel Kamal
on
October 31, 2020
Rating:
No comments:
For More Details Subscribe & Comment..!