Scalar Data Types Part (A

Scalar Data Types Part (A

Programming for MSc Part I Part 2: Scalar Data Types (a) What Scalar Types are C Type Hierarchy C Types void Scalar Function Union Aggregate Types Types Types Types Pointer Arithmetic Array Structure Types Types Types Types Integral Floating−point Types Types Enumerated Character Types Types −→ Scalar Types are the different kinds of Numbers Herbert Martin Dietze <[email protected]> 27 Programming for MSc Part I Part 2: Scalar Data Types — What Scalar Types are Scalar Types Categories ScalarType: ArithmeticType | PointerType ArithmeticType: IntegralType | FloatingPointType IntegralType: EnumeratedType | CharacterType Integer Types • There are integer types of different length and signedness • Signedness: a type can represent negative numbers • The size of integer types is implementation defined • Example: int is 32 bit on many systems Different integer types: Name Signed Comment int Yes The standard type in C short int Yes Or: short char ? Signedness depends on compiler signed char Yes long int Yes Or: long, is not always longer than int unsigned int No Or: unsigned unsigned short int No Or: unsigned short unsigned long int No Or: unsigned long unsigned char No −→ Rule of thumb: Use int whenever possible! Herbert Martin Dietze <[email protected]> 28 Programming for MSc Part I Part 2: Scalar Data Types — What Scalar Types are Overflow and Underflow Integer numbers’ binary representation, e.g. signed char: 10 = 21 + 23 = 0000 1010 On most computers negative numbers are encoded using the two’s complement: −10 = invert(10) + 1 = 1111 0110 −→ What happens when adding one to 0111 1110? #include <stdio.h> #include <limits.h> int main (void) { printf "%d, %d\n", INT_MIN - 1, INT_MAX + 1); return 0; } Limits • The header file limits.h contains the types’ minimal and maximal values • Example: INT MAX for the maximal int value • The rest: Use the source, Luke! Herbert Martin Dietze <[email protected]> 29 Programming for MSc Part I Part 2: Scalar Data Types — What Scalar Types are Integer Format codes for ‘printf’ and ‘scanf’ For int types: %d int Signed integer %hd short Short signed integer %ld long Long signed integer %u unsigned Unsigned integer %hu unsigned short Short unsigned integer %lu unsigned long Long unsigned integer %o unsigned Octal, length like above %x unsigned Hexadecimal, lowercase, length like above %X unsigned Hexadecimal, Uppercase, length like above For char types: %c char interpreted as character %hhd signed char interpreted as signed number %hhu unsigned char interpreted as unsigned number • Especially for scanf() the correct format codes are crucial! • We can also add fill characters and field width • Example: printf ("%04d\n", 2) −→ “0002” • Many more flags −→ See Online Documentation! Herbert Martin Dietze <[email protected]> 30 Programming for MSc Part I Part 2: Scalar Data Types — What Scalar Types are Floating Point Types • There are two floating point types in C: float and double • Floating point types are always signed • The double type has double precision but uses more memory • The maximum and minimum values are found in float.h • Example: DBL MAX for the largest double value Comparing Floating Point Types • With floating point numbers we will have rounding errors −→ Exact comparisons are impossible! • No problems: “<”, “>”, “<=”, “>=” • May not work: “==” • Instead of equality use FLT EPSILON (for float) and DBL EPSILON (for double) from float.h • How? To see whether d1 and d2 are equal just compare their difference with DBL EPSILON Herbert Martin Dietze <[email protected]> 31 Programming for MSc Part I Part 2: Scalar Data Types — What Scalar Types are Floating Point Format codes for ‘printf’ and ‘scanf’ For printf(): %f double Floating point notation, this is used for both, float and double! %e double Exponential notation with lowercase ‘e’, this is used for both, float and double! %E double Exponential notation with uppercase ‘E’, this is used for both, float and double! • The output format can be modified again: – printf ("%5.1f\n", 3.14) −→ “ 3.1” – printf ("%07.3f\n", 3.14) −→ “003.140” – printf ("%e\n", 3.14) −→ “3.140000e+00” – printf ("%+e\n", 3.14) −→ “+3.140000e+00” • Many more flags −→ See Online Documentation! For scanf(): %f float Floating point or exponential notation, only for float! %lf double Floating point or exponential notation, only for double! %e float like %f %le double like %lf %E float like %f %lE double like %lf Herbert Martin Dietze <[email protected]> 32 Programming for MSc Part I Part 2: Scalar Data Types (b) Operations on Scalar Types Scalar Type Constants • A constant is an immediate value, like 3.14 or 3.1400e+00 • But what type does a given constant have? • The type can be specified using suffixes • Without suffix a standard type will be assumed Standard types for constants: 3.14 double 42 int ’c’ char Nonstandard types for constants: 3.14f or 3.14F float 42l or 42L long 42u or 42U unsigned 42lu or 42lU or ... unsigned long Numbering systems: 022 Octal, the corresponding decimal value here is 18! 0x2a or 0x2A Hexadecimal, the corresponding decimal value is 42! Herbert Martin Dietze <[email protected]> 33 Programming for MSc Part I Part 2: Scalar Data Types — Operations on Scalar Types Mixing Types • We can use an explicit typecast to set the type • An explicit typecast looks like this: (type)expression • Example: (int)3.14 will result in an int discarding anything after the dot: 3 • If we don’t the compiler will use the Usual Conversions • Typecasts give no protection against data loss! The Usual Arithmetic Conversions 1. If any operand is double the whole expression will be double 2. Else if any operand is float the whole expression will be float 3. All values of shorter types than int (signed and unsigned) will be converted to int if they fit, else to unsigned 4. Else the shortest signed-if-possible value that can hold all the values occuring will be chosen Rule of thumb1: Operands with different types get converted when you do arithmetic. Everything is converted to the type of the floatiest, longest operand, signed if possible without losing bits. 1See Peter van der Linden: Expert C Programming, deep C secrets Herbert Martin Dietze <[email protected]> 34 Programming for MSc Part I Part 2: Scalar Data Types — Operations on Scalar Types The Math Library • The math library provides advanced arithmetic functions • The prototypes can be found in the header math.h • On Unix systems it must be explicitly linked to the program: gcc -o foo foo.o -lm • On other systems it is part of the standard library Some Functions from the Math Library • Trigonometric functions: double sin(double x) • Inverse trigonometric: double asin(double x) • Exponentiation: double pow(double base, double pow) • Logarithm: double log (double x) • Many more: Read The Fine Manual Boolean Expressions • Boolean expressions are represented as int in C • If a logical expression is false we’ll get 0 • If a logical expression is true we’ll get 1 • However any other value but 0 is considered as true, too • Example: int equal = (a == b); • Or even: int foo = (a == b) + (c == d); Herbert Martin Dietze <[email protected]> 35.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us