180 likes | 196 Views
Learn the definition of expressions and operators in C programming language, including arithmetic, relational, logical, assignment, increment/decrement, and miscellaneous operators.
E N D
Expression: Definition • An expression is any legal combination of symbols that represents a value. • Each programming language and application has its own rules for what is legal and illegal. • For example, in the C language x+5 is an expression • Every expression consists of at least one operand and can have one or more operators. Operands are values, whereas operators are symbols that represent particular actions. In the expression • In x + 5, x and 5 are operands, and + is an operator.
Operator: Definition • An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. • C language is rich in built-in operators and provides the following types of operators − • Arithmetic Operators • Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators • Increment/decrement operator • Misc Operators
Arithmetic Operator • Operator that performs addition, subtraction, multiplication and division • C programming has following binary arithmetic operator • Arithmetic Unary operator are + and -
Arithmetic Operator precedence and associativity • If more than one operators are involved in an expression, C language has a predefined rule of priority for the operators. • This rule of priority of operators is called operator precedence. • i + j * k equivalent to i + (j * k) or (i + j) * k
Arithmetic Operator Precedence Highest + - (unary) * / % Lowest + - (binary) • Listed on same line same precedence
Arithmetic Operator Associativity • When operator with same precedence came then associativity matters • Left associative if it groups from left to right. Binary arithmetic operator are left associative • Right associative if it groups from right to left. The unary operator (+, -) are right associative
Assignment Operator • Assigns values from left to right variable • = is simple assignment operator • If different data type then left operand data type is converted to right.
Assignment Operator Contd.. • Assignment operator can be chained together Eg. i=j=k=0; • = is right associative thus- i=j=k=0 is equivalent to i=(j=(k=0)); • What is assigned to f? int I; float f; f=i=3.2f
Compound Assignment • Compound assignment allows to shorten statement • Common compound assignment operator are • Has same precedence as =
Increment and Decrement Operator • Increases and decreases value by 1. • Can simple done by using compound statement • i=i+1; i+=1; • j=j-1; j+=1; • But C provided ++ and -- operator which shorten even further • Can be used on two way with two different meaning prefix and postfix
Prefix and Postfix increment / decrement • Eg. • Same for -- operator
More Example • What are value of i, j and k ?
Expression Statement • Any expression by appending semicolon is turned into statement • i++; • i+j; (do nothing statement)
Other Operator • Relational and logical operator