What is operator?

 Operators in programming are symbols that represent computations or operations on variables and values. They define how variables and values interact with each other. Here are some common types of operators:


1.Arithmetic Operators:

+ (Addition): Adds two values together       Example: a + b.      
  
- (Subtraction): Subtracts the right operand from the left operand.           Example: a - b.

* (Multiplication): Multiplies two values.       Example: a * b.

/ (Division): Divides the left operand by the right operand.      Example: a / b.

% (Modulus): Returns the remainder of the division of the left operand by the right operand.        Example: a % b


2.Assignment Operator:

  • = (Assignment): Assigns the value on the right to the variable on the left.            Example: x = 10
  • Comparison Operators:== (Equal to): Checks if the values of two operands are equal.     Example: a == b
  • != (Not equal to): Checks if the values of two operands are not equal.          Example: a != b
  • < (Less than): Checks if the value of the left operand is less than the value of the right operand.  Example: a < b>
  •  (Greater than): Checks if the value of the left operand is greater than the value of the right operand.                                                           Example: a > b
  • <= (Less than or equal to): Checks if the value of the left operand is less than or equal to the value of the right operand.                         Example: a <= b
  • >= (Greater than or equal to): Checks if the value of the left operand is greater than or equal to the value of the right operand.    Example: a >= b

3.Logical Operators:

  • && (Logical AND): Returns true if both operands are true.                                                          Example: x && y||
  •  (Logical OR): Returns true if at least one of the operands is true.                                          Example: x || y.!
  •  (Logical NOT): Returns true if the operand is false and vice versa.                                     Example: !x.

4.Increment and Decrement Operators:

  • ++ (Increment): Increases the value of a variable by 1                                                 Example: a++
  • -- (Decrement): Decreases the value of a variable by 1.                                                 Example: a--

5.Bitwise Operators:

  • & (Bitwise AND): Performs a bitwise AND operation.                                                              Example: a & b.
  • | (Bitwise OR): Performs a bitwise OR operation                                                          Example: a | b
  • ^ (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.                        Example: a ^ b
  • << (Left shift): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.                                                                          Example: a << 2
  • >> (Right shift): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.                                                                        Example: a >> 2


Here's a simple example that demonstrates the use of variables, data types, and operators in C:



#include <stdio.h>

int main() 

{

    int a = 10;

    int b = 5;

    int result;

    // Perform some operations

    result = a + b;

    printf("a + b = %d\n", result);

    result = a - b;

    printf("a - b = %d\n", result);

    float pi = 3.141592;

    double radius = 2.5;

    double area = pi * radius * radius;

    printf("Area of a circle with radius %.2lf:%.2lf\n", radius, area);


    return 0;

}

In this example, we declare integer variables a and b, perform addition and subtraction, and calculate the area of a circle using floating-point numbers. The printf function is used to display the results on the screen.

#include <stdio.h>

This line includes the standard input-output library (stdio.h), which provides functions for input and output operations. 

In this code, it's used for the printf function, which is used to display text on the screen.

int main() {

This line defines the main function. In C, every program must have a main function, which serves as the entry point for the program. The int before main specifies the return type of the function, which in this case, indicates that the program should return an integer value when it's done executing.

int a = 10;

 int b = 5;

 int result;

These lines declare and initialize three integer variables: a, b, and result.

a is assigned the value 10.

b is assigned the value 5.

result is declared without an initial value. It will be used to store the results of arithmetic operations.


result = a + b;

   printf("a + b = %d\n", result);

Here, we perform an addition operation between the values of a and b, and store the result in the result variable. Then, we use the printf function to display the result on the screen. %d is a format specifier for integers, and it is replaced with the value of result in the output.

result = a - b;

 printf("a - b = %d\n", result);

Similarly, we perform a subtraction operation between the values of a and b, store the result in the result variable, and print it using printf.

float pi = 3.141592;

    double radius = 2.5;

    double area = pi * radius * radius;

    printf("Area of a circle with radius %.2lf: %.2lf\n", radius, area);

In this part, we introduce floating-point variables (pi and radius) and calculate the area of a circle using the formula area = π * r * r, where π (pi) is a constant with a floating-point value. The printf function is used to display the result, with format specifiers (%.2lf) to format the output.

return 0;

}

Finally, the return 0; statement indicates that the program has finished executing, and it returns an exit status of 0 to the operating system, indicating a successful execution.

When you compile and run this code, you should see output on the screen that displays the results of the addition, subtraction, and the calculated area of a circle. This example illustrates the basic usage of variables, data types, and operators in C for performing simple mathematical operations.