0% found this document useful (0 votes)
10 views

Explain Operator and Expressions in c Programming

The document provides an overview of operators and expressions in C programming, defining operators as symbols that perform operations on operands. It categorizes operators into arithmetic, relational, logical, assignment, bitwise, conditional, and special operators, and explains the types of expressions such as constant, arithmetic, relational, logical, assignment, bitwise, function call, and conditional expressions. Additionally, it discusses operator precedence and associativity, emphasizing the importance of using parentheses for clarity in expressions.

Uploaded by

kk1982johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Explain Operator and Expressions in c Programming

The document provides an overview of operators and expressions in C programming, defining operators as symbols that perform operations on operands. It categorizes operators into arithmetic, relational, logical, assignment, bitwise, conditional, and special operators, and explains the types of expressions such as constant, arithmetic, relational, logical, assignment, bitwise, function call, and conditional expressions. Additionally, it discusses operator precedence and associativity, emphasizing the importance of using parentheses for clarity in expressions.

Uploaded by

kk1982johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Okay, let's discuss operators and expressions in C programming, keeping in mind our current

context (Tuesday, April 15, 2025, 11:39 AM IST, Bhubaneswar, Odisha, India). These are
fundamental concepts for performing computations and making decisions in your C programs.
1. Operators in C:
●​ Definition: Operators are special symbols that perform specific operations on one or
more operands (variables, constants, or other expressions). They are the verbs of the C
language, allowing you to manipulate data.
●​ Categories of Operators in C:
○​ Arithmetic Operators: These perform mathematical calculations.
■​ + (Addition)
■​ - (Subtraction)
■​ * (Multiplication)
■​ / (Division)
■​ % (Modulo - gives the remainder of integer division)
■​ ++ (Increment - increases the operand by 1)
■​ -- (Decrement - decreases the operand by 1)
int a = 10, b = 5;​
int sum = a + b; // sum is 15​
int difference = a - b; // difference is 5​
int product = a * b; // product is 50​
int quotient = a / b; // quotient is 2​
int remainder = a % b; // remainder is 0​
a++; // a becomes 11​
b--; // b becomes 4​

○​ Relational Operators: These compare two operands and return a boolean value
(true or false), represented as 1 (true) or 0 (false) in C.
■​ == (Equal to)
■​ != (Not equal to)
■​ > (Greater than)
■​ < (Less than)
■​ >= (Greater than or equal to)
■​ <= (Less than or equal to)
int x = 7, y = 12;​
int isEqual = (x == y); // isEqual is 0 (false)​
int isNotEqual = (x != y); // isNotEqual is 1 (true)​
int isGreater = (y > x); // isGreater is 1 (true)​
int isLessOrEqual = (x <= 7); // isLessOrEqual is 1 (true)​

○​ Logical Operators: These combine or modify boolean expressions.


■​ && (Logical AND) - true if both operands are true.
■​ || (Logical OR) - true if at least one operand is true.
■​ ! (Logical NOT) - true if the operand is false, and false if the operand is true.
int p = 1, q = 0;​
int andResult = (p && q); // andResult is 0 (false)​
int orResult = (p || q); // orResult is 1 (true)​
int notP = !p; // notP is 0 (false)​
int notQ = !q; // notQ is 1 (true)​
○​ Assignment Operators: These assign a value to a variable.
■​ = (Simple assignment)
■​ += (Add and assign)
■​ -= (Subtract and assign)
■​ *= (Multiply and assign)
■​ /= (Divide and assign)
■​ %= (Modulo and assign)
■​ <<= (Left shift and assign)
■​ >>= (Right shift and assign)
■​ &= (Bitwise AND and assign)
■​ |= (Bitwise OR and assign)
■​ ^= (Bitwise XOR and assign)
int num = 10;​
num = 20; // num is now 20​
num += 5; // num is now 25 (num = num + 5)​
num *= 2; // num is now 50 (num = num * 2)​

○​ Bitwise Operators: These perform operations at the level of individual bits of


integer operands.
■​ & (Bitwise AND)
■​ | (Bitwise OR)
■​ ^ (Bitwise XOR - exclusive OR)
■​ ~ (Bitwise NOT - one's complement)
■​ << (Left shift)
■​ >> (Right shift)
unsigned char a = 5; // Binary: 00000101​
unsigned char b = 3; // Binary: 00000011​
unsigned char andResult = a & b; // Binary: 00000001
(decimal 1)​
unsigned char orResult = a | b; // Binary: 00000111
(decimal 7)​
unsigned char notA = ~a; // Binary: 11111010
(decimal 250 on an 8-bit unsigned)​
unsigned char leftShift = a << 2; // Binary: 00010100
(decimal 20)​

○​ Conditional (Ternary) Operator: A shorthand for a simple if-else statement.


■​ condition ? expression_if_true : expression_if_false
int age = 20;​
char *status = (age >= 18) ? "Adult" : "Minor"; // status
will be "Adult"​

○​ Special Operators:
■​ sizeof(): Returns the size (in bytes) of a variable or data type.
■​ & (Address-of operator): Returns the memory address of a variable.
■​ * (Dereference operator): Accesses the value at a memory address (used
with pointers).
■​ . (Member access operator): Accesses members of a structure or union.
■​ -> (Pointer to member access operator): Accesses members of a structure or
union through a pointer.
■​ , (Comma operator): Separates expressions in a list (the value of the entire
expression is the value of the rightmost expression).
2. Expressions in C:
●​ Definition: An expression is a combination of operands (variables, constants, function
calls) and operators that evaluates to a single value.
●​ Types of Expressions:
○​ Constant Expressions: Consist only of constant values (literals) and constant
operators. Their value is determined at compile time.​
5 + 3 // Evaluates to 8​
'A' // Evaluates to the ASCII value of 'A'​
3.14 * 2 // Evaluates to 6.28​

○​ Arithmetic Expressions: Combine arithmetic operators and numeric operands.​


a + b * c​
(x - y) / z​

○​ Relational Expressions: Use relational operators and evaluate to 0 or 1.​


i < max​
count == 0​

○​ Logical Expressions: Combine logical operators and boolean (or integer


interpreted as boolean) operands.​
(age > 18) && (hasLicense == 1)​
!isValid​

○​ Assignment Expressions: Use assignment operators. The value of an assignment


expression is the value assigned to the left operand.​
value = 10​
sum += 5​

○​ Bitwise Expressions: Use bitwise operators on integer operands.​


mask & data​
value << 3​

○​ Function Call Expressions: Invoke a function and evaluate to the value returned
by the function.​
sqrt(25.0) // Evaluates to 5.0​
printf("Hello") // Evaluates to the number of characters
printed (though often the return value is ignored)​

○​ Conditional Expressions: Use the ternary operator.​


(a > b) ? a : b // Evaluates to the larger of a and b​

3. Operator Precedence and Associativity:


●​ Precedence: Determines the order in which operators are evaluated in an expression.
Operators with higher precedence are evaluated before those with lower precedence. For
example, multiplication and division have higher precedence than addition and
subtraction.
●​ Associativity: Determines the order of evaluation for operators with the same
precedence (either left-to-right or right-to-left). For example, most binary arithmetic
operators are left-associative (a - b - c is evaluated as (a - b) - c), while the assignment
operator is right-associative (x = y = 5 is evaluated as x = (y = 5)).
●​ Using Parentheses: You can use parentheses () to explicitly control the order of
evaluation in an expression, overriding the default precedence and associativity. This is
highly recommended for clarity, even when the default order is what you intend.​
int result = (a + b) * c; // Parentheses force addition to happen
before multiplication​

Understanding operators and expressions is crucial for writing C programs that can perform
calculations, make comparisons, and manipulate data effectively right here in Bhubaneswar, or
anywhere your C code might run! They are the building blocks of the logic and functionality of
your programs.

You might also like