You are on page 1of 12

Operators & Expression in C

C-Operators
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.

• C language provides the following types of operators:

1.Arithmetic Operators
2.Relational Operators
3.Logical Operators
4.Assignment Operators
5.Bitwise Operators
6.Sift Operators
7. Ternary operators
Arithmetic Operator
• Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example

+ Adds two operands A + B will give 30


- Subtracts second operand from the A - B will give -10
first
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2

% Modulus Operator and remainder of B % A will give 0


after an integer division

++ Increments operator increases A++ will give 11


integer value by one
-- Decrements operator decreases A-- will give 9
integer value by one
Relational Operator
• Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example

== Checks if the values of two operands are (A == B) is not true.


equal or not
!= Checks if the values of two operands are (A != B) is true.
equal or not
> Checks if the value of left operand is (A > B) is not true.
greater than the value of right operand

< Checks if the value of left operand is (A < B) is true.


less than the value of right operand

>= Checks if the value of left operand is (A >= B) is not true.


greater than or equal to the value of
right operand
<= Checks if the value of left operand is (A <= B) is true.
less than or equal to the value of right
operand
Logical Operator
• Assume variable A holds 1 and variable B holds 0, then:
Operator Description Example

&& Called Logical AND operator. If both (A && B) is false.


the operands are non-zero, then
condition becomes true.

|| Called Logical OR Operator. If any (A || B) is true.


of the two operands is non-zero,
then condition becomes true.

! Called Logical NOT Operator. Use to !(A && B) is true.


reverses the logical state of its
operand. If a condition is true then
Logical NOT operator will make
false.
Operators Precedence in C
• Operator precedence determines the grouping of terms in an expression.
This affects how an expression is evaluated.
Category  Operator  Associativity 
Postfix  () [] -> . ++ - -   Left to right 
Unary  + - ! ~ ++ - - (type)* & sizeof  Right to left 

Multiplicative   * / %  Left to right 

Additive   + -  Left to right 


Shift   << >>  Left to right 
Relational   < <= > >=  Left to right 
Equality   == !=  Left to right 
Logical AND  &&  Left to right 
Logical OR  ||  Left to right 
Conditional  ?:  Right to left 
Assignment  = += -= *= /= %=>>= <<= &= Right to left 
^= |= 

Comma  ,  Left to right 


Type Conversion

• The process of converting one predefined data type into another is


called type conversion.
• Type conversion in c can be classified into the following two types:
1.Implicit Type Conversion
2.Explicit Type Conversion

Implicit Type Conversion :


• When the type conversion is performed automatically by the compiler
without programmers intervention, such type of conversion is known
as implicit type conversion or type promotion.
• The compiler converts all operands into the data type of the largest
operand.
Explicit Type Conversion :
• The type conversion performed by the programmer by posing the data type
of the expression of specific type is known as explicit type conversion.
• The explicit type conversion is also known as type casting.
• Type casting in c is done in the following form: (data_type)expression
For example, x = (int) (a+b*d);
#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
}
Basic Input/Output
• All input and output is performed with streams, which are sequences of
bytes.

• In input operations, the bytes flow from a device (e.g., a keyboard, a disk
drive, a network connection) to main memory.

• In output operations, bytes flow from main memory to a device (e.g., a


display screen, a printer, a disk drive, a network connection, and so on).

• When program execution begins, three streams are connected to the


program automatically.
Formatted I/O in C
• The formatted Input / Output functions can read and write values of all data
types, provided the appropriate conversion symbol is used to identify the
datatype.

• scanf is used for receiving formatted Input


• printf is used for displaying formatted output.

• Syntax for scanf :


scanf(“control_string 1, - - , control_string n”,&variable1, - - - - ,variable n);

• Syntax for printf :


printf(“control_string 1, - - , control_string n”,variable1, - - - - , variable n);
Unformatted I/O in C
• Unformatted I/O functions works only with character datatype (char).
•  The unformatted Input functions used in C are getch(), getche(), getchar(),
gets() and unformatted output statements in C are putch(),
putchar() and puts().
Function Syntax Description
getch() variable_name = getch(); getch() accepts only single character

getche() variable_name = getche() also accepts only single character, but


getche(); unlike getch(), getche() displays the entered
character
getchar() variable_name = getchar() accepts one character
getchar();
gets() gets(variable_name); gets() accepts any line  of string including spaces

putch() putch(variable_name); putch() displays any alphanumeric characters

putchar() putchar(variable_name); putchar() displays one character at a time

puts() puts(variable_name); puts() displays a single / paragraph of text


Escape Sequences in C
• An escape sequence is a sequence of characters that does not represent itself when
used inside a character or string literal, but is translated into another character or a
sequence of characters that may be difficult or impossible to represent directly.

Sequence Character
\a Bell (speaker beeps)
\b Backspace (non-erase)
\f Form feed/clear screen
\n New line
\t Tab
\v Vertical tab
\\ Backslash
\? Question mark
\' Single quote
\" Double quote

You might also like