C Programming Operators

C Programming Operators



This tutorial is all about C programming operators. Along with a detailed answer of operators in C, you will also come to know the answer for the following:

What is & and * operators in c?
What is operators and its types?
What are different types of operators in c?

Let's start by knowing the actual meaning of operators and what the operators are? 

Operators are the symbols that are used to perform some operations. Suppose we are given a statement a+b*c. Here, symbols like +, * are operators and the alphabets like a, b, c are operands. 

When operands and operators combine to perform any operations then this is called expression.

Before knowing more about c programming operators, let us first understand some basic fundamentals. You may know that any programs written in any language are made up of using some basic elements.
  1. Alphabets
  2. Digits
  3. Blank spaces
  4. Special characters
What are the alphabets used in writing a program? 

"A" to "Z" and "a" to "z".

Here, you should remember that C is a case-sensitive language that means that  ' '  and  ' A'  both are different in C.

What are the digits used in writing a program?

"0" to "9".

What are the blank spaces used in writing a program?

'\n' , '\t' , '\r' , and so on.

What are the special characters used in writing a program?

Here is the list of the special symbols or characters that are used in writing a program.
  1.  .        period
  2.  ,        comma
  3.  :        colon
  4.  ;        semi-colon
  5.  _       underscore
  6.  %     percentile
  7.  -        hyphen or minus
  8.  +       plus
  9.  *       asterisk
  10.  &     ampersand
  11.  !       exclamation mark
  12.  ?       question mark
  13.  #       number sign
  14. /         forward slash
  15. \         backward slash
  16. '         single quote
  17.  "       double quotes
  18.  ~       tilde
  19. ( )      parenthesis or small brackets
  20. { }      curly braces or middle brackets
  21. [ ]       big brackets
  22. < >     angular brackets
  23. |          pipe


What are the keywords used in C language?

C language has some reserved words that cannot be used in naming any variables, functions, etc. They are the special because C compiler does not need any introduction before being applied in a program. They are only 32 words in c language which are keywords in C language and they are:
  1.  auto
  2.  break
  3.  case
  4.  char
  5.  const
  6.  continue
  7.  default
  8.  do
  9.  double
  10.  else
  11.  num
  12.  extern
  13.  float
  14.  for
  15.  goto
  16.  if
  17.  int
  18.  long
  19.  register
  20.  return
  21.  short
  22.  signed
  23.  sizeof
  24.  static
  25.  struct
  26.  switch
  27.  typedef
  28.  union
  29.  unsigned
  30.  void
  31.  volatile
  32.  while

What are identifiers in C?

Identifiers are the words that are formed by the programmers to give names to the variables, functions, array, pointers, etc so that it becomes very easier to recognize them in the future by its unique name. However, there are some specific rules which are to be known in our mind before creating any identifiers.


Rules to create Identifiers in C

  1. No keywords can be used as an identifier because they are special and already known to the compiler.
  2. The maximum length of the identifiers should not exceed 32 characters and almost length of identifier is 32.
  3. Only the following character sets can be used for creating identifiers. 'A' to 'Z', 'a' to 'z', '0' to '9', and ' _' (underscore).
  4.  The starting character of an identifier can either be an alphabet or underscore.
  5.  No two identifiers can be similar or identical in a single block.
Note that C is a case sensitive language where upper and lower case characters are important and pretend to be different from each other. for example, we can take Sum, sum, sUm, suM,... etc as identifiers. All of them represent different identifiers.

What are operators in C programming?

Operators are the symbol used to perform some operations. In C, operators are defined in two ways.
  1. On the basis of operands they required.
  2. On the basis of the type of operation they perform.
C programming operators On the basis of operands required:
  1. Unary operators
  2. Binary operators
  3. Ternary operators

Unary operators in C

The operator of this type requires one operand to complete its operation. for example 


  1.   ++ increment
  2.    --  decrement
  3.   + unary plus
  4.   unary minus
  5.    not

Here, let me tell you that what is mean by unary plus and minus. Consider a statement  -4, +5, the sign that is used before a number as a prefix only, then it is called unary plus or minus.
Let say -5-7: Here, the minus sign before 5 is called unary minus but the minus sign in between 5 and 7 is called subtraction.

Binary operators in C

The operators of this kind requires two operands to complete the operation. for example
  1.   + plus or addition
  2.   - minus or subtraction
  3.   * multiplication
  4.   / division
  5.   % modulus
  6.   < lesser than
  7.   > greater than
  8.   = is equal to or equals to, and so on.

Ternary operators in C

The operators of this type need three operands to complete its operations. for example   '' ?  : "  is a ternary operator and is followed in the conditional statements.

C programming operators on the base of the type of operation they show:
  1. Arithmetic operators or Mathematical operators
  2. Relational operators
  3. Logical operators
  4. Assignment operators
  5. Increment and Decrement operators
  6. Bitwise operators
  7. Size of operators [sizeof ()]
  8. Comma ( , ) operators

Arithmetic or Mathematical operators in C

  1. Addition
  2. -   Subtraction
  3. *  Multiplication
  4. /   Division
  5. % Modulus

These are the arithmetic or mathematical operators. They are also binary operators because they require exactly two operands to do an operation.

Example: Arithmetic operators in c

#include<stdio.h>
#include<conio.h>
void main()
 {
  printf("%d \n", 11+8);
  printf("%d \n", 11-8);
  printf("%d \n", 11*8);
  printf("%d \n", 11/8);    
  printf("%d \n", 11%8);      
 }

Output


19
3
88
1
3

Explanation:

1. When we add 11 and 8, we get 19.
2. When we subtract 8 from 11, we get 3.
3. When we multiply 8and 11, we get 88.
4. When we divide 11 and 8, we get 1 as quotient and 3 as remainder.
5. The symbol ' / ' gives us quotient. So, the answer is 1.
When we divide 11 and 8, we get 1 as quotient and 3 as remainder.
The symbol ' ' gives us the remainder. So, the answer is 3.

Relational operators in C

  1. <      lesser than 
  2. >      greater than
  3. < =    lesser than equals to
  4. > =    greater than equals to
  5. = =   is equal to
  6. ! =     is not equal to
These are called relational operators because they all show some relations between the operands. They are binary operators too because they require two operands for performing operations.

Example: Relational operators in c

#include<stdio.h>
#include<conio.h>
void main()
 {
  printf("%d \n",  10>7);          
  printf("%d \n",  10==7);       
  printf("%d \n",  10!=7);        
 }

Output


1
0
1

Explanation:


This type of question shows either 1 or 0 as the output on the screen.
  1.  10 is greater than 7. So, the answer is 1.
  2.  10 is not less than 7. So, the answer is 0.
  3.  10 is neither lesser nor equals to 7. So, the answer is 0. If anyone of the condition is true, the answer will be true (1).
  4.  10 is greater than 7 but is not equals to 7. Here, one condition is true So, the answer will be 1 (True).
  5.  10 is not equal to 7. So, the answer will be 0.
  6.  10 is not equal to 7. So, the answer will be 1.

Logical operators in C

  1.  &&    AND Operator
  2.  | |        OR Operator
  3.  !         NOT Operator
Logical operators are used to join two or more relations inside any program. An expression containing a logical operator returns 0 (0 means False) or 1(1 means True) depending upon the expression results. It is practiced for decision making in C language.
&& - Logical AND - It implies True only if all the operands are True.
|| - Logical OR - It implies True if either of the one operand is True.
! - Logical NOT - It implies True only if the operand denotes False.

Assignment operators in C

Assignment operators are the type of operators that manipulates the value of the variables. There are two types of assignment operators. 
  1.  Constructive assignment operators
  2.  Destructive assignment operators
Destructive assignment operators overwrite the old value of the variable with the new one whereas constructive assignment operators update the old value of the variable with the new one. 
This can be addition, subtraction, multiplication depends upon the operators that are being used.
Destructive assignment operator is: " = "
Constructive assignment operators are: += , -= , *= , /= , %= , >>= , <<=



Example: Assignment operators in c

#include<stdio.h>
void main()
 {
  int i;             // It is an example of destructive assignment operator.
  i = 0;        
  i = 2;
  i = 11;

  printf("It is an example of destructive assignment operator. \n");
  printf("%d", i);   
 }

Output

It is an example of destructive assignment operator.
11

Example: Assignment operators in c

#include<stdio.h>
void main()
 {
  int i = 2;  /* It is an example of constructive assignment operator.  */
  i += 1;        
  i = 3;
  i *= 2;

  printf("An example of constructive assignment operator. \n");
  printf("%d", i);           
 }

Output

An example of constructive assignment operator.
6

Increment / Decrement operator in C

++ is increment operator and -- is decrement operator. They can be used with variables only. They can be prefixed or postfixed with the variables. 

Example: Increment/Decrement operators in c

#include<stdio.h>
void main()
 {
  int i =  4;
  i++;
  i += 2;
  i+4;        // i+4 does not do anything.
  ++i;
  i *= i;

  printf("%d", i);     
 }

Output

64

Explanation:

first, we take variable 'i' and initialize it with 4. At the next step, it got incremented by 1 and 'i' become 5.
Then, the addition of 5 and 2 (that is 7) is updated in 'i' now i becomes 7.
Here, i+4 does mean nothing because no sign of '=' present here. So, it does nothing with i.
Now in the next step, i is again incremented by 1 and becomes 8.
Finally, the multiplication of i*i that is (8*8=64) and 64 is stored into i and 64 is printed by the printf statement as an output on the screen.


Bitwise operator in C

  1.  & Bitwise AND
  2.  |   Bitwise OR
  3.  ^  XOR (Exclusive OR)
  4.  ~ 1's Complement.
  5.  >> Right shift
  6.  << Left shift
Bitwise operators operate on the binary value of an operand.

Sizeof operator in C [sizeof ()]

The sizeof operator in c is used to determine the memory consumption of any variable and data types. 

Example: Sizeof operators in c


#include<stdio.h>
void main()
 {   
  int a;

  printf("%d", sizeof(a));        // prints size of a.
  printf("%d", sizeof(int));      // prints size taken by int datatype.
 }

Output

4
4

Comma operator in C

It is used as a separator between similar types of declaration. When the programmer wants to declare multiple variables of the same data types. Then, they make use of the comma operator as a separator between the declared variables.

Example: Comma operators in c


#include<stdio.h>
int main()
 {
  int a, b, c, e, f;      
  char m, z, j, g;           
  float n, k, s;      
     
  printf("Comma operator executed successfully");
  return 0;
 }

Output

Comma operator executed successfully

Conditional operator in C

The conditional operator is the only operator in C language which requires three operands to complete its operations. 

It is a replacement of if-else statement i.e., the programs that are made in multiple lines using an if-else statement can be made in a simple one-line using conditional operator.
Conditional operator is formed by the combination of two symbols ( ? and :).


It works exactly like an if-else statement where it tests for some condition first and after its evaluation executes either expression 1 or expression 2 as shown below in the syntax.

Syntax of Conditional operator in C

(True Condition) ? expression1 : expression2;

Example: Conditional operators in c


#include<stdio.h>
void main()
 {
  int a, b, c;

  printf("Enter two values \n");
  scanf("%d%d", &a, &b);

  c = (a>b) ? a : b;

  printf("Greatest number = %d", c);
 }

Output

Enter two values
105
10
Greatest number = 105

Example: Conditional operators in c

//     programs to find the greatest among three numbers using the conditional operator or conditional statement       */

#include<stdio.h>
void main()
 {
  int a, b, c, big;

  printf("Enter three values \n");
  scanf("%d%d%d", &a, &b, &c);

  big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;

  printf("Greatest number = %d", big);
 }

Output

Enter three values
5
120
13
Greatest number = 120

Operator precedence associativity or rank associativity in C

When there are multiple operators present in an expression then their priority of execution is determined by its precedence also known as rank. 

According to the rule, the operator with the least rank operates first in the expression but when there is more than one operator of the same rank is present in an expression.

Then their priority of execution is determined by their associativity that can be left to right or right to left.


Following are the lists of operators rank associativity:
Rank: 1 
Operator: ( ), [ ], ++ (prefix), -- (prefix)           
Direction: from left to right.
Rank: 2
Operator: sizeof( ), + (unary), - (unary), & (pointer), * (pointer)
Direction: from right to left.
Rank: 3
Operator: *,   /,   %
Direction: from left to right.
Rank: 4
Operator: + (add), - (subtract)
Direction: from left to right.
Rank: 5
Operator: >><<
Direction: from left to right.
Rank: 6
Operator: >, <, >=, <=
Direction: from left to right.
Rank: 7
Operator: = =, !=
Direction: from left to right.
Rank: 8
Operator: &
Direction: from left to right.
Rank: 9
Operator: ^
Direction: from left to right.
Rank: 10
Operator: |
Direction: from left to right.
Rank: 11
Operator: &&
Direction: from left to right.
Rank: 12
Operator:    | |
Direction: from left to right.
Rank: 13
Operator: (Condition) ?   :   
Direction: from right to left.
Rank: 14
Operator: =, +=, -=, /=, %=
Direction: from right to left.
Rank: 15
Operator: ,
Direction: from left to right.

<< Previous                                                                          Next  >>
Previous
Next Post »

Comment Below: ConversionConversion EmoticonEmoticon