1 / 7

Conversions of the type of the value of an expression

Learn the careful rules and best practices for handling type conversions in ANSI-C to reduce errors and improve code quality. Understand automatic and explicit type conversions, exceptions, and formatted numbers for output.

rkirchner
Download Presentation

Conversions of the type of the value of an expression

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. Conversions of the type of the value of an expression ANSI-C

  2. Careful rules to follow • You should know the type of the resulting expression • You should match the type of the resulting expression with the expected type of • The variable being assigned • The parameter being matched by the expression as argument • The type of the result of the function • Knowing that you matching or did not match it EXPLICITELY will reduce your errors.

  3. Autmatic type conversions for matching • If the type of the value of the expression does not match the type expected (by the variable, by the parameter, by the function return type) the C language will convert the type of the resulting value to the expected type. • This implicitely, automatic match performed by the language can produce be the source of hidden errors in your code.

  4. Type of an expression • When all the operands of an operation are of the same type, the resulting value will be of that type. • We can write in C expressions where the operands are of different types. How to compute it’s the type of the resulting value? Based on the type of the dominand operand • long double • double • float • unsigned • int

  5. Exceptions • For any expression involving only char, signed or unsigned char, short, or unsigned short, dominating type will be int or unsigned. • For any expression involving only long int and unsigned int, dominating type is implementation dependent; should be unsigned long int, or long int.

  6. Explicit conversions: Casting • The language will implicitely find the type of the value of a resulting expression (important when the expression has mixed type operands). • Sometimes programmer knows the type that the resulting value ought to have, and it can direct the machine to compute it: • (type) expression • Example: int x; int y; • x/y or (float) x / y • Important: the precedence of (cast) : lower than any unary operator but hiegher than any binary operator.

  7. Formatted numbers for output: • As you know you can specify the format for output values • %d • %c • %s • %f • %e • These can include a specification for field width: • %10d • %14.5f

More Related