unary operator in c programming

Unary operator in c |Guide how to use unary operators in c programming

Welcome to this article and here we will discuss the Unary operator in c and the various types of unary operator.

As we mentioned in the previous article that in c language there are many types of unary operators and their works are also important while we creating any program.

Unary operator in c :

unary operators in c

If we define a unary operator then it would be like this, the unary operator is those operators which need one operand to work with and it does not commit any arithmetic operations between the two operands.

In c language there are lots of unary operators are available such as – unary plus operator(+) , minus operator(-) ,increment operator(++),decrement operator(–), size off() and others operators as well.

We will discuss about all theunary operator in c language one by one :

Plus operator(+):

The + operator is a kind of simple unary operator, and this is a very confusing operator because normally the + needs two operands to work with one another and to perform the arithmetic operation, but here the plus operator does not use to perform the arithmetic instructions instead of that this operator is mainly used to tells the sign of our numbers such as- +5,+7, here these are telling us that it is an operator which indicates the number is positive.

Increment operator(++) :

unary operator in c

The work of an increment operator is to increase our value with one addition, in easier terms if we enter 4 the use the increment operator then the result will come as 5, because it adds one with our number, 4++ indicating the 5.

The increment operator is further divided into two main divisions one is the increment operator and another is the decrement operator.

Both the operators do the same work but the main difference is in between their priority. Pre increments priority is most.

And post increments priority is less, even less than the assignment operator.

Example 1:

main()

{

int x=3;

clrscr();

x++;

printf(“%d”,x);

getch();

}

In example 1 we define 1 inside within the x variable and after that, we use the increment operator and increase the value with the increment operator, and now it will become 4. Here the x++ indicting x=X+1.

Example 2:

main()

{

int x=3;

clrscr();

x++;

printf(“%d”,x);

++x;

printf(“%d”,x);

getch();

}

This is an example of the pre-increment operator because here we use the increment operator before the x.

And its output will come 4 and 5 because x=x+1 and it will become 5 because x value 4.

And remember one more thing about the post-increment operator that is although post-increment is a unary operator till its value is less than the assignment operator.

Decrement operator :

unary operator in c

Decrement operator works similarly as well as increment operator.

The basic difference between them is that the decrement operator is used to decrease the value from its values by one number.

Example:

main()

{

int x=3;

clrscr();

x–;

printf(“%d”,x);

–x;

printf(“%d”,x);

getch();

}

Here we give an example of post-increment and pre-increment. And therefore the result will appear

as 2 and 1 because we already assign the x value 3 and it will become 2 and in the next pre-increment example it will become 1.

example 1:

main()

{

int x=3,y;

clrscr();

y=x++;

printf(“%d %d”,x,y);

getch();

}

This is a special program and we know post-increment has less value therefore at first the x value is assigned to y.

That means it becomes 3 in the y position and after that x will perform, which means 3=3+1

3=4

example 2:

main()

{

int x=3,y;

clrscr();

y =++x;

printf(“%d %d”,x,y);

getch();

}

As we know pre-increment work before assignment operator therefore it becomes y=1+3

and after it become y=1+3

and after pre increment y=4

operator works as assignment operator 4=4

sizeoff() :

unary operator in c

Well, people often ask that Is the size of a unary operator? , definitely yes, it is a kind of unary operator which is used to bring the size of data type, variable and constant.

Syntax of sizeoff():

  • Sizeof(data type)
  • sizeof (variable)
  • Sizeof(constant)

Data type in sizeof:

1.

main()

{

int x;

clrscr();

x=sizeof(float);

printf(“%d”,x);

getch()

}

Here the output will come to 4 because according to the 16-bit architecture float creates 8 bytes of memory.

Similarly, if we write double inside the parentheses then it will display 8, if we write char then it displays and if we write int then it will display 2.

Variable type in sizeof:

1.

main()

{

Int x,y;

float k;

double d1;

char ch;

clrscr();

x=sizeof(ch);

printf(“%d” ,x);

getch();

}

Here the result will display result 1 because according to 16-bit architecture it holds 1 bytes memory.

Here we create 2 variables by the name of x and y. And below that section, we create other variables as well by the name of k,d1, and ch.

2.

x=sizeof(d1);

If we print this then the result will come 8 because d1 is a kind of double data type.

In the similar way we can print the other datatypes as well.

Constant type in size of:

main()

{

int x,y,z;

clrscr();

x=sizeof(34);

y=sizeof(3.56);

z=sizeof(‘a’);

printf(“%d %d %d”,x,y,z);

getch();

}

Value of x will be because integer value always consumes 2 bytes.

The value of y will be because by default the real value considered as double because double is more accurate than float.

The value of z will be here the answer is 2, not 1 because the character value is also internally treated as an integer value.

The simple reason is that the value of the character cannot be translated into binary without ASCII code.

Through the help of the ASCII code, we got the number of small ‘a’ 97. And 97 is an integer value therefore here 97 treated as an integer.

Not operator :

The not operator is a unary operator which is normally used to transform the true numbers into false numbers.

And we use the exclamation mark to define the not operator.

For example, if we write p and considered p as true then the! p will become false.

Address of operator(&):

The address of the operator is also an example of a unary operator. This operator is mainly used with the pointers.

And it actually provides us the address of another variable.

We define the address of the operator with the help of an ampersand sign. We will read this operator in detail in the pointer chapter.

Also read

printf function in c programming
Best editors for c programming

Conclusion:

This article is all about the unary operator in c language and types of unary operators.

I hope you like this article, and if anyone has any query then please ask at [email protected]

And please follow our websites for this kind of informational articles.