blog banner
 

1. What is operator in C?

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −

    1. Arithmetic Operators
    2. Relational Operators
    3. Logical Operators
    4. Bitwise Operators
    5. Assignment Operators
    6. Misc Operators

2. Explain The different types of operators in C ?

C Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).

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.

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.

Bitwise Operators

In processor, mathematical operations like: addition, subtraction, addition and division are done in bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Now lets see truth table for bitwise &, | and ^

C Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Misc Operators 

Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language.


 

3. What is the difference between Logical AND (&&) and Bitwise AND (&) in C programming ?

The logical AND operator ‘&&’ expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value.
 

The bitwise AND operator ‘&’ works on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return Integral value.
 

& has higher precedence than &&. so when they both appear in the same expression, & is evaluated before &&
 

Example

int main()
{

int x = 3; //binary representation of 3 is 0011
int y = 7; //binary representation of 7 is 0111

 

// A typical use of '&&'
if (y > 1 && y > x)

printf("y is greater than 1 AND y\n");

 

// A typical use of '&'
int z = x & y; // 0011

printf ("z = %d", z);
return 0;

}

Output

y is greater than 1 AND y
z = 3.
 

4. What is the difference between pre increment operator and post increment operator ? Or What is the difference between ++i and i++?

A. Pre Increment Operator

Pre-increment operator is used to increment the value of variable before using in the expression. In the Pre-Increment value is first incremented and then used inside the expression.

b = ++y;

In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 6 because the value of ‘y’ gets modified before using it in a expression.

B. Post Increment Operator

Post-increment operator is used to increment the value of variable as soon as after executing expression completely in which post increment is used. In the Post-Increment value is first used in a expression and then incremented.

b = x++;

In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5 because old value of ‘x’ is used.

Differences

    • ++a first increments the value of a and then returns an lvalue referring to a, so if the value of a is used then it will be the incremented value.
    • a++ first returns an rvalue whose value is a, that is, the old value, and then increments a at an unspecified time before the next full-expression (i.e., "before the semicolon").
    • Postfix increment has higher precedence than prefix increment.

 

5. What is the difference between & and * in C programming ?

The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator.

 

The * is a unary operator which returns the value of object pointed by a pointer variable. It is known as value of operator. It is also used for declaring pointer variable.

 

int main() {

int i; // i is an int
int *p; // this is a * in a type-name. It means p is a pointer-to-int
p = &i; // use & operator to get a pointer to i, assign that to p.
*p = 3; // use * operator to "dereference" p, meaning 3 is assigned to i.

}
 

6. What is unary operator in C programming ? What are the types of unary operators in C programming ?

A unary operator, in C, is an operator that takes a single operand in an expression or a statement. A unary operator takes only one operand and performs its' action on that operand.

We have following Unary Operators in C.

! (logical negation)

This operator can be used only with operands of Boole type. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.

!(FALSE && FALSE) is true.

~ (one’s complement or bitwise negation)

This operator can be used with integer, unit, long and ulong operand types. The result of the operation is a bitwise 1's complement (inverse of the binary representation) of the operand. One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary pattern. It is denoted by ~.

– (unary minus)

This operator can be used to negate numbers of the integer, floating-point and decimal type.The operand of Unary minus operator must have arithmetic type, and the result is the –ve of its operand. The operand can be numerical constant, variable or expression. It is different from arithmetic subtraction operator (requires two operand).

Example:

-(p+q)
x=-y
-10*(p+q)

+ (unary plus)

The result of an operation on a numeric type is the value of the operand itself. This operator has been predefined for all numeric types.

Example:

+(p+q)
+10*(p+q)

& (addressof)

The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. refer to question number 60 for reference.

* (dereferencing)

The * is a unary operator which returns the value of object pointed by a pointer variable. It is known as value of operator. It is also used for declaring pointer variable. refer to question number 60 for reference.

Increment (++) and Decrement (--) Operator

The operand can be a variable, property access, or an indexer access. With an increment operator, the result of the operation for operands of integer type would be the value incremented by 1. With a decrement operator, the result would be the value decremented by 1 from the operand. The increment/decrement operator can also be used with postfix notation. refer to question number 59 for reference.

sizeof operator :

The sizeof operator returns the size in bytes of its operand. Operand may be a variable or data type. refer to question number 33 for reference.

(type) or cast operator

Used for typ ecasting of a variable. This process is also know as type casting in C. Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows -

Syntax : 

(type_name) expression

For example :

float i = 4.71

int j = (int) i;

All Unary Operators have associativity from Right to Left.
 

7. What is the difference between logical negation : NOT(~) and one’s complement or bitwise negation : NEGATION(!) ?

When i gets to -1, the value of ~i is ~-1, or 0, so the while loop stops executing. The ! operator works because it does something completely different; it results in 1 for 0 values and 0 for all other values. ~ is a bitwise negation.

A little more in detail:

  • ~ takes each bit in a number and toggles it. So, for example, 100102 would become 011012
  • -1 is 11111111 in binary when a two's complement signed integer.

However:

  • !0 is 1, !anythingElse is 0
  • -1 is not 0
  • !-1 is still 0

 

8. What is ternary operator in C programming ?

The ternary operator is a conditional operator. The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as shortened way of writing an if-else statement. It is often used as a way to assign variables based on the result of an comparison. When used correctly it can help increase the readability and reduce the amount of lines in your code.

Syntax :

expression 1 ? expression 2 : expression 3

where

expression1 is Condition
expression2 is Statement Followed if Condition is True
expression2 is Statement Followed if Condition is False

Meaning of Syntax :

  • Expression1 is nothing but Boolean Condition i.e it results into either TRUE or FALSE
  • If result of expression1 is TRUE then expression2 is Executed
  • Expression1 is said to be TRUE if its result is NON-ZERO
  • If result of expression1 is FALSE then expression3 is Executed
  • Expression1 is said to be FALSE if its result is ZERO

How it works

Conditional operators return one value if condition is true and returns another value is condition is false.

Example

#include
main()
{

int a , b;
a = 10;
printf( "Value of b is %d\n", (a == 1) ? 20: 30 );

printf( "Value of b is %d\n", (a == 10) ? 20: 30 );

}

This will produce following result:

Value of b is 30
Value of b is 20

Admin Team


0 Respond



Written
Exam Questions
BLOG Posts
For study materials
Job Walk-In
Updates across India
Interview round
Questions
Interview or procedure
Experiences
Files
For study materials
Please LIKE our page to Get regular JOB WALK-IN UPDATES across India and STUDY MATERIALS on facebook news feed.