0% found this document useful (0 votes)
20 views34 pages

ENEB340 Week04 Data Types

IOT Slides

Uploaded by

nsasikum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views34 pages

ENEB340 Week04 Data Types

IOT Slides

Uploaded by

nsasikum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

ENEB 340 Week 4 - 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
There are also 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 -32,768 to 32,767
long int 4 -2,147,483,648 to 2,147,483,647
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
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 4.1 - Size of Data Types
/* #2.1 determine size of data types on my laptop
written by W. Lawson
last modified 27 Sept 2019 */
#include <stdio.h>
int main(void)
{
char name1[5], prompt[]="Enter the second name:";
int test=20;
printf("Size of character:\t%lu\n",sizeof(char));
printf("Size of short int:\t%lu\n",sizeof(short int));
printf("Size of integer:\t%lu\n",sizeof(int));
Program 4.1 - part 2
printf("Size of long int:\t%lu\n",sizeof(long int));
printf("Size of real:\t\t%lu\n",sizeof(float));
printf("Size of double:\t\t%lu\n",sizeof(double));
printf("Size of long double:\t%lu\n",sizeof(long double));
printf("Size of name1:\t\t%zlu\n",sizeof(name1));
printf("Size of prompt:\t\t%lu\n",sizeof prompt);
printf("Size of test:\t\t%lu\n",sizeof test);
return 0;
}
Program 2.1 - Integrated testing

char name1[5], prompt[]="Enter the second name:";


int test=20;
constants
ØSymbolic (pre-processor statement) :
#define PI 3.141592
Ø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
Implicit constant examples
42 integer 42. double
42U unsigned int 42.0 double
42L long int 42.0f float
42UL unsigned long int 4.2e1 double
42000signed long int 0.42e2l long double
062 int (octal) 42000.e-3L long double
0x2A int (hexadecimal) (upper or lower case l, f,
and u are o.k.)
Character data - ASCII table

Ø Letters and digits are sequential in ASCII: https://www.bibase.com/ascii.htm


‘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<0x39));
}
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;
}
Program 4.2 - convert string to integer
/* Read a string and convert it to a character
written by W. Lawson
last modified 27 Sept 2019 */
#include <stdio.h>
int main (void)
{
int ATOI(char[]);
char test[BUFSIZ];
puts("enter a number");
fgets(test, sizeof(test), stdin);
printf("\n%s = %d\n",test,ATOI(test));
return 0;
}
Program 4.2 - Integrated testing
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.
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 4.3 - concept demonstration
#include <stdio.h>
/* demonstration of storage classes
written by W. Lawson
last modified 27 Sept 2019 */
int i=10, j=5, k; // global variables
int main(int argc, char *argv[])
{
int l=1, 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 4.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;
}
Program 4.3 - Integrated testing
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);
printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l);
return 0;
}
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;
}

You might also like