GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Unary operators in C

The operators which act on single operand to give the result is called as unary operator. We shall discuss the following unary operators here.

  1. Unary plus ( + )
  2. Unary minus ( – )
  3. Increment operator ( ++ )
  4. Decrement operator ( — )
  5. Address operator ( & )
  6. Dereference operator ( * )
  7. Negation operator ( ~ )
  8. sizeof operator

You may refer to the video posted at the end of this page.

Unary plus (+)

This operator is used to represent the positive numbers.

Ex: +3, +a

Unary minus (-)

This operator is used to represent the negative numbers.

Ex: -1, -a

Increment operator (++)

This operator means add one to the given number.

This operator can only be used with the variables.

They cannot be used with the constants.

There are two types of increment operators.

  1. pre increment.
  2. post increment.

Pre increment

In pre-increment, the operator appears before the operand. 

++a (we can see that the operator ++, is before the operand ‘a’)

if a = 5

then ++a, will make a = 6

add one to the number.

if a = -1

then ++a, will make a = 0

add one to the number.

Pre-increment means increment first then assign. What does that mean let us see by some examples.

if, a = 4 and

b = ++a,

first increment the value of ‘a’, a becomes 5, then assign 5 to b.

Therefore, a = 5 and b = 5

if, a = -1 and

b = ++a,

first increment the value of ‘a’, a becomes 0, then assign 0 to b.

Therefore, a = 0 and b = 0

Post Increment

In post-increment, the operator appears after the operand. 

a++ (we can see that the operator ++, is after the operand ‘a’)

if a = 5

then a++, will make a = 6

add one to the number.

if a = -1

then a++, will make a = 0

add one to the number.

At this point, we see that both the pre-increment and post-increment produces the same result. But there is difference when we assign the result to another variable.

Post-increment means assign first then increment. Let us understand this by some examples.

if, a = 4 and

b = a++,

Assign the value a to b first then increment the value of a.

Therefore, a = 5 and b = 4

if, a = -1 and

b = a++,

Assign the value a to b first then increment the value of a.

Therefore, a = 0 and b = -1

The following are invalid.

++3, 4++ (increment operators can only be applied on variables, cannot be applied on constants)

Decrement operator (--)

This operator means subtract one from the given number.

This operator can only be used with the variables.

They cannot be used with the constants.

There are two types of decrement operators.

  1. pre decrement.
  2. post decrement.

Pre decrement

In pre-decrement, the operator appears before the operand. 

–a (we can see that the operator –, is before the operand ‘a’)

if a = 5

then –a, will make a = 4

subtract one from the number.

if a = -1

then –a, will make a = -2

subtract one from the number.

Pre-decrement means decrement first then assign. What does that mean let us see by some examples.

if, a = 4 and

b = –a,

first decrement the value of ‘a’, a becomes 3, then assign 3 to b.

Therefore, a = 3 and b = 3

if, a = -1 and

b = –a,

first decrement the value of ‘a’, a becomes -2, then assign -2 to b.

Therefore, a = -2 and b = -2

Post decrement

In post-decrement, the operator appears after the operand. 

a– (we can see that the operator –, is after the operand ‘a’)

if a = 5

then a–, will make a = 4

Subtract one from the number.

if a = 0

then a–, will make a = -1

Subtract one from the number.

At this point, we see that both the pre-decrement and post-decrement produces the same result. But there is difference when we assign the result to another variable.

Post-decrement means assign first then decrement. Let us understand this by some examples.

if, a = 4 and

b = a–,

Assign the value a to b first then decrement the value of a.

Therefore, a = 3 and b = 4

if, a = 0 and

b = a–,

Assign the value a to b first then decrement the value of a.

Therefore, a = -1 and b = 0

The following are invalid.

--3, 4-- (decrement operators can only be applied on variables, cannot be applied on constants)

Address operator (&)

This operator is used to get the address of the variable. This operator we will discuss in detail in the scanf statement and the pointers chapter.

Dereference operator (*)

This operator is used to get the value at the given address. We shall discuss this operator in detail in the pointers chapter.

Negation operator (~)

This operator flip the bits of a given number. 

This operator can only be applied on integers.

We will discuss this operator in the bitwise operator section.

sizeof

This operator gives the size taken by the datatype or a variable or a constant. We will discuss this in detail after the datatypes and variables.

Scroll to Top