C-Operators, Associativity and Precedence: A Group Report Submitted To: MR Sachin Dave
C-Operators, Associativity and Precedence: A Group Report Submitted To: MR Sachin Dave
By: Mansi Badheka Priyanka Banthia Akanksha Arora Chandni Baktarwala 2012-13
What is C?
C is a very flexible and powerful programming language originally designed in the early 1970s. It is famous as the language the UNIX operating system was written in, and was specially designed for this. However its use has now spread way beyond that field and it is currently very popular.
What is a C operator?
Operators are functions. Specifically, operators are special functions that are used to perform operations on data without directly calling methods each time. Common operators that you should be familiar with include +, -, *, /, <<, >>, and so on. All such operators can be used on primitive data types in C to achieve certain results. For example, we can use the + operator to add together two numeric values, such as ints, floats.
Types of operators
Arithmetic operators include the familiar addition (+), subtraction (-), multiplication (*) and division (/) operations. In addition there is the modulus operator (%) which gives the remainder left over from a division operation. a + b = 103 a - b = 97 a * b = 300 a / b = 33 a%b=1 Logical operators allow the program to make decisions based on multiple criteria, without using nested conditionals. They are known as shortcircuit because they skip evaluating their right argument if evaluating their left argument is sufficient to determine the overall value.
Assignment Operators: The assignment operator is + is used to assign a constant, value of variable or expression to a variable on its left. This is written as follows. Variable = expression; Many times it happens that the variable on left side of the assignment operator is repeated on right side also. In such situation the short-hand assignment operators provided iin C. for example X += 10; Where += is short-hand assignment operator. Hence, X += 10; Is short form of X = x+10;
The arithmetic operators have left-to-right associativity, and multiplication and division have higher precedence than addition. So starting from the left of the equation, the highest precedence operator is multiplication, so 3 * 6 (= 18) is performed, then 18 / 2 (= 9) is performed. The expression is evaluated again from the left with the next level of precedence being addition, so 8 + 9 (= 17) is performed. Finally, the right side of the assignment operator is assigned to variable x