340 likes | 580 Views
Chapter 2 - Data Types and Storage Classes. We will now take a closer look at the details of C’s data types, learn about the various types of constants, and learn about the scope and privacy of the different classes of data storage.
E N D
Chapter 2 - Data Types and Storage Classes • We will now take a closer look at the details of C’s data types, learn about the various types of constants, and learn about the scope and privacy of the different classes of data storage. • C has four fundamental data types... (you have already worked with three )
Fundamental data types Type Usage int Integral numbers char Text and control characters; small integers float Low/medium precision real numbers double High precision real numbers
Modified data types Type Usage short int small - medium integers long int large integers long double very large reals integers can also be unsigned, which means that there are no negative values and the most significant bit is used to double the positive range.
Other data type expressions You will often see *_t data types. For example, look in <stdint.h> • Generally used to show intent: • uint8_t Unsigned short integer (8bits) • size_t Unsigned integer (at least 16 bits) • time_t Arithmetic type for storing times
Data type sizes and ranges • The storage size (and hence ranges) of the various character types are often machine dependent. Typical values for 32-bit words: type #bytes range char 1 -128 to 127 short int 2 -32768 to 32767 long int 4 -2147483648 to 2147483647
real variable sizes and ranges type #bytes exponent precision float 4 ±38 7 digits double 8 ±308 15 digits long double 10 ±4932 19 digits
sizeof operator • sizeof is an operator, like +, -, *, AND / it is NOT a function • sizeof can be used to find the (sometimes device-specific) lengths of data types, variables, and arrays. • with data types use sizeof(...) • with variables the parentheses are optional • sizeof has many applications, particularly dynamic memory allocation (ENEE 150)
printf format indicators %d char, short, int decimal %x char, short, int hexadecimal %o char, short, int octal %u char, short, int unsigned decimal %ld, %lx... long integer decimal, hex, ... %f float, double floating point %e float, double scientific notation %g float, double shortest of %f & %e %Lf, Le... long double floating, scientific,...
scanf format indicators integer %d %i %u %x %o short int %hd %hi %hu %hx %ho long integer %ld %li %lu %lx %lo for decimal, decimal, unsigned dec, hex, octal float %f %e %g double %lf %le %lg long double %Lf %Le %Lg for floating point, scientific notation, or shortest of %f & %e
Program 2.1 - Size of Data Types /* #2.1 determine size of data types in Pelles on my laptop*/ #include <stdio.h> int main(void) { char name1[5], prompt[]="Enter the second name:"; int test=20; printf("Size of character:\t%zu\n",sizeof(char)); printf("Size of short int:\t%zu\n",sizeof(short int)); printf("Size of integer:\t%zu\n",sizeof(int));
Program 2.1 - part 2 printf("Size of long int:\t%zu\n",sizeof(long int)); printf("Size of real:\t\t%zu\n",sizeof(float)); printf("Size of double:\t\t%zu\n",sizeof(double)); printf("Size of long double:\t%zu\n",sizeof(long double)); printf("Size of name1:\t\t%zu\n",sizeof(name1)); printf("Size of prompt:\t\t%u\n",sizeof prompt); printf("Size of test:\t\t%d\n",sizeof test); return 0; }
Program 2.1 - Integrated testing • Let’s see what the default values are on the our compiler from the Pelles IDE...on my laptop
constants • symbolic • #define PI 3.141592 (pre-processor statement) • explicit • const float pi = 3.141592; • implicit • area = 3.141592654*radius*radius;
explicit constants • add “const” before type • attempting to change an explicit constant is illegal.
implicit constants • defaults • decimal integers (no decimal point) • double real (decimal point) • integers that are too large are converted to long int (or unsigned long int) • reals that are too large are NOT converted to long double - an overflow occurs • identifiers are used to override defaults
42 integer 42U unsigned int 42L long int 42UL unsigned long int 42000 signed long int 062 int (octal) 0x2A int (hexadecimal) 42. double 42.0 double 42.0f float 4.2e1 double 0.42e2l long double 42000.e-3L long double (upper or lower case l, f, and u are o.k.) Implicit constant examples
Character data - ASCII table • Letters and digits are sequential • in ASCII: • ‘0’ is 0x30 • ‘9’ is 0x39 • ‘a’ is 0x61 • ‘A’ is 0x41 (hexadecimal)
Character functions • We can use these facts to make simple versions of character functions which are defined in <ctype.h> and <stdlib.h> • isdigit checks to see if character is 0 - 9 • atoi converts a string to a number
ISDIGIT function /* true if character is 0 - 9; false otherwise */ int ISDIGIT(char c) { return ((c>=0x30)&&(c<0x3A)); }
ATOI function /* converts numeric string to an integer */ int ATOI (char s[]) { inti, j=0, ISDIGIT(char); for (i=0;ISDIGIT(s[i]);i++) j=10*j+(s[i]-'0'); return j; }
main - convert string to integer /* read a string and convert it to a character */ #include <stdio.h> int main (void) { int ATOI(char[]); char test[BUFSIZ]; puts("enter a number"); gets(test); printf("\n%s = %d\n",test,ATOI(test)); return 0; }
Program 2.2 - Integrated testing • Let’s test the program now in the Pelles IDE...
Variable storage classes • global • automatic • register • static
Scope (privacy), duration, and linkage • scope: • region of program in which a variable is known • storage duration: • the time period when a variable exists in memory • linkage: • in multiple-source-file programs (more than one *.c file), indicates the connection of variables in one file to other files. (we won’t discuss this attribute further in this class).
Global variables • a global variable is defined outside (or between) functions. • The scope of a global variable extends from the point of definition up to the end of the file (but not before the definition). • global variables can be initialized only by a constant (not an expression). • global variables will be set to zero if not explicitly initialized. • initialization of all globals occurs before main starts. • globals exist for the life of the program
Automatic variables (auto) • an automatic variable is defined inside a function or a statement block {}. • they must be defined immediately after the opening {. • The scope of an automatic variable extends from the point of definition up to the end of the function or block. No code outside this block can access the variable. • automatic variables can be initialized with an expression. • automatic variables will contain garbage if not explicitly initialized (they are NOT set to zero). • initialization occurs at “run time”.
Automatic variables - continued • duration: automatic variables are created when the execution of their statement block begins and the variables cease to exist when the block execution concludes. This means that the next time the block is run, the value from the previous execution is GONE! • If a global variable and an automatic variable have the same name, the global variable is “hidden” from the program for the duration of the automatic variable’s existence. The global variable becomes accessible to the program as soon as the automatic variable is gone and retains its original value.
Global versus automatic • whenever practical, automatic variables should be used because: • minimizes storage and program size. • reduces the chance of accidently modifying a variable (when inadvertently used for two different purposes). • improves the portability and reusability of the code to have functions as self-contained units.
Register variables • variables are normally stored in memory. • The time it takes to acquire data from memory is usually much longer than the time it takes to perform an operation in the CPU/ALU. • C allows you to request that frequently used variables be stored in CPU registers rather than memory.
Register variables - continued • place register in front of the variable type to request a register variable. • There are a limited number of registers, so the compiler may choose to ignore request, in which case the variable is stored in memory. • global variables can not be register variables. • arrays can not be register variables. • function arguments can be register variables • same initialization rules as auto variables
Static variables • keyword static precedes data type. • has the scope of an auto variable. • duration is extended beyond the execution of the statement block so that static variables retain their values between function calls. • can be initialized only with a constant. • Set to zero if not explicitly initialized.
Program 2.3 - concept demonstration • #include <stdio.h> • /* demonstration of storage classes */ • inti=10, j=5, k; // global variables • intmain(intargc, char *argv[]) • { • int l, func(int); • printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l); • func(i); • l=func(j); • printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l); • return 0; • }
Program 2.3 - continuation intfunc (register inti) { auto int j=i+4, l; static int k; printf("func: i=%d\t j=%d\t k=%d\t l=%d\n",i++,j++,k++,l++); return k; }