C

Examples Of The Unary Operator In C

Unary operators only have one operand. The unary operators supported by the C programming language are as follows:

  • Minus unary (-)
  • Increase (++)
  • Decrement (–) (–)
  • NOT (!)
  • Operator of Addresses (&)
  • The operator sizeof()

1. Example of an Unary Minus (-) Operator

To change the sign of an operand, use the unary minus operator. It converts a positive operand to a negative operand and a negative operand to a positive operand. As an example:

int num = 10; //positive number
int num2 = -num; //value of num2 is -10

The following is the distinction between the minus(-) unary operator and the minus(-) arithmetic operator:
The minus (-) unary operator and minus (-) arithmetic operator appear to be the same, but they are not. The unary – operator operates on a single operand, whereas the arithmetic – operator operates on two. As an example:

int num = 10, num2 = 5, num3 = 100, sub; 

//this is minus(-) arithmetic operator
//working on two operands num and num2
sub = num - num2;

//this is minus (-) unary operator
//working on a single operand 
int inverseNum3 = -num3; //value of inverseNum3 is -100

Example of Unary minus(-) Operator:

#include <stdio.h>
int main ()
{
  int num = 5;

  // We are using minus unary operator to find additive
  // inverse number. The sum of number and its additive
  // inverse is zero. The difference is only the sign.
  int inverseNum = -num;

  printf ("Original number: %d \n", num);
  printf ("Opposite of given number: %d \n", inverseNum);
  return 0;
}

2. Example of the Increment (++) Operator

The increment operator is used to increase an operand’s value by one. Increment operators are classified into two types:
1. Add a prefix to the increment operator (for example, ++num).
2. Postfix increment operator (e.g., num++)
The distinction between prefix and postfix increment operators is that prefix increment instantly increases the value of an operand. The postfix increment operator, on the other hand, increases the value after it is used.
In this example, the variables num and num2 are both set to the same value. This is to demonstrate the distinction between prefix and postfix increment. The ++num (pre) function printed the increased value, but the num2++(post) function did not. This is due to the fact that the postfix increment operator increases the value after it is used; as you can see, when we printed the num2 value in the following statement, it printed the increased value.

#include <stdio.h>
int main ()
{
  //two integer variables with the same values
  int num = 100, num2 = 100;

  //pre increment increments the value instantly
  printf("Pre-increment variable num: %d\n", ++num);

  //post increment operator increase the value at the end of
  //the statement, before executing next statement
  printf("Post-increment variable num2: %d\n", num2++);

  //displaying num2 value after the post increment
  printf("Value of num2 after post increment: %d", num2);
  return 0;
}

3. Example of a Decrement (-) Operator

The increment operator reduces the value of an operand by one. The decrement operator, like the ++ operator, has two types:
1. Prefix decrement Operator Instantly decreases the value by one, for example: -num.
2. Postfix decrement Operator, for example: num-, decreases the value of an operand by one after it is used.

#include <stdio.h>
int main ()
{
  //two integer variables with the same values
  int num = 100, num2 = 100;

  //prefix decrement operator decreases value before the operand is used
  printf("Value of num after prefix decrement: %d\n",--num);

  //postfix decrement operator decreases the value after it is used
  printf("Value of num2 after postfix decrement: %d\n", num2--);

  //displaying num2 value after the postfix decrement
  printf("Value of num2 after it is used in previous statement: %d", num2);
  return 0;
}

Output:

Value of num after prefix decrement: 99
Value of num2 after postfix decrement: 100
Value of num2 after it is used in previous statement: 99

4. Operator NOT (!) Example

The NOT unary operator is used to reverse an operand’s logical state. If an operand’s value is true, the! operator will make it false, and if the value is false, it will make it true.

If the logical state of variable bool is true, then !bool is false
If the logical state of variable bool is false, then !bool is true

Here’s an example: In this example, the variable mathMarks represents the grades in the math subject. We are developing a C program to print a message “Congratulations! Admission granted” if the student’s math marks are greater than 60; otherwise, we print the message “Sorry! Admission is not permitted “.

#include <stdio.h>
int main ()
{
  int mathMarks=65;

  //if marks in math subject is not less than 60 then
  //prints the message "admission granted"
  if(!(mathMarks<60)){
    printf("Congratulations! Admission granted.");
  }
  else{
    printf("Sorry! Admission not granted.");
  }

  return 0;
}

5. AddressOf operator(&) Operator Exemplification

To get the address of a variable, use the AddressOf & operator. The & (ampersand) symbol represents this operator.

int num=100; //int variable
int *ptr; //pointer to store int variable address
ptr = # // address of variable num is stored in pointer ptr.

Example:

#include <stdio.h>
int main ()
{
  // int variable
  int num = 100;

  //int pointer variable to store the address of
  //an int variable
  int *ptr;

  // use addressof (&) operator to get the address
  ptr = &num;
  printf ("Value of variable num is: %d", num);
  printf ("\nAddress of variable num is: %p", ptr);

  //value of num using pointer, *ptr gives the value
  //stored at the address, represented by ptr
  printf ("\nValue of num using pointer: %d\n", *ptr);
  return 0;
}

6. Example of the sizeof() Operator

This operator, as the name implies, is used to determine the size of a data item in bytes. For example, the size of an int is 4 bytes, so using this operator on an int operator will return 4.
The correct format specifier for the sizeof() operator is%zu; however, it is not portable.

#include <stdio.h>
int main ()
{
  char ch; //char variable
  int i; //int variable
  float f; //float variable
  double d; //double variable

  // Using sizeof() operator to find the size of various data types
  printf ("Size of the char data type: %zu", sizeof(ch));
  printf ("\nSize of the int data type: %zu", sizeof(i));
  printf ("\nSize of the float data type: %zu", sizeof(f));
  printf ("\nSize of the double data type: %zu", sizeof(d));

  return 0;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button