# Tutorial by Examples

## Relational Operators

Relational operators check if a specific relation between two operands is true. The result is evaluated to 1 (which means true) or 0 (which means false). This result is often used to affect control flow (via if, while, for), but can also be stored in variables. Equals &quot;==&quot; Checks whether...

## Assignment Operators

Assigns the value of the right-hand operand to the storage location named by the left-hand operand, and returns the value. int x = 5; /* Variable x holds the value 5. Returns 5. */ char y = 'c'; /* Variable y holds the value 99. Returns 99 * (as the character 'c' is repr...

## Arithmetic Operators

Basic Arithmetic Return a value that is the result of applying the left hand operand to the right hand operand, using the associated mathematical operation. Normal mathematical rules of commutation apply (i.e. addition and multiplication are commutative, subtraction, division and modulus are not). ...

## Logical Operators

Logical AND Performs a logical boolean AND-ing of the two operands returning 1 if both of the operands are non-zero. The logical AND operator is of type int. 0 &amp;&amp; 0 /* Returns 0. */ 0 &amp;&amp; 1 /* Returns 0. */ 2 &amp;&amp; 0 /* Returns 0. */ 2 &amp;&amp; 3 /* Returns 1. */ Lo...

## Increment / Decrement

The increment and decrement operators exist in prefix and postfix form. int a = 1; int b = 1; int tmp = 0; tmp = ++a; /* increments a by one, and returns new value; a == 2, tmp == 2 */ tmp = a++; /* increments a by one, but returns old value; a == 3, tmp == 2 */ tmp = --b; ...

## Conditional Operator/Ternary Operator

Evaluates its first operand, and, if the resulting value is not equal to zero, evaluates its second operand. Otherwise, it evaluates its third operand, as shown in the following example: a = b ? c : d; is equivalent to: if (b) a = c; else a = d; This pseudo-code represents it : c...

## Comma Operator

Evaluates its left operand, discards the resulting value, and then evaluates its rights operand and result yields the value of its rightmost operand. int x = 42, y = 42; printf(&quot;%i\n&quot;, (x *= 2, y)); /* Outputs &quot;42&quot;. */ The comma operator introduces a sequence point between i...

## Cast Operator

Performs an explicit conversion into the given type from the value resulting from evaluating the given expression. int x = 3; int y = 4; printf(&quot;%f\n&quot;, (double)x / y); /* Outputs &quot;0.750000&quot;. */ Here the value of x is converted to a double, the division promotes the value of...

## sizeof Operator

With a type as operand Evaluates into the size in bytes, of type size_t, of objects of the given type. Requires parentheses around the type. printf(&quot;%zu\n&quot;, sizeof(int)); /* Valid, outputs the size of an int object, which is platform-dependent. */ printf(&quot;%zu\n&quot;, sizeof int); ...

## Pointer Arithmetic

Pointer addition Given a pointer and a scalar type N, evaluates into a pointer to the Nth element of the pointed-to type that directly succeeds the pointed-to object in memory. int arr[] = {1, 2, 3, 4, 5}; printf(&quot;*(arr + 3) = %i\n&quot;, *(arr + 3)); /* Outputs &quot;4&quot;, arr's fourth e...

## Access Operators

The member access operators (dot . and arrow -&gt;) are used to access a member of a struct. Member of object Evaluates into the lvalue denoting the object that is a member of the accessed object. struct MyStruct { int x; int y; }; struct MyStruct myObject; myObject.x = 42; myObj...

## Function Call Operator

The first operand must be a function pointer (a function designator is also acceptable because it will be converted to a pointer to the function), identifying the function to call, and all other operands, if any, are collectively known as the function call's arguments. Evaluates into the return valu...

## Bitwise Operators

Bitwise operators can be used to perform bit level operation on variables. Below is a list of all six bitwise operators supported in C: SymbolOperator&amp;bitwise AND|bitwise inclusive OR^bitwise exclusive OR (XOR)~bitwise not (one's complement)&lt;&lt;logical left shift&gt;&gt;logical right shift...

## _Alignof

C11 Queries the alignment requirement for the specified type. The alignment requirement is a positive integral power of 2 representing the number of bytes between which two objects of the type may be allocated. In C, the alignment requirement is measured in size_t. The type name may not be an inco...

## Short circuit behavior of logical operators

Short circuiting is a functionality that skips evaluating parts of a (if/while/...) condition when able. In case of a logical operation on two operands, the first operand is evaluated (to true or false) and if there is a verdict (i.e first operand is false when using &amp;&amp;, first operand is tr...

Page 1 of 1