Week 2_Final
Week 2_Final
This is a sample program which depicts the use of each operators and how are
they being used
If we consider an expression, a * b / c + d then ‘*’ and ‘/’ has the same
precedence, but multiplication is performed first and then division will be
performed because the associativity is from left to a right after performing
multiplication and then division. And addition will be performed onto the whole
result of a * b / c.
Let us consider a small program to demonstrate how to use division operator
and modulus operator.
#include main()
{
int months , days ;
printf ( "Enter days \n “ ) ;
scanf ( "%d“ , &days ) ;
months = days / 30 ;
days = days % 30 ;
printf ( "Months =%d Days= %d \n " , months, days);
}
The first line is a declaration statement where we are declaring two variables’
months and days of type integer, so months and days will store integer values in
them. The second printf statement which displays the statement, enter days onto
the output console where users can enter a value. The value that is entered by
the user will be read through the scanf statement where the value that user
enters will be stored in the variable days. In the next statement, we are dividing
the value of the variable days by 30 by using a division operator. Now this
expression will give the quotient and that quotient will be assigned to the
variable months. In the next statement we are using a modules operator. days
value will be divided by 30 and remainder value will be assigned to days
variable again. The last statement simply displays the value of months and days.
Relational operators:
These operators are used to compare quantities and ascertain the relationship
between them. The outcome of the relational expression is either one or zero. It
is one if the specified relation holds true and zero if the relation is false. These
operators are termed binary operators as they require two expressions as
operands.
Operator Details
AND (&&) If both A and B are non-zero, then the condition becomes
TRUE
A=1 && B=1 1
A=1 && B=0 0
OR (||) If any of the A or B is non-zero, then the condition
becomes TRUE
A=0 || B=1 1
A=0 || B=0 0
NOT (!) It reverses logical state of the operand. If a condition is
TRUE, then NOT operator will make it FALSE
−b ± b 2 − 4ac
If we consider an expression say . This is the way to write
2a
an expression manually, when we're using any mathematical function or
mathematical expression, we use division operator. we use a root symbol. But if
we right the expression in the same way in program, the compiler will not
understand because it uses certain mathematical functions. To make sure the
compiler understands the expression, we have to write it in certain way.
For example, if you say 2a it is understood, that it is 2*a. but here to make sure
the compiler understands you have to specify 2 is multiplied by a by using
multiplication operator, so the whole expression can be expressed
programmatically as = ( -b + sqrt ( b*b - 4*a*c ) ) / ( 2 * a ) .
Function Purpose
toupper(c) Convert letter to uppercase
tolower(c) Convert letter to lowercase
tan(d) Return the tangent of d
log(d) Return the natural logarithmic of d
ceil(d) Round up to next integer value
floor(d) Round down to next integer value
Table 3: Few examples of library functions
Table 3 lists some of the library functions, which we mostly use. Not all the
library functions are mentioned, but few of the library functions which can be
invoked, for example, toupper() is a function that we invoke to convert a
character or a letter in to an uppercase value. We can also use in the same way
tolower() function. to get tangent value a tan function is used, to get a
logarithmic value, we use log function. We can also get ceiling and floor values
by using ceil and floor functions.
Program to Convert a Lowercase to Uppercase
#include<stdio.h>
#include <ctype.h>
main()
{
int lower, upper;
lower = getchar();
upper = toupper(lower);
putchar(upper);
}
This is a simple program to demonstrate how to include library functions in the
program. This program is basically converting a lower case character in to
uppercase character.
Program showing the use of Library Functions
#include<stdio.h>
#include<ctype.h>
#include<math.h>
main()
{
int i = -10, e = 2, d =10;
float rad = 1.57;
double d1 = 2.0, d2 = 3.0;
printf(“%d\n”, abs(i));
printf(“%f\n”, exp(rad));
printf(“%f\n”, pow(d1,d2));
printf(“%f\n”, sin(rad));
printf(“%f\n”, cos(rad));
printf(“%d\n”, log(rad));
}
In this program the abs(i) function returns the absolute value of an integer, sin()
computes the sine of an angle given in radians here rad = 1.57 is approximately
π/2 radians, so sin(rad) will be close to 1. Similarly cos(rad) will be 0.00,
exp(rad) will be 4.811 approximately, log(rad) will be .451 approximately, and
pow(d1,d2) will give the output 8.
Bit-wise Operators
As we know the values are considered by the machine as a binary values, the
stored values will be converted to a binary number always. And then, to perform
operations at the bit level, we use a bit wise operators. We have various bit wise
operators.
Operator Description
& To perform bit-wise AND operation
| To perform bit-wise OR operation
~ To perform bit-wise NOT operation
^ To perform bit-wise XOR operation
<< To perform bit-wise left shift
>> To perform bit-wise right shift
Table 4: Bit wise operators
Comma Operator
We also have a special operator which is called as a common operator. We use
comma operator to link the related expressions together. For example, if we
have to declare two integer variables and we have to assign the values also at
the same time. To do it in a single statement we can write it as int a=10,b=20. If
suppose the statement is Value = ( X = 10, Y = 5, X + Y);
Then following the associativity rule of left to right and precedency rule the
expression will be evaluated as:
First assigns the value 10 to X,
Then assigns 5 to Y,
X + Y = 10 + 5 = 15
And finally assigns 15 to Value.
References:
1. Book Let Us C, by Yashavant Kanetkar
2. Book Programming in ANSI C, by E. Balagurusamy
3. SLM of Manipal University Jaipur
4. Use of AI tools for language and syntax corrections.