What is Basic Operators in Programming
Operator: An operator is a symbol that represents a specific type of operation that can be performed on one or more values, or variables, in a programming language. For example, the addition operator + is used to perform addition on two values, and the assignment operator = is used to assign a value to a variable. Different programming languages have different sets of operators, and each operator typically has a specific meaning and function. It is a special symbol which is used to perform logical or mathematical operation on data or variable.
|
Learn Best Operators C Language in 2023 |
Types of Operator in C Language
1. Arithmetic operators: These operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.
2. Relational operators: These operators compare two values and return a Boolean value (true or false) based on the relationship between the values.
3. Logical operators: These operators perform logical operations, such as AND, OR, and NOT.
4. Bitwise operators: These operators perform operations on individual bits of a binary number.
5. Assignment operators: These operators assign a value to a variable.
6. Increment and decrement operators: These operators increase or decrease the value of a variable by 1.
7. Conditional operators: The conditional operator, also known as the ternary operator, is an operator in C that allows you to assign a value to a variable based on a certain condition.
8. Special operators: Special operator is a address store(&) and sizeof() value store in special operator.
Pointer Arithmetic Operators in C
C programming language, the following arithmetic operators are available:
1. Addition operator.
2. Subtraction operator.
3. Multiplication operator.
4. Division operator.
5. Modulus operator.
Arithmetic Operator Table in C Programming
Symbols | Operations | Examples |
---|
+ | Addition | a+b |
- | Subtraction | a-b |
* | Multiplication | a*b |
/ | Division | a/b |
C Language Arithmetic Operators Example
#include<stdio.h> #include<conio.h> void main() { clrscr(); int f,s,add,sub,mul,div,mod; printf("Enter First Number:"); scanf("%d",&f); printf("Enter second Number:"); scanf("%d",&s); add=f+s; sub=f-s; mul=f*s; div=f/s; mod=f%s; printf("Addition:%d\n",add); printf("Subtraction:%d\n",sub); printf("Multiplication:%d\n",mul); printf("Division:%d\n",div); printf("Modulus:%d",mod); getch(); }
**********OUTPUT********** Enter First Number:5 Enter Second Number:2 Addition:7 Subtraction:3 Multiplication:10 Division:2.5 Modulus:1 |
Explain Arithmetic and Relational Operators in C Language
Relational Operator: A computer programming, relational operators are operators that are used to compare values and determine the relationship between those values. There are several relational operators in most programming languages.
1. Equal to.
2. Not equal to.
3. Greater than.
4. Less than.
5. Greater than or equal to.
6. Less than or equal to.
Relational Operator Table in C Programming
Symbols | Operations | Examples |
---|
= | Equal | a=b |
!= | Not Equal To | a!=b |
> | Greater Than | a>b |
< | Less Than | a<b |
>= | Greater Than or Equal To | a>=b |
<= | Less Than or Equal To | a<=b |
Explain Relational Operators in C Language
#include<stdio.h> #include<conio.h> int main() { int f,s; printf("Enter First Number:"); scanf("%d",&f); printf("Enter second Number:"); scanf("%d",&s); printf("Equal To:%d\n",f==s); printf("Greater Than:%d\n",f>s); printf("Less Than:%d\n",f<s); printf("Greater Than or Equal To:%d\n",f>=s); printf("Less Than or Equal To:%d\n",f<=s); printf("Not Equal To:%d",f!=s); }
**********OUTPUT********** Enter First Number:5 Enter second Number:3 Equal To:0 Greater Than:1 Less Than:0 Greater Than or Equal To:1 Less Than or Equal To:0 Not Equal To:1 |
Logical Operators in C Definition
Logical operators: A C programming language, there are several logical operators that you can use to perform logical operations on expressions.
1. AND (&&) operator: This operator returns true if both the operands are true, otherwise it returns false.
2. OR (||) operator: This operator returns true if either of the operands is true, otherwise it returns false.
3. NOT (!) operator: This operator returns true if the operand is false, and returns false if the operand is true.
Logical Operator Table in C Programming
Symbols | Operations | Examples |
---|
&& | Logical AND | a&&b |
|| | Logical OR | a||b |
! | Logical NOT | a!b |
Logical Operators in C Example
#include<stdio.h> #include<conio.h> int main() { int f,s; printf("Enter First Number:"); scanf("%d",&f); printf("Enter second Number:"); scanf("%d",&s); printf("Logical AND Operator:%d\n",f&&s); printf("Logical OR Operator:%d\n",f||s); printf("Logical NOT Operator:%d\n",f!=s);
}
**********OUTPUT********** Enter First Number:5 Enter second Number:3 Logical AND Operator:0 Logical OR Operator:1 Logical NOT Operator:1 |
Bitwise Operators in C Definition
Bitwise operators: A C programming language, there are several bitwise operators that you can use to perform bitwise operations on integers.
1. Bitwise AND (&) operator: This operator performs a bitwise AND operation on two operands. It returns 1 if both the operands are 1, and returns 0 otherwise.
2. Bitwise OR (|) operator: This operator performs a bitwise OR operation on two operands. It returns 1 if either of the operands is 1, and returns 0 otherwise.
3. Bitwise XOR (^) operator: This operator performs a bitwise exclusive OR operation on two operands. It returns 1 if either of the operands is 1, but not both, and returns 0 otherwise.
4. Bitwise NOT (~) operator: This operator performs a bitwise NOT operation on an operand. It inverts all the bits of the operand.
5. Bitwise left shift (<<) operator: This operator shifts the bits of an operand to the left by a specified number of positions.
6. Bitwise right shift (>>) operator: This operator shifts the bits of an operand to the right by a specified number of positions.
Bitwise Operator Table in C Programming
Symbols | Operations | Examples |
---|
& | Bitwise AND | a&b |
| | Bitwise OR | a|b |
^ | Bitwise XOR | a^b |
~ | Bitwise NOT | a~b |
>> | Bitwise RIGHT SHIFT | a>>2 |
Bitwise Operators in C Hackerrank Solution
#include<stdio.h> #include<conio.h> int main() { int f,s; printf("Enter First Number:"); scanf("%d",&f); printf("Enter second Number:"); scanf("%d",&s); printf("Bitwise AND Operator:%d\n",f&s); printf("Bitwise OR Operator:%d\n",f|s); printf("Bitwise XOR Operator:%d\n",f^s); printf("Bitwise NOT Operator:%d\n",f~s); printf("Bitwise LEFT SHIFT Operator:%d\n",f<<1); printf("Bitwise RIGHT SHIFT Operator:%d\n",f>>1);
}
**********OUTPUT********** Enter First Number:10 Enter second Number:5 Bitwise AND Operator:0 Bitwise OR Operator:15 Bitwise XOR Operator:15 Bitwise NOT Operator:-11 Bitwise LEFT SHIFT Operator:20 Bitwise RIGHT SHIFT Operator:5 |
Assignment Operator use in C Language
Assignment operators: A C programming language, there are several assignment operators that you can use to assign a value to a variable.
Compound assignment operators: These operators are used to perform an operation and assign the result to a variable in a single statement. The compound assignment operators are:
1. Addition assignment operator(+=): This operator adds the right operand to the left operand and assigns the result to the left operand.
2. Subtraction assignment operator(-=): This operator subtracts the right operand from the left operand and assigns the result to the left operand.
3. Multiplication assignment operator(*=): This operator multiplies the left operand by the right operand and assigns the result to the left operand.
4. Division assignment operator(/=): This operator divides the left operand by the right operand and assigns the result to the left operand.
5. Modulus assignment operator(%=): This operator calculates the remainder when the left operand is divided by the right operand and assigns the result to the left operand.
Assignment Operator Table in C Programming
Symbols | Operations | Examples |
---|
+= | Addition | a+=b or a=a+b |
-= | Subtraction | a-=b or a=a+b |
*= | Multiplication | a*=b or a=a+b |
/= | Division | a/=b or a=a+b |
Assignment Operator in C Simple Example
#include<stdio.h> #include<conio.h> void main() { int a; printf("Enter First Number:"); scanf("%d",&a); printf("Assignment Addition:%d\n",a+=5); printf("Assignment Subtraction:%d\n",a-=5); printf("Assignment Multiplication:%d\n",a*=5); printf("Assignment Division:%d\n",a/=5); printf("Assignment Modulus:%d",a%=5); } getch(); }
**********OUTPUT********** Enter First Number:7
Assignment Addition:12 Assignment Subtraction:7 Assignment Multiplication:35 Assignment Division:7 Assignment Modulus:2 |
Increment and Decrement Operators in C Questions
Increment and decrement operators: A C, there are two increment operators: ++ and --. The ++ operator increases the value of a variable by 1, while the -- operator decreases the value of a variable by 1.
Pre increment and Post increment Operator in C examples
Symbols | Operations | Examples |
---|
++ | Pre-Increment | a++
|
-- | Pre-Decrement | a--
|
++ | Post-Increment | ++a
|
-- | Post-Decrement | --a |
Increment Operator Simple Example in C
#include <stdio.h> int main() { int x = 10; x++; printf("x is %d\n", x); ++x; printf("x is %d\n", x); return 0; }
**********OUTPUT********** x is 11 x is 12 |
Decrement Operator Simple Example
#include <stdio.h> int main() { int x = 10; x--; printf("x is %d\n", x); --x; printf("x is %d\n", x); return 0; }
**********OUTPUT********** x is 9 x is 8 |
Increment or Decrement Operator Examples
#include <stdio.h> int main() { int x = 10; int y = 5; int z = x++ + y; int a = x-- + y; int b = ++x + y; int c = --x + y; return 0; }
**********OUTPUT********** z is 15 a is 16 b is 16 c is 15 |
Conditional Operator in C Programming
Conditional Operator: The conditional operator, also known as the ternary operator, is an operator in C that allows you to assign a value to a variable based on a certain condition. It is written as "condition ? value1 : value2", and it works as follows:
1. First, the condition is evaluated.
2. If the condition is true, the operator returns value1.
3. If the condition is false, the operator returns value2.
Conditional Operator in C Example
#include <stdio.h> #include<conio.h> int main() {
5>2?printf("Hello"):printf("Welcome"); }
**********OUTPUT********** Welcome |
Special Operators in C Example Program
Symbols | Operations | Examples |
---|
& | it is used find address int a=10; | printf("%d",&a); |
* | It is used pointer variable | int *p; |
Sizeof | It Used Memory Size | printf("%d",sizeof(a)); |
0 Comments