LPU CSE101 C Programming Assignment Operator They are used to assign the result

Lpu cse101 c programming assignment operator they are

This preview shows page 15 - 24 out of 30 pages.

©LPU CSE101 C Programming Assignment Operator They are used to assign the result of an expression on right side to a variable on left side. Operator Description Example(a=4 and b=2) += a=a+b a+=b; a=a+b = 6 -= a=a-b a-=b; a=a-b = 2 *= a=a*b a*=b; a=a*b = 8 /= a=a/b a/=b; a=a/b = 2 %= a=a%b a%=b; a=a%b = 0 >>= a=a>>b a=00000100 >> 2 = 00010000 <<= a=a<<b A=00000100 << 2 = 00000001 &= a=a&b (a=0100, b=0010) a&=b; a=a&b = 0000 |= a=a|b (a=0100, b=0010) a|=b; a=a|b =0110 ^= a=a^b (a=0100, b=0010) a^=b; a=a^b = 0110
Image of page 15
©LPU CSE101 C Programming Assignment Operator To increase the cost of item soap by 50rs. Cost_soap = Cost_soap + 50; or Cost_soap += 50; To double the quantity of water in a bowl. Water_inBowl *= 2; Therefore assignment operator are used to store the changed value of the variable in the same variable. Quick yak: Ask how assignment operator is used in mobile call duration v/s balance left
Image of page 16
©LPU CSE101 C Programming Conditional Operator Conditional operator contains condition followed by two statements. If the condition is true the first statement is executed otherwise the second statement. It is also called as ternary operator because it requires three operands . Operator Description Example ?: conditional expression, Condition? Expression1: Expression2 (a>b)? “a is greater”: “b is greater”
Image of page 17
©LPU CSE101 C Programming Conditional Operator Eligibility to cast vote (age>=18)? “can cast vote”: “cannot cast vote”; In C (age>=18)? printf(“can cast vote”) : printf(“cannot cast vote”);
Image of page 18
©LPU CSE101 C Programming Bitwise Operator A bitwise operator works on each bit of data. Operator Description Example(a=1 and b=0) & bitwise AND a & b = 0 | bitwise OR a| b = 1 ^ bitwise XOR a ^ b = 1 ~ bitwise one’s complement ~a = 0, ~b=1 << bitwise left shift, indicates the bits are to be shifted to the left. 1101 << 1 = 1010 >> bitwise right shift, indicates the bits are to be shifted to the right. 1101 >> 1 = 0110 Logical Table a b a & b a | b a ^ b 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1
Image of page 19
©LPU CSE101 C Programming Some Special Operators Operator Description Example , comma operator, can be used to link the related expressions together int a, b, x; sizeof () sizeof operator to find the size of an object. int a; sizeof(a)=2 type Cast operator, to change the data type of the variable float x= 12.5; int a; a = (int) x; value of a is 12.
Image of page 20
©LPU CSE101 C Programming Comma operator can b used like: for(i=0 , j=1;i>10:i++ , j++) To know space occupied by variable in computer memory we use sizeof() operator. char choice; sizeof(choice); // 1 because char is 1byte If we are adding float number and integer number and we require output in float then integer number is converted to float using type cast operator. int num1; float num2, sum; sum= (float) num1 + num2;
Image of page 21
©LPU CSE101 C Programming
Image of page 22
Image of page 23
Image of page 24

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture