[go: up one dir, main page]

0% found this document useful (0 votes)
18 views18 pages

C Language

The document provides an overview of operators in the C programming language, categorizing them into groups such as arithmetic, relational, logical, sizeof, conditional, and assignment operators. It includes examples and explanations of how each operator functions, along with sample code demonstrating their usage. Additionally, it highlights the behavior of operators with different data types and the implications of using them in expressions.

Uploaded by

Tej Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views18 pages

C Language

The document provides an overview of operators in the C programming language, categorizing them into groups such as arithmetic, relational, logical, sizeof, conditional, and assignment operators. It includes examples and explanations of how each operator functions, along with sample code demonstrating their usage. Additionally, it highlights the behavior of operators with different data types and the implications of using them in expressions.

Uploaded by

Tej Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

FUNDAMENTALS OF COMPUTER SCIENCE

OPERATORS IN C

• An operator is a symbol that specifies the mathematical, logical or relational operation


to be performed.
• C language supports different types of operators, which can be used with variables
and constants to form expressions.
• These operators can be categorized into the following major groups.
• comma operator
• unary operators
• pre and post increment and decrement operators
• arithmetic operators
• relational operators
• logical operators
• sizeof operator
• conditional operators
• assignment operators
• bitwise operators
ARITHMETIC OPERATORS

An arithmetic operator performs mathematical operations such as addition, subtraction,


multiplication, division etc on numerical values (constants and variables).

Operator Description Example


+ Addition Adds values on either side of the operator. a + b = 30
- Subtraction Subtracts right hand operand from left hand a – b = -10
operand.
* Multiplies values on either side of the operator a * b = 200
Multiplication
/ Division Divides left hand operand by right hand operand b / a = 2
% Modulus Divides left hand operand by right hand operand b % a = 0
and returns remainder.
while performing modulo division, the sign of
the result is the sign of first operand.

16 % 3 = 1
-16 % 3 = -1
-16 % -3 = -1

Example 1: Arithmetic Operators


// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
The operators +, - and * computes addition, subtraction, and multiplication respectively as
you might have expected.

In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.


It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.

The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder
is 1. The % operator can only be used with integers.
Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,

// Either one of the operands is a floating-point number


a/b = 2.5
a/d = 2.5
c/b = 2.5

// Both operands are integers


c/d = 2

C RELATIONAL OPERATORS

A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.

Operato Description Example


r
== If the values of two operands are equal, then the (a == b) is not true.
condition becomes true.
!= If values of two operands are not equal, then condition (a != b) is true.
becomes true.
> If the value of left operand is greater than the value of (a > b) is not true.
right operand, then condition becomes true.
< If the value of left operand is less than the value of right (a < b) is true.
operand, then condition becomes true.
>= If the value of left operand is greater than or equal to the (a >= b) is not true.
value of right operand, then condition becomes true.
<= If the value of left operand is less than or equal to the (a <= b) is true.
value of right operand, then condition becomes true.
example
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}

Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

C LOGICAL OPERATORS

An expression containing logical operator returns either 0 or 1 depending upon whether


expression results true or false. Logical operators are commonly used in decision making in C
programming.

Operator Meaning Example

Logical AND. True only if all If c = 5 and d = 2 then, expression


&&
operands are true ((c==5) && (d>5)) equals to 0.

|| Logical OR. True only if either If c = 5 and d = 2 then, expression


Operator Meaning Example

one operand is true ((c==5) || (d>5)) equals to 1.

Logical NOT. True only if the If c = 5 then, expression !(c= =5)


!
operand is 0 equals to 0.

Example : Logical Operators


// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}

Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

Explanation of logical operator program


 (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1
(true).
 (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
 (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
 (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
 !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1
(true).
 !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

sizeof operator

The sizeof operator is the most common operator in C. It is a compile-time unary operator
and used to compute the size of its operand. It returns the size of a variable. It can be applied
to any data type, float type, pointer type variables. When sizeof() is used with the data types,
it simply returns the amount of memory allocated to that data type. The output can be
different on different machines like a 32-bit system can show different output while a 64-bit
system can show different of same data types.

Example: sizeof Operator


#include <stdio.h>
int main() {
int a = 16;
printf("Size of variable a : %d\n",sizeof(a));
printf("Size of int data type : %d\n",sizeof(int));
printf("Size of char data type : %d\n",sizeof(char));
printf("Size of float data type : %d\n",sizeof(float));
printf("Size of double data type : %d\n",sizeof(double));
return 0;
}

Output
Size of variable a : 4
Size of int data type : 4
Size of char data type : 1
Size of float data type : 4
Size of double data type : 8
When the sizeof() is used with an expression, it returns the size of the expression. Here is an
example.

Example

#include <stdio.h>
int main() {
char a = 'S';
double b = 4.65;
printf("Size of variable a : %d\n",sizeof(a));
printf("Size of an expression : %d\n",sizeof(a+b));
int s = (int)(a+b);
printf("Size of explicitly converted expression : %d\n",sizeof(s));
return 0;
}
Output
Size of variable a : 1
Size of an expression : 8
Size of explicitly converted expression : 4

Example
#include<stdio.h>
int main()
{
char text[]="California";
printf("number of characters in the text =%d", sizeof text);
return 0;
}

Output
number of characters in the text =11

CONDITIONAL OPERATOR IN C

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 of a conditional operator


1. Expression1? expression2: expression3;

The pictorial representation of the above syntax is shown below:


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

Let's understand the ternary or conditional operator through an example.


#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditi
onal operator
return 0;
}

OUTPUT:

If we provide the age of user above 18, then the output would be:

Now, we will see how a conditional operator is used to assign the value to a variable.
Let's understand this scenario through an example.
#include <stdio.h>
int main()
{
int a=5,b; // variable declaration
b=((a==5)?(3):(2)); // conditional operator
printf("The value of 'b' variable is : %d",b);
return 0;
}
In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the 'a'
variable. After the declaration, we are assigning value to the 'b' variable by using the
conditional operator. If the value of 'a' is equal to 5 then 'b' is assigned with a 3 value
otherwise 2.

Output

ASSIGNMENT OPERATORS
The assignment operator ( = ) is used to assign a value to the variable. The assignment
operator is responsible for assigning values to variables. While the equal sign(=) is the
fundamental assignment operators, the C language also supports other assignment operators
that provide shorthand ways to represent common variable assignments

Its general format is as follows:

variable = right_side

The operand on the left side of the assignment operator must be a variable and operand on the
right-hand side must be a constant, variable or expression. Here are some examples:

x = 18 // right operand is a constant


y = x // right operand is a variable
z = 1 * 12 + x
The precedence of the assignment operator is lower than all the operators and it
associates from right to left.

We can also assign the same value to multiple variables at once.

x = y = z = 100 . //here x, y and z are initialized to 100.


Since the associativity of the assignment operator ( = ) is from right to left. The above
expression is equivalent to the following: x = (y = (z = 100))

Note that expressions like:

x = 18
y=x
z = 1 * 12 + x

are called assignment expression. If we put a semicolon(;) at the end of the expression like
this:

x = 18;
y = x;
z = 1 * 12 + x;

then the assignment expression becomes assignment statement.

Operator Example Same as

= a=b a=b

a += b . It adds the right operand to the left


+= operand and assign the result to the left a = a+b
operand.

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b

Example: Assignment Operators


// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5

printf("c = %d\n", c);


c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}

Output
c=5
c = 10
c=5
c = 25
c=5
c=0

The following program demonstrates Compound assignment operators in action:


#include<stdio.h>
int main(void)
{
int i = 10;
char a = 'd';
printf("ASCII value of %c is %d\n", a, a); // print ASCII value of d

a += 10; // increment a by 10;


printf("ASCII value of %c is %d\n", a, a); // print ASCII value of n

a *= 5; // multiple a by 5;
printf("a = %d\n", a);

a /= 4; // divide a by 4;
printf("a = %d\n", a);

a %= 2; // remainder of a % 2;
printf("a = %d\n", a);

a *= a + i; // is equivalent to a = a * (a + i)
printf("a = %d\n", a);

return 0; // return 0 to operating system


}
Output:
ASCII value of d is 100
ASCII value of n is 110
a = 38
a=9
a=1
a = 11

COMMA OPERATOR

What is Comma Operator?


The comma operator is used to separate two or more expression. Where first
expression1 is evaluated, then expression2 is evaluated, and the value of
expression2 is returned for the whole expression.

comma.c

#include <stdio.h> //header file section


int main() //main section
{
int a=5, b=10;
if(a>b,a<b)
printf("if condition executes");
else
printf("else condition executes");
return 0;
}

Comma Operator In printf()


Here is an example program where comma operator get used in printf() statement.

commaprintf.c

#include <stdio.h> //header file section


int main() //main section
{
printf("Addition of 4 + 4 is %d ", 4+4);
return 0;
}

output
Addition of 4 + 4 is 8

Comma Operator in scanf()


Here is an example program where comma operator get used in scanf() statement.

commascanf.c

#include <stdio.h> //header file section


int main() //main section
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
printf("you entered %d", num);
return 0;
}

output
Enter a number: 2
you entered 2

The comma operator can be used to link the related expressions together.
A comma-linked list of evaluated left to right and he value of right-most expression is the
value of the combined expression.

For example
inti = (5, 10); /* 10 is assigned to i*/
int j = (f1(), f2()); /* f1() is called (evaluated) first followed by f2().
The returned value of f2() is assigned to j */

Some applications of comma operator are:

In for some loops:


(n = 1, m = 10, n <=m; n++, m++)
In while loops:
while (c = getchar(), c != ‘10’)

Exchanging values :
t = x, x =y, y =t;

INCREMENT AND DECREMENT OPERATORS


 Increment operators are used to increase the value of the variable by one and
decrement operators are used to decrease the value of the variable by one in C
programs.
 Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
 Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;

Types of Increment and Decrement Operators in C


Following types of increment and decrement operators are found in the C language:
 Pre Increment operator
 Pre Decrement operator
 Post Increment operator
 Post Decrement operator
Both the operators vary a lot in their fundamental working. However, we can only use these
with variables, and not with expressions or constants..

Increment Operators
We use increment operators in C to increment the given value of a variable by 1.
For instance,

int a = 1, b = 1;
++b; // valid
++3; // invalid – increment operator is operating on constant value
++(a+b); // invalid – increment operator is operating on expression

Pre Increment Operators


The prefix increment operator increments the current value of the available variable
immediately, and only then this value is used in an expression. In simpler words, the value of
a variable first gets incremented and then used inside any expression in the case of an
increment operation.

b = ++a;

In this case, the current value of a will be incremented first by 1. Then the new value will be
assigned to b for the expression in which it is used.

example,
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q;
i=10;
p=++q;
printf("p: %d",p);
printf("q: %d",q);
getch();
}

OUTPUT
p: 11
q: 11

Post Increment Operators


The postfix increment operator allows the usage of the current value of a variable in an
expression and then increments its value by 1. In simpler words, the value of a variable is
first used inside the provided expression, and then its value gets incremented by 1.

For example:
b = a++;

In this case, the current value of a is first assigned to b, and after that, a is incremented.

EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
b=10;
a=b++;
printf("a: %d",a);
printf("b: %d"”,b);
getch();
}

OUTPUT
a: 10
b: 11

Decrement Operators
We use decrement operators in C to decrement the given value of a variable by 1.
For instance,
int a = 2, b = 1;
–b; // valid
–5; // invalid – decrement operator is operating on constant value
–(a-b); // invalid – decrement operator is operating on expression

Prefix Decrement Operator


The prefix decrement operator decrements the current value of the available variable
immediately, and only then this value is used in an expression. In simpler words, the value of
a variable first gets decremented and then used inside any expression in the case of a
decrement operation.
b = - -a;
In this case, the current value of a will be decremented first by 1. Then the new value will be
assigned to b for the expression in which it is used.

EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q;
i=10;
p= --q;
printf("p: %d",p);
printf("q: %d",q);
getch();
}

OUTPUT
p: 9
q: 9

Postfix Decrement Operator

The postfix decrement operator allows the usage of the current value of a variable in an
expression and then decrements its value by 1. In simpler words, the value of a variable is
first used inside the provided expression, and then its value gets decremented by 1. For
example:
b = a- - ;
In this case, the current value of a is first assigned to b, and after that, a is decremented.

EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
b=10;
a=b–;
printf(“a: %d”,a);
printf(“b: %d”,b);
getch();
}

OUTPUT
a: 10
b: 9
BITWISE AND SIZEOF OPERATOR

C provides operators that let you perform logical operations on the individual bits of integer
values, and shift the bits right or left.

The bitwise operators are:

&(bitwise AND) : Bitwise AND is a binary operator and requires two operands. When we
use the bitwise AND operator, the bit in the first operand is ANDed with the corresponding
bit in the second operand. The result of AND is 1 only when the bits in both operands are 1,
otherwise it is 0.

|(bitwise OR) : Bitwise OR is a binary operator and requires two operands. When we use the
bitwise OR operator, the bit in the first operand is ORed with the corresponding bit in the
second operand. The result of OR is 0 only when the bits in both operands are 0, otherwise it
is 1.

^(bitwise XOR) : Bitwise XOR is a binary operator and requires two operands. When we use
the bitwise XOR operator, the bit in the first operand is XORed with the corresponding bit in
the second operand. The result of XOR is 1, if bits of both operands have different value,
otherwise it is 0.

<<(bitwise left shift) : Bitwise left shift operator is used for shifting the bits left. It requires
two operands; the left operand is the operand whose bits are shifted and the right operand
indicates the number of bits to be shifted.

>> (bitwise right shift) : Bitwise right shift operator is used for shifting the bits right. It
requires two operands; the left operand is the operand whose bits are shifted and the right
operand indicates the number of bits to be shifted.

~ (bitwise NOT) : Bitwise NOT (One's complement) operator is an unary operator (works
on only one operand). It changes 1 to 0 and 0 to 1.
The following program demonstrates bitwise operators:

#include <stdio.h>
int main(void)
{
unsigned int a = 4, b = 5; /* a = 4(00000100), b = 5(00000101) */
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b); /* The result is 00000100 */
printf("a|b = %d\n", a|b); /* The result is 00000101 */
printf("a^b = %d\n", a^b); /* The result is 00000001 */
printf("~a = %d\n", a = ~a); /* The result is 11111011 */
printf("b<<1 = %d\n", b<<1); /* The result is 00001010 */
printf("b>>1 = %d\n", b>>1); /* The result is 00000010 */
return 0;
}

Output:
a = 4, b = 5
a&b = 4
a|b = 5
a^b = 1
~a = -5
b<<1 = 10
b>>1 = 2

The bitwise operators are discussed in detail in upcoming tutorials.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

You might also like