Constants and Operators in C

Constants

A constant is a fixed value that does not change during the execution of a program.

Types of Constants

1. Integer Constants

Whole numbers without a decimal point.

Examples:

10
-25
1000

2. Floating-Point Constants

Numbers containing a decimal point.

Examples:

3.14
25.75
-0.5

3. Character Constants

A single character enclosed in single quotes.

Examples:

'A'
'Z'
'5'

4. String Constants

A sequence of characters enclosed in double quotes.

Examples:

"Hello"
"C Programming"

5. Symbolic Constants

Defined using the #define preprocessor directive.

Syntax:

#define CONSTANT_NAME value

Example:

#define PI 3.14159
#define MAX 100

6. Constant Variables

Declared using the const keyword.

Example:

const int DAYS = 7;

Operators in C

An operator is a symbol used to perform operations on variables and constants.

1. Arithmetic Operators

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

Example:

int a = 10, b = 3;

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

2. Relational Operators

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example:

if (a > b)
{
    printf("a is greater");
}

3. Logical Operators

OperatorMeaning
&&Logical AND
`
!Logical NOT

Example:

if (age >= 18 &amp;&amp; age &lt;= 60)
{
    printf("Eligible");
}

4. Assignment Operators

OperatorExampleMeaning
=a = 5Assign
+=a += 2Add and assign
-=a -= 2Subtract and assign
*=a *= 2Multiply and assign
/=a /= 2Divide and assign
%=a %= 2Modulus and assign

5. Increment and Decrement Operators

OperatorMeaning
++Increment by 1
--Decrement by 1

Example:

int x = 5;

x++;
x--;

6. Bitwise Operators

OperatorMeaning
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

7. Conditional (Ternary) Operator

Syntax:

condition ? expression1 : expression2;

Example:

int max = (a > b) ? a : b;

8. sizeof Operator

Returns the size (in bytes) of a data type or variable.

Example:

printf("%zu", sizeof(int));

Example Program

#include &lt;stdio.h>

#define PI 3.14159

int main()
{
    const int DAYS = 7;

    int a = 10, b = 5;

    printf("Addition = %d\n", a + b);
    printf("Subtraction = %d\n", a - b);
    printf("Multiplication = %d\n", a * b);
    printf("Division = %d\n", a / b);
    printf("Remainder = %d\n", a % b);

    if (a > b)
        printf("a is greater than b\n");

    printf("PI = %.2f\n", PI);
    printf("Days = %d\n", DAYS);

    return 0;
}

Output

Addition = 15
Subtraction = 5
Multiplication = 50
Division = 2
Remainder = 0
a is greater than b
PI = 3.14
Days = 7

Summary

  • Constants are fixed values that cannot be changed during program execution.
  • Common types of constants are integer, floating-point, character, string, symbolic (#define), and const variables.
  • Operators perform operations on variables and constants.
  • The major categories of operators in C are:
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Assignment Operators
    • Increment/Decrement Operators
    • Bitwise Operators
    • Conditional (Ternary) Operator
    • sizeof Operator

Related Posts