Skip to main content

6. Keywords, Operators and Comments in C

 

Keywords in C

A keyword is a reserved word. We cannot use it as a variable name, constant name, etc. There are only 32 reserved words (keywords) in the C language.

A list of 32 keywords in the C language is given below :

autobreakcasechar
constcontinuedefaultdo
doubleelseenumextern
floatforgotoif
intlongregisterreturn
shortsignedsizeofstatic
structswitchtypedefunion
unsignedvoidvolatilewhile

C Operators

An operator is simply a symbol that is used to perform operations. There are the following types of operators to perform different types of operations in C language.

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operator

1. Arithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus

2. Relational Operators

OperatorDescription
==Is equal to
!=Is not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

3. Logical Operators

OperatorDescriptionExample
&&Logical AND operator. If both the operands are satisfied, then the condition is true.(A && B)
||Logical OR operator. If any of two operands is true, then condition becomes true.(A || B)
!Logical NOT operator. It is used to reverse the logical state of its operand.
If condition is true, then logical NOT operator will make it false.
!(A && B) or
!(A || B)

4. Bitwise Operators

aba & ba | ba ^ b
00000
01011
11110
10011
  • ~ is the binary one's complement operator.
  • << is the binary left shift operator.
  • >> is the binary right shift operator.

5. Assignment Operators

OperatorDescription
=Simple assignment operator. Assigns values from right side operands to left side operands.
+=Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
-=Subtract AND assignment operator. It subtracts the right operand to the left operand and the result is assigned to the left operand.
*=Multiply AND assignment operator. It multiplies the right operand with the left operand and the result is assigned to the left operand.
/=Divide AND assignment operator. It divides the left operand with the right operand and the result is assigned to the left operand.

Precedence and Associativity of Operators in C

The precedence of operator specifies that which operator will be evaluated first and next.
The associativity specifies the operator direction to be evaluated; it may be left to right or right to left.

CategoryOperatorsAssociativity
Postfix() [] {}Left to Right
Multiplicative* / %Left to Right
Additive+ -Left to Right
Shift<< >>Left to Right
Relational< <= > >=Left to Right
Equality== !=Left to Right
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
Bitwise OR|Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Assignment= += -= *= /=
%= >>= <<=
&= ^= |=
Right to Left

The + and - are equal in precedence, as *, /, and %.
The *, /, and % are performed first in order from left to right and then + and -, also in order left to right.

We can change the order of operations by using paranthesis () to indicate which operations are to be performed first.

Comments in C

Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types of comments in the C language :

  1. Single Line Comments
  2. Multi-Line Comments

1. Single Line Comments

Single line comments are represented by double slash // or \\.

#include <stdio.h>

int main()
{
   // printing information
   printf("Hello C");
   return 0;
}

Output :

Hello C

Even we can place the comment after the statement.

printf("Hello C");  // printing information

2. Multiple Line Comments

Multi-Line comments are represented by slash asterisk /* ..... */. It can occupy many line of code, but it can't be nested.

#include <stdio.h>

int main()
{
   /* printing information
   Multi-Line Comment */

   printf("Hello C");
   return 0;
}

Output :

Hello C

Comments