, Variables, and Constants
What is a Type The space in which a value is defined Space All possible allowed values All defined operations Integer Space whole numbers +, -, x No divide 2 tj
Why Types No room for confusion in a computer Must get the same answer every time All data must be stored into memory somewhere Program Memory Data Memory Memory used to be expensive Minimize the amount needed 3 tj
C Types Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 4 tj
C Types - void No values No defined operations Used when we want to indicate that nothing is here Examples MyFunction(void); // call a function with no input parameters void YourFunction(int val){ // indicate that a function returns nothing 5 tj
C Types - bool - boolean 2 values true, false Logical operations and (&&), or ( ), not (!) Examples bool in1, in2, in3; in1 && in2; // in1 AND in2 // declare 3 variables of type bool in3 =!(in1 in2); // in3 = in1 NOR in2 6 tj
C Types char - character ASCII - 128 values a,b,c,1,2,3,$,%,*, English language characters Unicode millions of values Stored in the computer as integers Same operations as integers Become characters when visualized Require a single quote Examples char initial1 = t ; char initial2 = j ; printf( %c%c, initial1, initial2); // print - tj printf( %c, (initial1 - initial2) ); // print (line feed) // 116 106 = 10 linefeed 7 tj
NULL no character numbers add 0x30 upper and lower case differ by hex 0x20 8 tj
C Types int - integer Values are system dependent integers only 1, 2, 4, 8 bytes short int, int, long int, long long int Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Bitwise operations ~,, &, ^, <<, >> Examples int aa, bb, cc; aa + bb; // add aa to bb // declare 3 variables of type int 9 tj
C Types int - integer Special considerations with type int Range is defined and limited SIGNED and UNSIGNED variants 14 15 0 1 2-2 -1 0 1 2 65534 65535 0 1 2-2 -1 0 1 2-7 -8 7 6-32767 -32768 32766 32767 8 bit unsigned 8 bit signed 16 bit unsigned 16 bit signed 10 tj
C Types int - integer Consider 3 16bit variables of type short int aa = 32,500 bb = 300 cc = 15,000 signed unsigned aa + bb = -32,736 32,800 aa/cc = 2 11 tj
C Types special integers A special set of integers are defined for embedded systems Designed to allow register/memory access Not system dependent #include <stdint.h> signed char unsigned char short unsigned short int unsigned long long unsigned long long int8_t; uint8_t; int16_t; uint16_t; int32_t; uint32_t; int64_t; uint64_t; 12 tj
C Types int - integer TI MSP432 Laptop 13 tj
C Types float real Values are system dependent SIGNED 4, 8 bytes 1,8,23-1,11,52 float, double, long double Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Examples float aa, bb, cc; aa / bb; // aa divided by bb // declare 3 variables of type float 14 tj
C Types float imaginary Values are system dependent SIGNED 4, 8 bytes 1,8,23-1,11,52 float imaginary, double imaginary, long double imaginary Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Not supported in many systems 15 tj
C Types float complex Values are system dependent SIGNED 4, 8 bytes 1,8,23-1,11,52 float complex, double complex, long double complex Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Real and imaginary parts must be the same size 16 tj
C Types float - real Consider 3 variables of type float aa = 2.5 bb = 300.5 cc = 0.035 signed aa + bb = 303.0 aa/cc = 71.428571 17 tj
Special Details Functions: sizeof(type) sizeof expression typeof(expression) Include <limits.h> <float.h> <stdint.h> SHRT_MIN SHRT_MAX USHRT_MAX INT_MIN INT_MAX Minimum value for an object of type short int Maximum value for an object of type short int Maximum value for an object of type unsigned short int Minimum value for an object of type int Maximum value for an object of type int defines max and min values for standard types 18 tj
Variables Variable Symbolic representation for a value - name Stored in memory (data) Can be modified during execution Since it requires space in memory it must have a type to tell the compiler how much space to reserve 19 tj
Variables Variable Declaration Specify the type and name for a variable Must be declared before it can be used int foo; float rate; char initial1; int var1, this, is, not, a, good, practice; int AccountBalance; int annual_interest_rate; ** Note: name length has no impact on compiled program size focus on readable code 20 tj
Variables Variable Initialization Variables are not initialized just by declaring them They do not automatically have a value of 0 They may well have garbage values Nothing stops you from using an un-initialized variable int foo = 23; int foo, boo = 23; int count; int foo = 23, boo = 23; count = 0; char = A ; float pie = 3.14259; 21 tj
Variables Variables and Memory int foo; char initial1 = t ; float rate = 2.5; char initial2; int boo = 255; reserved for foo has garbage in it initial 1 t hex 0x74 initial 2 - garbage unused - alignment 2.5 10.1 binary 1.01 x 2 1 0 sign 01 mantissa 1000000 exponent boo x x x x x x x x x x x 0 1 1 1 0 1 0 0 x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x Memory Address 0x0001 1000 foo references this memory address 0x0001 1004 intial1 references this memory address 0x0001 1008 0x0001 100C 0x0001 1010 22 tj
Constants Constant Symbolic representation for a value - name Stored in memory (program) Cannot be modified during execution Since it requires space in memory it must have a type to tell the compiler how much space to reserve 23 tj
Constants Constant Types Same as variable types + string Boolean true, false must include stdbool.h Character requires single quotes Integer can use a modifier to declare type 123 int 123L long int 123UL unsigned long int 24 tj
Constants Constant Types Real - Floating Point default is double can use a modifier to declare type 12.3 double 12.3F float 12.3L long double Complex - Floating Point default is double can use a modifier to declare type 12.3 + 45.6*I double complex 12.3F + 45.6F*I float complex 12.3L + 45.6L*I long double complex 25 tj
Constants Constant Types String Series of characters enclosed in double quotes this is a string constant Special considerations empty string \0 null character 26 tj
Constants Defining Constants Literal un-named constant a = b + 5; // 5 is a literal constant Defined Pre-processor constant #define INTEREST_RATE 0.01 Note ALL CAPS Memory Similar to a variable but cannot be changed const type identifier = value; const float interestrate = 0.01; Why bother with types? 27 tj