0% found this document useful (0 votes)
660 views

C - Operators and Expressions

C operators are used to perform logical and mathematical operations in a C program. There are different types of operators - arithmetic operators for math operations like addition and subtraction, assignment operators to assign values to variables, relational operators to compare values, logical operators for logical operations using AND, OR and NOT, bitwise operators for bit operations, and increment/decrement operators to increase or decrease a variable by one. Operators, variables, constants are combined to form expressions, which are then used in C programs.

Uploaded by

Bobby
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
660 views

C - Operators and Expressions

C operators are used to perform logical and mathematical operations in a C program. There are different types of operators - arithmetic operators for math operations like addition and subtraction, assignment operators to assign values to variables, relational operators to compare values, logical operators for logical operations using AND, OR and NOT, bitwise operators for bit operations, and increment/decrement operators to increase or decrease a variable by one. Operators, variables, constants are combined to form expressions, which are then used in C programs.

Uploaded by

Bobby
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C – Operators and Expressions

 The symbols which are used to perform logical and mathematical operations in a C program are called C
operators.
 These C operators join individual constants and variables to form expressions.
 Operators, functions, constants and variables are combined together to form expressions.
 Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is
an expression.
TYPES OF C OPERATORS:
Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction,
multiplication, division and modulus
Arithmetic Operators/Operation Example
Eg;
+ (Addition) A+B #include <stdio.h>
– (Subtraction) A-B int main(){
* (multiplication) A*B int a=40,b=20, add,sub,mul,div,mod;
/ (Division) A/B add = a+b;
% (Modulus) A%B sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n",
mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}

Assignment_operators These are used to assign the values for the variables in C programs.
 In C programs, values for the variables are assigned using assignment operators.
 For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
 There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
Operators Example/Description

sum = 10;
= 10 is assigned to variable sum

sum += 10;
+= This is same as sum = sum + 10

sum -= 10;
-= This is same as sum = sum – 10

sum *= 10;
*= This is same as sum = sum * 10

sum /= 10;
/= This is same as sum = sum / 10

sum %= 10;
%= This is same as sum = sum % 10

sum&=10;
&= This is same as sum = sum & 10

sum ^= 10;
^= This is same as sum = sum ^ 10
Eg:
# include <stdio.h>
int main(){
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}

Relational operators These operators are used to compare the value of two variables.
Operators Example/Description Eg:
#include <stdio.h>
> x > y (x is greater than y)
4 int main()
5 {
< x < y (x is less than y)
6 int m=40,n=20;
7 if (m == n)
>= x >= y (x is greater than or equal to y) 8 {
9 printf("m and n are equal");
<= x <= y (x is less than or equal to y) 10 }
11 else
== x == y (x is equal to y) 12 {
13 printf("m and n are not equal");
14 }
!= x != y (x is not equal to y) }

Logical operators These operators are used to perform logical operations on the given two variables.

 These operators are used to perform logical operations on the given expressions.
 There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operators Example/Description

(x>5)&&(y<5)
&& (logical AND) It returns true when both conditions are true

(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true

!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
! (logical NOT) If “((x>5) && (y<5))” is true, logical NOT operator makes it false

Eg;
#include <stdio.h>
int main(){
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0) {
printf("&& Operator : Both conditions are true\n"); }
if (o>p || p!=20) {
printf("|| Operator : Only one condition is true\n"); }
if (!(m>n && m !=0)){
printf("! Operator : Both conditions are true\n"); }
else {
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n"); }}
Bit wise operators These operators are used to perform bit operations on given two variables.
 These operators are used to perform bit operations. Decimal values are converted into binary values which are
the sequence of bits and bit wise operators work on these bits.
 Bit it wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift)
and >> (right shift).
Truth Table For Bit Wise Operation & Bit Wise Operators:

BELOW ARE THE BIT-WISE


WISE OPERATORS AND T
THEIR NAME IN C LANGUAGE.
1. & – Bitwise AND
2. | – Bitwise OR
3. ~ – Bitwise NOT
4. ^ – XOR
5. << – Left Shift
6. >> – Right Shift
Consider x=40 and y=80. Binary form of these values are given below.x = 00101000
y= 01010000
All bit wise operations for x and y are given below.
1. x&y = 00000000 (binary)) = 0 (decimal)
2. x|y = 01111000 (binary) = 120 (decimal)
3. ~x = 11111111111111111111111111 11111111111111111111111111111111010111 = -41 (decimal)
4. x^y = 01111000 (binary) = 120 (decimal)
5. x << 1 = 01010000 (binary) = 80 (decimal)
6. x >> 1 = 00010100 (binary) = 20 (decimal)

Eg:

3 #include <stdio.h>
4 int main(){
5 int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
6 AND_opr = (m&n);
7 OR_opr = (m|n);
8 NOT_opr = (~m);
9 XOR_opr = (m^n);
10 printf("AND_opr value = %d\n",AND_opr
n",AND_opr );
11 printf("OR_opr value = %d\n",OR_opr
n",OR_opr );
12 printf("NOT_opr value = %d\n",NOT_opr
n",NOT_opr );
13 printf("XOR_opr value = %d\n",XOR_opr
n",XOR_opr );
14 printf("left_shift value = %d\n",
n", m << 1);
15 printf("right_shift value = %d\n",
n", m >> 1); }
16

Conditional (ternary) operators


Conditional operators return one value if condition is true and returns another value is condition is false.
 Conditional operators rs return one value if condition is true and returns another value is condition is false.
 This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
 In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional
statements.
Eg:
#include <stdio.h>
int main(){
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
Increment/decrement operators
These operators are used to either increase or decrease the value of the variable by one.
 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:
Incrementoperator ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
 Example:
Incrementoperator ++i; i++;
Decrement operator : – – i ; i – – ;
Eg:
#include <stdio.h>
int main(){
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
} }

Operator Operator/Description

value of i is incremented before assigning it to the variable


Pre increment operator (++i) i

Post increment operator


(i++) value of i is incremented after assigning it to the variable i

value of i is decremented before assigning it to the variable


Pre decrement operator (–i) i

Post decrement operator (i–) value of i is decremented after assigning it to variable i

Special operators
&, *, sizeof( ) and ternary operators.

Operators Description

This is used to get the address of the variable.


& Example : &a will give address of a.

This is used as pointer to a variable.


* Example : * a where, * is pointer to the variable a.

This gives the size of the variable.


Sizeof () Example : size of (char) will give us 1.
Eg:

#include <stdio.h>
int main(){
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
C TOKENS:
 C tokens are the basic buildings blocks in C language which are constructed together to write a C
program.
 Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types. They are,
1. Keywords (eg: int, while),
2. Identifiers (eg: main, total),
3. Constants (eg: 10, 20),
4. Strings (eg: “total”, “hello”),
5. Special symbols (eg: (), {}),
6. Operators (eg: +, /,-,*)
Eg:

int main(){
int x, y, total;
x = 10, y = 20;
total = x + y;
printf ("Total = %d \n", total); }

where,
 main – identifier
 {,}, (,) – delimiter
 int – keyword
 x, y, total – identifier
 main, {, }, (, ), int, x, y, total – tokens
Do you know how to use C token in real time application programs? We have given simple real time
application programs where C token is used. You can refer the below C programs to know how to use C
token in real time program.

IDENTIFIERS IN C LANGUAGE:
 Each program elements in a C program are given a name called identifiers.
 Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name
given to integer variable in above program.
RULES FOR CONSTRUCTING IDENTIFIER NAME IN C:
1. First character should be an alphabet or underscore.
2. Succeeding characters might be digits or letter.
3. Punctuation and special characters aren’t allowed except underscore.
4. Identifiers should not be keywords.
KEYWORDS IN C LANGUAGE:
 Keywords are pre-defined words in a C compiler.
 Each keyword is meant to perform a specific function in a C program.
 Since keywords are referred names for compiler, they can’t be used as variable name.
C language supports 32 keywords

auto double long switch default goto do if


int struct continue for sizeof volatile static while

const float signed void char extern

short unsigned case enum return union

break else register typedef


C – Constant
 C Constants are also like normal variables. But, only difference is, their values can not be modified by
the program once they are defined.
 Constants refer to fixed values. They are also called as literals
 Constants may be belonging to any of the data type.
 Syntax:
const data_type variable_name; (or) const data_type *variable_name;
TYPES OF C CONSTANT:
1. Integer constants
2. Real or Floating point constants
3. Octal & Hexadecimal constants
4. Character constants
5. String constants
6. Backslash character constants
Constant type data type (Example)

int (53, 762, -478 etc )


unsigned int (5000u, 1000U etc)
long int, long long int
Integer constants (483,647 2,147,483,680)

float (10.456789)
Real or Floating point constants doule (600.123456789)

Octal constant int (Example: 013 /*starts with 0 */)

Hexadecimal constant int (Example: 0x90 /*starts with 0x*/)

character constants char (Example: ‘A’, ‘B’, ‘C’)

string constants char (Example: “ABCD”, “Hai”)


Rules For Constructing C Constant:
1. Integer Constants In C:
 An integer constant must have at least one digit.
 It must not have a decimal point.
 It can either be positive or negative.
 No commas or blanks are allowed within an integer constant.
 If no sign precedes an integer constant, it is assumed to be positive.
 The allowable range for integer constants is -32768 to 32767.
2. Real Constants In C:
 A real constant must have at least one digit
 It must have a decimal point
 It could be either positive or negative
 If no sign precedes an integer constant, it is assumed to be positive.
 No commas or blanks are allowed within a real constant.
3. Character And String Constants In C:
 A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
 The maximum length of a character constant is 1 character.
 String constants are enclosed within double quotes.
4. Backslash Character Constants In C:
 There are some characters which have special meaning in C language.
 They should be preceded by backslash symbol to make use of special function of them.
 Given below is the list of special characters and their purpose.
Backslash_character Meaning

\b Backspace

\f Form feed

\n New line

\r Carriage return

\t Horizontal tab

\” Double quote

\’ Single quote

\\ Backslash

\v Vertical tab

\a Alert or bell

\? Question mark

\N Octal constant (N is an octal constant)

\XN Hexadecimal constant (N – hex.dcml cnst)


1. By “const” keyword
2. By “#define” preprocessor directive
Example Program Using Const Keyword
#include <stdio.h>
void main(){
const int height = 100; /*int constant*/
const float number = 3.14; /*Real constant*/
const char letter = 'A'; /*char constant*/
const char letter_sequence[10] = "ABC"; /*string constant*/
const char backslash_char = '\?'; /*special char cnst*/
printf("value of height :%d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char); }

Example Program Using #Define Preprocessor Directive


#include <stdio.h>
#define height 100
#define number 3.14
#define letter 'A'
#define letter_sequence "ABC"
#define backslash_char '\?'
void main(){
printf("value of height : %d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n",letter_sequence);
printf("value of backslash_char : %c \n",backslash_char);
}

You might also like