Explain use of comma operator and What is the difference between & and && in C. 10m Jun2007

By | July 10, 2014

Explain use of comma operator in C with the help of an example. 5m Jun2007

 Comma Operator in C

A comma operator is used to separate a pair of expressions. A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the value of the type and value of the right operand. All side effects from the evaluation of the left operand are completed before beginning evaluation of the right operand. The left side of comma operator is always evaluated to void. This means that the expression on the right hand side becomes the value of the total comma-separated expression.

For example,   x = (y=2, y – 1);

first assigns y the value 2 and then x the value 1. Parenthesis is necessary since comma operator has lower precedence than assignment operator. Generally, comma operator (,) is used in the for loop

For example,

for (i = 0,j = n;i<j; i++,j–)


printf (“A”);


In this example for is the looping construct. In this loop, i = 0 and j = n are separated by comma (,) and i++ and j—are separated by comma (,). The example will be clear to you once you have learnt for loop. Essentially, the comma causes a sequence of operations to be performed. When it is used on the right hand side of the assignment statement, the value assigned is the value of the last expression in the comma-separated list.


What is the difference between & and && ? Explain with an exarnple. 5m Jun2007

&& operator is a logical and operator and & is a bit wise and operator. Therefore, && operator always evaluates to true or false i.e. 1 or 0 respectively While & operator evaluates bit wise so the result can be any value. For example:

2 && 5 => 1(true)

2 & 5 => 0(bit-wise ending)

Use of Bit Wise operators makes the execution of the program faster.


Write a program in C using structures to simulate salary calculation for employees of a company. Assumptions can be made wherever necessary. 10m Jun2007

Solved program can be found on this link http://cssimplified.com/assignments/an-interactive-c-program-to-generate-pay-slips-for-the-staff-of-size-12-employees-2-members-are-clerks-one-computer-operator-6-salesmen-3-helpers-working-in-a-small-chemist-retail-shop


Leave a Reply