Download presentation
Presentation is loading. Please wait.
1
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 )
2
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
3
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.
4
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
5
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 to 127 short int to 32767 long int to
6
real variable sizes and ranges
type #bytes exponent precision float ±38 7 digits double ± digits long double 10 ± digits
7
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)
8
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,...
9
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
10
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));
11
Program 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; }
12
Program 2.1 - Integrated testing
Let’s see what the default values are on the our compiler from the Pelles IDE...on my laptop
13
constants symbolic explicit implicit
#define PI (pre-processor statement) explicit const float pi = ; implicit area = *radius*radius;
14
explicit constants add “const” before type
attempting to change an explicit constant is illegal.
15
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
16
Implicit constant examples
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.)
17
Character data - ASCII table
Letters and digits are sequential in ASCII: ‘0’ is 0x30 ‘9’ is 0x39 ‘a’ is 0x61 ‘A’ is 0x41 (hexadecimal)
18
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
19
ISDIGIT function /* true if character is 0 - 9; false otherwise */
int ISDIGIT(char c) { return ((c>=0x30)&&(c<0x3A)); }
20
ATOI function /* converts numeric string to an integer */
int ATOI (char s[]) { int i, j=0, ISDIGIT(char); for (i=0;ISDIGIT(s[i]);i++) j=10*j+(s[i]-'0'); return j; }
21
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; }
22
Program 2.2 - Integrated testing
Let’s test the program now in the Pelles IDE...
23
Variable storage classes
global automatic register static
24
Scope (privacy), duration, and linkage
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).
25
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
26
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”.
27
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.
28
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.
29
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.
30
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
31
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.
32
Program 2.3 - concept demonstration
#include <stdio.h> /* demonstration of storage classes */ int i=10, j=5, k; // global variables int main(int argc, 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); return 0; }
33
Program 2.3 - continuation
int func (register int i) { 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.