logo of SA Coder
        Menu
sign in

      References




C programming language provides a wide range of operators that can be used to perform different operations. In this post, we will discuss the conditional operator, special operators, and escape sequences in C programming language.


Conditional Operator



The conditional operator is also known as the ternary operator because it takes three operands. It is a single line condition checking operator that evaluates a condition and returns one value if the condition is true, and another value if the condition is false. The syntax of the conditional operator is: Condition ? True Statement : False Statement; Here, the condition is evaluated first. If it is true, the true statement is executed, and if it is false, the false statement is executed. Let's see an example to understand the conditional operator better:


copy

#include <stdio.h>
int main()
{
    int num;
    printf("Enter a number\n");
    scanf("%d", &num);
    num < 10 ? printf("you number is smaller than 10") : printf("your number is greater than 10");
    return 0;
}



In this example, we are using the conditional operator to check whether the entered number is less than 10 or greater than 10.


Special Operators



There are some special operators in C programming language that are used to perform specific operations. One of them is the address operator (&). It is used to get the address of a variable. For example:


copy

#include <stdio.h>
int main()
{
    int num = 5;
    printf("%d", &num);
    return 0;
}



In this example, we are using the address operator to get the address of the variable "num". The output of this program will be the address of the "num" variable.


Escape Sequences



Escape sequences are used to represent special characters in a string literal. They start with a backslash () followed by a specific character. Some commonly used escape sequences in C programming language are: \n for a new line \t for a tab \b for a backspace For example:


copy

#include <stdio.h>
int main()
{
printf("Hello, world!\n");
printf("This is a tab\tand this is a backspace\b");
return 0;
}



In this example, we are using the \n escape sequence to print a new line, \t to print a tab, and \b to print a backspace. Conclusion: In this post, we have discussed the conditional operator, special operators, and escape sequences in C programming language. These operators are very useful in different situations and can help us to perform various operations in a simple and efficient way. By understanding these concepts, we can write better and more efficient C programs.




Please login first to comment.