Special Operators in C Programing



Special Operators:
       Programing language C contains many different kinds of operators which may be classified as special or miscellaneous operators because they do not belong to any specific category. In the following sections, we will be introducing to the conditional (?:) operator, the comma operator (,) and the sizeof operator.





The Conditional Operator (?:):
        The conditional operator (?:) helps in building a simple conditional expression in the following form:
Variable = expression_1 ? expression_2 : expression_3;
This from emulates the if statement as mentioned below:
if expression_1 is True Then
 variable = expression_2
else
variable = expression_3
        Note that, in both cases, expression_1 is relational or a logical expression. If expression_1 is evaluated as True, then the variable is assigned the value of expression_2; otherwise, it is assigned the value of expression_3.
For Example:
#include<stdio.h>
main()  {
int a, b, big;
printf("Enter The Value Of a & b:");
scanf("%d %d",&a, &b);
big = a > b ? a : b;
printf("Bigger No. is: %d", big);  }
Output:






 The Comma Operator:
        We can use the comma operator (,) available in C lanuage, to build a compound expression by putting several expression inside a set of parentheses. The expression are evaluated from left to right and the final value is the one that is evaluated last. For example the expression the expression:
(x=c, c=getchar() )
Here we have two expression separated by a comma and thus, making up one compound expression. The variables “c” and “x” are of the type char. In the first expression, “x” is assigned the value of “c”. In second expression, “c” receives a new value from the keyboard. That makes the final value of “c” different from the value of “x”. Actually, the value of the entire expression is the final value of “c”.
For Example:
#include<stdio.h>
main()  {
char x, c = 'B';
printf("The value of the expression is: %c", (x = c,c = getchar() ));
printf("\nThe value of the variables \"x\" is:  %c", x);  }
Output:






 The sizeof Operator:
       The sizeof operator is used to return the size (in bytes) of its operands. It is used in the form:
sizeof(b)
Where, b is a variable or a type.
     We can assign the value of the expression to an integer variable, or we can use it directly.
For Example:
#include<stdio.h>
main()   {
printf("The size of char is %d", sizeof(char));
printf("\nThe size of int is %d", sizeof(int));
printf("\nThe size of short is %d", sizeof(short));
printf("\nThe size of float is %d", sizeof(float));
printf("\nThe size of long is %d", sizeof(long));
printf("\nThe size of double is %d", sizeof(double));
printf("\nThe size of long double is %d\n", sizeof(long double));   }
Output:

Special Operators in C Programing Special Operators in C Programing Reviewed by vijay pratap singh on August 26, 2017 Rating: 5

No comments:

Powered by Blogger.