1 / 116

Basic Features of C (Review)

Basic Features of C (Review). C Philosophy. Low-level language System programming Small language Library of standard “functions” Permissive language Doesn’t require the detailed error-checking. C Strengths. Efficiency Limited amount of memory Fast Portability

yeriel
Download Presentation

Basic Features of C (Review)

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. Basic Features of C (Review)

  2. C Philosophy • Low-level language • System programming • Small language • Library of standard “functions” • Permissive language • Doesn’t require the detailed error-checking

  3. C Strengths • Efficiency • Limited amount of memory • Fast • Portability • Compilers are small and easily written • C: UNIX and ANSI/ISO standard • Power • Flexibility • Standard library • Input/output, string handling, storage allocation, etc. • Integration with UNIX

  4. C Weakness • Can be error-prone • Flexibility • C compiler doesn’t detect many programming mistakes • Pitfalls • Can be difficult to understand • Some features are easier to be misused • Flexibility • Can be difficult to modify • No modules

  5. Compiling and Linking

  6. Variables and Assignments • C is case-sensitive • Compiler remembers only first 31 characters • Type • Should be declared • int height, length, width; • Declarations must precede the statements. • The value should be assigned before using the variable in computations: • height = 8; • length = 12; • width = 5; • int volume = height * length * width;

  7. Constants • Macro definition: • #define SCALE_FACTOR (5.0/9.0) • No semicolon at the end!

  8. Formatted Output: printf • printf(string, expr1, expr2,…) • Format string contains both ordinary characters and conversion specifications • Conversion specification is a placeholder representing a value to be filled in during printing. • The information after % specifies how the value is converted form its internal form(binary) to printed form (characters)

  9. Pitfalls

  10. Formatted Input: scanf • scanf(string, expr1, expr2,…) • Reads input according to a particular format. • Format string contains both ordinary characters and conversion specifications • scanf(“%d%d%f%f”, &i, &j, &x, &y); • Number of conversion specification should match number of variables. • Each conversion should be appropriate for type of the variable. • while (scanf (“%d”, &i)==1) { … }

  11. How scanf Works • For each conversion specification, tries to locate an item of appropriate type, skipping blank spaces if necessary. • Reads it, stopping when it encounters the symbol that can’t belong o item. • Ignores white-space characters. • scanf(“%d%d%f%f”, &i, &j, &x, &y); 1 -20 .3 -4.0e3 •  1-20   .3      -4.0e3  • 1-20 .3 -4.0e3

  12. Arithmetic Operators • Unary operators: + (unary plus), - (unary minus) • Binary operators

  13. Assignment Operators • Simple assignment(right associative): = • inti=5, j=3; • int k = 5*3; • i = 72.99; • float f; • f = 136; • i = j = k = 7; • f = i = 33.3; • k = 1 + (j = i); • Compound assignments(right associative): +=, -=, *=, /=, %= • i += j += k; • i += k; vsi =+ k;

  14. Increment and Decrement Operators • Prefix operators: ++i, --i • Postfix operators: i++, i— • Side effect: • x=1, y=2; • k=1 + ++x; vs k = 1+ x++; • k = ++x + y++; vs k = x++ + y++;

  15. Precedence and Associativity

  16. Sub expression Evaluation • a = 5; c = (b = a + 2) – (a = 1); • i = 2; j = i * i ++; • Any expression can be used as a statement • i++; • i*j -1; • i + j ; /* i = j */

  17. Relational Operators • 0 (false) and 1 (true) • Their precedence is lower than the precedence of the arithmetic operators. • Left associative • i < j < k

  18. Equality Operators • 0 (false) and 1 (true) • Their precedence is lower than the precedence of the relational operators. • i < j == j < k • Left associative

  19. Logical Operators • Logical negation: ! • !expr : 1 if expr has the value 0 • Right associative • The same precedence as unary plus and minus • Logical and: && • expr1 && expr2 : 1 if both expr1 and expr2 has non-zero values • Logical or: || • expr1 || expr2 : 1 if either expr1 or expr2 (or both) has non-zero values • Short-circuit evaluation • Left associative • The precedence is lower that that of the relational and equality operators

  20. if … else Statement • if (expression) statement • if (expression) statement else statement • Statement can be compound: { statements} • if (i==0) vs if (i=0) • if (expression) statement else if (expression) statement … else statement

  21. “Dangling else” Problem if (y != 0) if (x != 0) result = x / y; else printf (“Error: y is equal to ) \n”); • y = 5 and x = 3 • y = 0 and x =3 • y = 5 and x = 0

  22. Conditional Expression • expr1 ? expr2: expr3 • int i, j, k; i = 1; j = 2; k = i > j ? i : j ; k = (i > 0 ? i : 0) + j ; • return (i > j ? i : j); • printf (“%d\n”, i > j ? i : j);

  23. switch Statement • switch ( expression ){ case constant-expression: statements … case constant-expression: statements default: statements } • Controlling expression should be an integer expression (characters) • Constant expression can’t contain variables or function calls. • Statements do not require {}. Usually, the last statement is break.

  24. Example

  25. while Loop • while (expression) statement • Statement can be compound: {} • while (i>0) printf (“%d\n”, i--); • while (i>0) { printf (“%d\n”, i); i--; } • Infinite loops: while(1) • Break, goto, return

  26. Example scanf(“%d”, &n);

  27. do Loop • do statement while (expression); • Statement can be compound: {} • do printf (“%d\n”, i--); while (i>0) ; • do { printf (“%d\n”, i); i--; } while (i>0);

  28. for Loop • for (expr1; expr2; expr3) statement • Statement can be compound: {} • expr1; while (expr2) { statement expr3; } • for (i=10; i>0; i--) printf (“%d\n”, i); • Infinite loop: for (;;) • Comma operator: • for (i=1, j=2; i+j<10; i++, j++) printf (“%d\n”, i+j);

  29. Example

  30. Exiting From a Loop: break • for (d=2; d<n; d++) if (n%d==0) break; if (d<n) printf (“%d is divisible by %d\n”, n,d); else printf(“%d is prime \n”,n); • for (;;){ printf (“Enter a number(0 to stop): ”); scanf(“%d”,&n); if (n==0) break; printf(“%d cubed is %d\n”,n,n*n*n); } • break escapes only one level of nesting.

  31. Skipping the Rest of Iteration: continue • n = 10; sum = 0; while (n-->0){ scanf(“%d”, &i); if (i%2==0) continue; sum+=i; }

  32. Unconditional Jump: goto • goto label ; • label: statement • for (d=2; d<n; d++) if (n%d==0) goto done; done: if (d<n) printf (“%d is divisible by %d\n”, n,d); else printf(“%d is prime \n”,n);

  33. Example while(expr1){ switch(expr2){ case constant_expression1: statement; break; … case constant_expression1: statement; if (expr3) goto while_done; break; default: break; } } while_done: ….

  34. Null statement • ; • for (d=2; d<n; d++) if (n%d==0) break; • for (d=2; d<n && n%d !=0 ; d++); • Accidentally putting a semicolon after the parentheses in if, while or for statement ends the statement prematurely. • if (i==0); printf (“Zero\n”); • while (i>0); printf (“%d\n”, i--);

  35. Basic Types: Integers • Signedness: signed (defaut), unsigned • Size: short, long • <limits.h> holds ranges for int types.

  36. Integer Constants • Decimal (base 10) literals • digits between 0-9, no leading zero • 15, 255, 32767 • Octal (base 8) literals • digits between 0-7, must start with 0 • 017, 0377, 077777 • Hexadecimal (base 16) literals • digits between 0-9 and letters between A-F (a-f), must start with 0x (0X) • 0xF, 0xFF, 0x7FFF • Long literals: 15L, 0377L, 0x7fffL • Unsigned literals: 15U, 0377U, 0x7ffUL

  37. Basic Types: Floating Types • <float.h> • Assume IEEE 754 standard • Scientific notation: sign, an exponent, a fraction • .57e2, 57, 5.7e+1, 570.0e-1

  38. Basic Types: char • Character set: Latin (7 bit), ASCII (8 bit) • Treats as integers • unsigned (0-255) and signed (-128-127) version • Some compilers use unsigned by default, the other compilers use signed by default. • char ch=65; /* it’s ‘A’ now */ int i = ‘a’; /* it’s 97 */ ch++; /* it’s ‘B’ now */ • if (‘a’< =ch && ch <=‘z’) ch = ch – ‘a’ + ‘A’; /* ch=toupper(ch);*/ • for (ch=‘A’; ch<=‘Z’; ch++) … • ch=‘a’ * ‘b’ / ‘c’ …

  39. Read and Write char: Alternative • ch = getchar(); • putchar(ch); • while ( ( ch = getchar() ) != ‘\n’ ) … • while ((ch=getchar())==‘ ’); • printf(“Enter an integer: ”); scanf(“%d”, &i); printf(“Enter a command: ”); command = getchar();

  40. sizeof Operator • sizeof (type-name) • Unsigned integer representing the number of bytes required to store a value of type-name • sizeof(char) is always 1 • Can be applied to constants, variables, expressions • int i, j; int k= sizeof(i); /* k is assigned 2*/ k = sizeof (i + j );

  41. Implicit Type Conversion • Convert operands to the “narrowest” type that will safely accommodate both values. • If the type of either operand is a floating point: float -> double - > long double • Otherwise: if there are short and char operands, convert them to int, then int -> unsigned int -> long int -> unsigned long int • int i= -10; unsigned int u=10; if (i < u) …

  42. Conversion During Assignment • char c = ‘A’; int ind; float f; double d; i = c; /* will get 65 */ f = i; /* will get 65.0 */ d = f; i = 824.97; /* 824 */ c= 100000000; f = 1.0e1000;

  43. Explicit Type Conversion: cast • (type-name) expression • Unary operator • float f = 3.45, frac; frac = f – (int) f; • int num1=5, num2 =3; float quotient = (float) num1/ num2; • int i=1000; long int i = (long int) j * j; long int i = (long int) (j * j)

  44. One –Dimensional Array • Data structure containing a number of values of the same type. • Declare array: type, name and number of elements • int a[10] • Subscripting: • for (i=0; i<N; i++) a[i]=0; • for (i=0; i<N; i++) scanf (“%d”, &a[i]); • i=0; while (i<N) a[i++] = 0; • const int month[]={31,28,31,30,31,30,31,31,30,31,30,31};

More Related