1 / 29

Declarations and Expressions

Learn about variable declaration, operators, expressions, types, constants, and more in C programming with informative examples and rules. Delve into variable initialization, arrays, logic operators, bit manipulation, assignments, type conversions, and best practices.

hazard
Download Presentation

Declarations and Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

  2. Overview • Variable declaration • the way to introduce variables into programs • Operators • do some useful operations on variables and constants • Expressions • form complex expressions with variables, operators, constants and other expressions

  3. Variables • Variable formation rules: • consists of one or more letter or digit, (“_” counts as a letter) • starts with a letter • lower and upper cases are distinct • different from key words • It’s common practice not to start with “_” • variable names in C library • Choose informative variable names • Compare “sldj_wdwp_” with “name”

  4. Types and Constants

  5. Types and Constants(2)

  6. Variable Declaration • C obeys the variable use-after-declaration rule • Variable declarations consists of these parts: • Type name • Variable list (may only one) • initialization • Ex: int x, y, z; double f; char c = ‘c’; • Type qualifiers: • signed, unsigned (default is signed), const

  7. Sample Variable Declaration int low, high; char line[1000]; double pi = 3.14; const double e = 2.71; const char digits[] = “0123456789abcdef”;

  8. Variable Declaration • A declaration makes clear a variable’s type, size and allocates memory for it int i, j; $#*@? ?%*

  9. Variable Initialization • Programmers’ duty to initialize that memory space int i, j; i = 9; 9 ?%*

  10. Array Declaration int i, j; i = 9; int a[5]; 9 ?%* a $? #@ !~ &^ @$

  11. Array Initialization int i, j; i = 9; int a[5]; for (int j=0; j<5; j++) a[j] = j*j; 9 ?%* a 0 1 4 9 16

  12. Arithmetic Operators

  13. Relational and Logical Operators

  14. Relational and Logical Operators • Boolean operators are short-circuit • Computations stop as soon as true or false is known x = 9; if ((3>2) || (x=8)); // what’s x’s value? 8 or 9?

  15. Relational and Logical Operators // another example on && x = 9; if ((1<0) && (x=8)); // what’s x’s value? 8 or 9? // a more fancy example int i = 0; int a[10]; while (i<10 && a[i]!=0){ … }

  16. “!” • Not “!” converts 0 to 1, and non-zero values to 0 if (!a){ … } // reads “if a does not hold”, then … // be equivalent with if (0 == a) { … } // we’d see more examples later

  17. Increment and Decrement int n = 9; int x; x = ++n; // x == 10 // n == 10 int n = 9; int x; x = n++; // x == 9 // n == 10

  18. Bitwise Operators

  19. Bitwise Operators Example // count num of 1’s in an integer x int count (int x) { int num = 0; unsigned int y = (unsigned int)x; while (y) { if (y & 0x1) num++; y >>= 1; } return num; }

  20. Assignment Operators and Expressions • Assignment is a special kind of expression, so the following code fragment is legal int x; printf (“%d\n”, x=999); • Other kind of assignment expressions are of the form: e1 op= e2 • Equivalent to: e1 = e1 op e2 • Example: x += 9; ====> x = x + 9; • See the text for a complete list of op

  21. Assignment Operators and Expressions // more examples on assignment operators: int a, b, c; a = b = 9; a = (b = 9) + (c = 8); a = (b = 9) / (c = 3); // even as the arguments of function calls: int a, b; x = sum (a = 1, b = 2);

  22. Conditional Expressions if (a > b) max = a; else max = b; // C provides a more succinct way to do this: max = (a>b) ? a : b; // the general form is: e1 ? e2 : e3; // if e1 evaluates to true, then evaluates e2, // else evaluates e3.

  23. Type Conversion (Cast) • When the type of some expression is not compatible with the one expected, automatic type conversion occurs • Example from our previous code: // all of c, ‘0’ and ‘9’ are automatic converted // into integer values by the compiler: if ((c>=‘0’) && (c<=‘9’)) a[c-’0’]++;

  24. Type Conversion (Cast) • Generally, there are two kinds of automatic cast: • cast a “big” into “smaller” ones • long ==> int • cast a “small” into “bigger” ones • char ==> int • The former is not always safe

  25. Type Conversion (Cast) • The general scheme for “safe” cast is: double float long unsigned int char

  26. Type Conversion (Cast) // Dirty simple? However, it’s more subtle than // it first looks. Consider: #include <stdio.h> int main () { int i = -1; unsigned int j = 0; if (i < j) printf (“Never reach here, :-( \n”); else printf (“Shoot myself in the foot\n”); return 0; }

  27. Type Conversion (Cast) • Don’t expect the compiler will always behave as you desire • Two general principals: • Always use the C’s type system as strong as possible • It’s your basic protection • One reason for strong type system’s popularity • Make use of C’s type conversion explicitly • To make clear what we are doing

  28. Explicit Type Cast • General form of explicit type conversion: (type) expression // which converts the expression’s type to type // More examples: int i = (int)3333.14; char c = (char)i; int a[10]; a[(int)2.71] = 99; • As we see, data precision may be changed

  29. Safety Issue • Type conversion is an infamous source of C programs bugs • especially with pointers • (int *)999 will send your passwd to BillG… • Any serious and well-designed C program should use type conversion really rarely • General principle: • do NOT use it, • always use the explicit form

More Related