special operators in C
Hello coders! Welcome back!
Today will look into special operators that are used in C.
Syntax- expression 1? expression 2: expression 3;
expression 1 check for the condition.
If it is true, then expression 2 is evaluated.
If it is false, then expression 3 is evaluated.
let's look with an example-
Using the 'if-else' statement it needs more lines of code. By using conditional statement we can reduce the lines of code.
Expression 1 always returns either TRUE or FALSE.
Reference operator →
'&' symbol is used as a reference operator.
You might have seen this in printf statements.
eg-
printf("%d",&x);
Here '&' symbol refers to the memory location of the given variable.
Dereference operator→
'*' symbol is used as a dereference operator.
We use this to access a value from a particular memory location. Usually, we use this in pointers concept (will look at this concept in later blogs) For time being understand that it is used to access the value from a particular memory location.
Comma operator →
🌟comma operator can be used as a separator.
Example -
int a=2, b=3, int c=4;
It is also equal to-
int a=2;
int b=3;
int c=4;
Simply declaring it 3 times we can declare it once and use comma operator.
🌟 Comma operator can be used as 'operator'
Example-
int a= (2,3,4);
printf("%d",a);
This returns the rightmost value. Remaining values are evaluated and finally rejected.
Therefore the output is '4'.
🌟 Comma operator has the least precedence amongst all the operators that are available in C.
Example-
int a;
a= 2,3,4;
printf("%d",a);
This will produce the output as '2'.
Because the assignment operator has the highest precedence compared to the comma operator.
The compiler treats it as-
(a=2),3,4;
Hence, the output is 2.
Let's take one more example-
int a=2,3,4;
Here, the comma operator is acting as a separator. Complier considers it separately.
int a=2, int 3, int 4;
This gives us an error because we can't declare a variable name with a digit (int 3 and int 4).
Brackets have the highest precedence than any other operator.
example-
int a=(2,3,4);
printf("%d",a);
Brackets are evaluated first. Then, assignment operator.
Today will look into special operators that are used in C.
Conditional operator, → This operator is used checking whether the condition satisfies or not.
It is the only turnery operator that is available in C.
expression 1 check for the condition.
If it is true, then expression 2 is evaluated.
If it is false, then expression 3 is evaluated.
let's look with an example-
Using the 'if-else' statement it needs more lines of code. By using conditional statement we can reduce the lines of code.
Expression 1 always returns either TRUE or FALSE.
Reference operator →
'&' symbol is used as a reference operator.
You might have seen this in printf statements.
eg-
printf("%d",&x);
Here '&' symbol refers to the memory location of the given variable.
Dereference operator→
'*' symbol is used as a dereference operator.
We use this to access a value from a particular memory location. Usually, we use this in pointers concept (will look at this concept in later blogs) For time being understand that it is used to access the value from a particular memory location.
Comma operator →
🌟comma operator can be used as a separator.
Example -
int a=2, b=3, int c=4;
It is also equal to-
int a=2;
int b=3;
int c=4;
Simply declaring it 3 times we can declare it once and use comma operator.
🌟 Comma operator can be used as 'operator'
Example-
int a= (2,3,4);
printf("%d",a);
This returns the rightmost value. Remaining values are evaluated and finally rejected.
Therefore the output is '4'.
🌟 Comma operator has the least precedence amongst all the operators that are available in C.
Example-
int a;
a= 2,3,4;
printf("%d",a);
This will produce the output as '2'.
Because the assignment operator has the highest precedence compared to the comma operator.
The compiler treats it as-
(a=2),3,4;
Hence, the output is 2.
Let's take one more example-
int a=2,3,4;
Here, the comma operator is acting as a separator. Complier considers it separately.
int a=2, int 3, int 4;
This gives us an error because we can't declare a variable name with a digit (int 3 and int 4).
Brackets have the highest precedence than any other operator.
example-
int a=(2,3,4);
printf("%d",a);
Brackets are evaluated first. Then, assignment operator.
Therefore the output is '4'.
This is it for this blog! If any doubts drop a comment.
This is the last part of operators you can view previous operators link is mentioned ↓
1) arithmetic operator (click here to view)
2) increment and decrement operator ( click here to view )
3) logical and relational operator (click here to view )
4) bitwise and assignment operator ( click here to view )
This is it for this blog! If any doubts drop a comment.
This is the last part of operators you can view previous operators link is mentioned ↓
1) arithmetic operator (click here to view)
2) increment and decrement operator ( click here to view )
3) logical and relational operator (click here to view )
4) bitwise and assignment operator ( click here to view )
Comments
Post a Comment