Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never.

Similar presentations


Presentation on theme: "Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never."— Presentation transcript:

1 Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never use an expression such as: 5 += A, It will make no sense.

2 Unary and Binary Operators Prefix and unary + - ++ -- Right to Left (Associativity rule) int X=5, Y; Y = -X++; After running the program statements, Y = ? X = ? Week04 pre.c

3 Type Conversion in C When an operator is applied to two variables of the different type will result in an error in most program languages. C has a clear set of rules that govern most cases. ** In general, when we have two different types, the “most restrictive” type is converted into the “least restrictive” type.**

4 Types for different numbers in C 1. int x - 32 bits - or 16 bits x = ?(range) ** Multiply or divide two integer in range can cause out of range error in C so that it will cause fatal errors in some situations** (in most other programs, it will give you a run time error and terminate the program)

5 Long and Short Integers Typically, short int is 16 bits -- %hd long int is 32 bits -- %/ld Nowadays, it is not a good idea to use short int in any situation. The main advantage of using type int is to speed up processing time.

6 Real World --- Real Number Floating-Point Type and Constant In the real world, real numbers dominate. double -- normally eight bytes memory is reserved for a double. --- %f %lf -- ** A decimal point or exponential part must be supplied when representing floating point constant. ** float, double, and long double double is the default type in most of the C

7 “String Constant in C” One token rule: “this” “is” “a” “test” will be interpreted as “thisisatest” /* */ inside a string no longer mean a comment. Week04 one_token.c

8 Octal and Hexadecimal Numbers We use 0 in front a number to represent an octal number, such as 0377 is 255 decimal. We use 0x to represent a hexadecimal number, such as 0xff which is 255 decimal. In printf, %o and %x are used to represent the octal and hexadecimal respectively. **Leading 0 not be used in any number if you don’t mean to use an octal number**

9 Bitwise operations ~ -- bitwise-negation >> -- shift right << -- shift left & -- bitwise-AND | -- bitwise-OR ^ -- bitwise -- XOR

10 sizeof operator It gives the number of bytes associated with a specified type or a variable. Example: size_of.c week04 sizeof2.c week04

11 Example Week04 bit.c

12 Header file limit.h Example: weeko4 limit.c

13 Example #include main (void) { int A, B=5, C=8; A = B++ + C++; printf (“A = %d”, A; “B= %d”, B, “C= %d\n”, C); A = B++ + ++C; printf (“A = %d”, A; “B= %d”, B, “C= %d\n”, C); A = ++B + C++; printf (“A = %d”, A; “B= %d”, B, “C= %d\n”, C); A = ++B + ++C; printf (“A = %d”, A; “B= %d”, B, “C= %d\n”, C); return 0; }


Download ppt "Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never."

Similar presentations


Ads by Google