Lecture 3: Datatypes - Programmeringsteknik DT143G PDF

Summary

This PDF document is a lecture on data types in C programming. The document covers integers, real numbers, type conversions, and the mathematical library. It was presented on November 11, 2024, to a programmeringsteknik DT143G class likely at Örebro University.

Full Transcript

Introduction Programmeringsteknik DT143G Lecture 3: Datatypes Pascal Rebreyend 11-11-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Datatypes Manipulating operations...

Introduction Programmeringsteknik DT143G Lecture 3: Datatypes Pascal Rebreyend 11-11-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Datatypes Manipulating operations Pascal Rebreyend Programmeringsteknik DT143G Introduction Integers The first datatype we have seen Used to save integer Its size depend on the compiler and architecture (at least in C 2 bytes, i.e. from -32,768 to 32,767) (in general, 4 bytes - 32 bits!) Unsigned integer exists too What is happening is we are out of the range? Pascal Rebreyend Programmeringsteknik DT143G Introduction Other integer types short int: at least 2 bytes long int: 4 bytes at least long long int: 8 bytes at least Both signed (default) and unsigned (uint,... ) char: 1 byte (-128 to 127), used to store a character (ASCII) Sizes may differ.. (compiler and architecture dependant) A byte is 8 bit Pascal Rebreyend Programmeringsteknik DT143G Introduction Real numbers We may need to use real numbers float: 4 bytes double: 8 bytes long double: at least 80 bits Pascal Rebreyend Programmeringsteknik DT143G Introduction Real numbers: float Float: 32 bits IEEE 754 floating point number 1-bit for the sign, 8-bits for exponent, 23-bits for the value (mantissa) Advantage: the absolute precision depend on the absolute value of the number (Or the relative precision is almost constant) Pascal Rebreyend Programmeringsteknik DT143G Introduction printf format %f: float %lf: double %.2f: 2 decimals %5.3f: 5 digits for the integer parts, 3 for decimals %e: scientific notation... Pascal Rebreyend Programmeringsteknik DT143G Introduction Mixing datatype What’s happend if we do a + b, with a and b having different datatype? And when c = a + b? a + 1 is not as a + 1.0 !! Pascal Rebreyend Programmeringsteknik DT143G Introduction Conversion Conversion from a small to a bigger datatype: No problem The opposite is NOT true! We can always force (cast) to a specific type float to int: we loose the decimal part! functions: round,floor,... Pascal Rebreyend Programmeringsteknik DT143G Introduction limits.h A file we may includes contains the limits of datatypes: INT MAX INT MIN ULONG MAX... Pascal Rebreyend Programmeringsteknik DT143G Introduction Modifiers const: the value of the variable cannot be changed Useful in some circumstances static: (later, when we will use functions) Pascal Rebreyend Programmeringsteknik DT143G Introduction The mathematical library #include #include int main() { double x, y; printf("Enter x\n"); scanf("%lf", &x); y = sqrt(x); printf("y = %lf\n", y); return 0; } You need the option -lm if you compile with gcc Pascal Rebreyend Programmeringsteknik DT143G Introduction Conclusion Important to know the datatype of a variable C: specifies a lower limit of size but size may vary (i.e be bigger) What is happening when we mix datatype? Take care of maximum/minimum values which can be stored Pascal Rebreyend Programmeringsteknik DT143G

Use Quizgecko on...
Browser
Browser