include stdioh include stdlibh int mainvoid char x a printfc x x x 3 printf c x

Include stdioh include stdlibh int mainvoid char x a

This preview shows page 6 - 8 out of 8 pages.

#include <stdio.h> #include <stdlib.h> int main(void) { char x = ’a’; printf("%c", x); x = x + 3; printf(" %c", x); x = x + ’A’ - ’a’ + 4; printf(" %c\n", x); return EXIT_SUCCESS; } COMP9021 (Principles of Programming) Notes 6.0: Variables, assignments, operators 2014 session 1 21 / 29 The cast operator An expression of the form ( type ) name_of_data_item creates a new data item of type type , whose value is obtained from the value of the data item whose name is name_of_data_item by applying the conversion rules that govern the behaviour of the assignment operator. We describe that effect of expression as casting name_of_data_item to type . For instance: (int)29.3 + (int)11.7 evaluates to 40 (int)(29.3 + 11.7) evaluates to 41 (int)29.3 + 11.7 evaluates to 40.7 (float)6 / (float)4 evaluates to 1.5 (float)(6 / 4) evaluates to 1.0 (float)6 / 4 evaluates to 1.5 COMP9021 (Principles of Programming) Notes 6.0: Variables, assignments, operators 2014 session 1 22 / 29 Increment and decrement operators (1) The unary increment and decrement operators increase and decrease the value of their operand by one, respectively. These operators come in two versions: prefix and postfix When used in simple statements, both versions are similar: a = 1, b = 1 is the output of #include <stdio.h> #include <stdlib.h> int main(void) { int a = 0, b = 0; a++; ++b; printf("a = %d, b = %d \n", a, b); return EXIT_SUCCESS; } COMP9021 (Principles of Programming) Notes 6.0: Variables, assignments, operators 2014 session 1 23 / 29 Increment and decrement operators (2) When one of these operators is part of a larger expression, the prefix version does its job before the expression is evaluated; the suffix version does its job after the expression is evaluated. This is illustrated in __crement.c (x * y)++ is invalid : these operators can only be applied to variables. These operators should not be applied to a variable that appears more than once in an expression: dealing with p = n * (n++ / 2) , some compilers would first evaluate n , others would first evaluate n++ / 2 , so the actual result cannot be known without knowing how the compiler works, which is not meant to be known as programmers should strive to write portable programs. COMP9021 (Principles of Programming) Notes 6.0: Variables, assignments, operators 2014 session 1 24 / 29
Image of page 6

Subscribe to view the full document.

Relational operators There are 6 relational operators: == for equality (not to be confused with the assignment operator, = ), and a number of operators for inequality, namely != , <= , < , > and >= , to test whether two values are distinct, or if one value is smaller than or equal to, strictly smaller than, strictly greater than, or greater than or equal to the other. Relational operators returns true or false . All these operators associate left-to-right, they all have lower precedence than the arithmetic operators, higher precedence than the assignment operators, with == and != of lowest, identical priority, and with the other four relational operators of higher, identical priority. For (an awful) example, true would be assigned to both a and b with bool a = 0 == 1 == 0; bool b = 0 == 3 < 0; COMP9021 (Principles of Programming) Notes 6.0: Variables, assignments, operators 2014 session 1 25 / 29 Boolean operators There are 3 boolean operators: the unary ! for negation , and the binary || and && for inclusive disjunction and conjunction , respectively.
Image of page 7
Image of page 8
  • Fall '19
  • 11

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes