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
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Example:
int a = 10, b = 3;
printf("%d\n", a + b);
printf("%d\n", a % b);
2. Relational Operators
| Operator | Meaning |
|---|---|
== | 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
| Operator | Meaning |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
Example:
if (age >= 18 && age <= 60)
{
printf("Eligible");
}
4. Assignment Operators
| Operator | Example | Meaning |
|---|---|---|
= | a = 5 | Assign |
+= | a += 2 | Add and assign |
-= | a -= 2 | Subtract and assign |
*= | a *= 2 | Multiply and assign |
/= | a /= 2 | Divide and assign |
%= | a %= 2 | Modulus and assign |
5. Increment and Decrement Operators
| Operator | Meaning |
|---|---|
++ | Increment by 1 |
-- | Decrement by 1 |
Example:
int x = 5;
x++;
x--;
6. Bitwise Operators
| Operator | Meaning |
|---|---|
& | 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 <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), andconstvariables. - 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
sizeofOperator
Category: MCA
