C in a Nutshell A crash course in

  • Slides: 27
Download presentation
‘C’ in a Nutshell A “crash course” in C. . . with designs for

‘C’ in a Nutshell A “crash course” in C. . . with designs for embedded systems by J. S. Sumey Part I: intro, variables, constants, operators ver. Feb 2018

REFERENCE: The C Programming Language (2 nd ed. ) Brian W. Kernighan Dennis M.

REFERENCE: The C Programming Language (2 nd ed. ) Brian W. Kernighan Dennis M. Ritchie Prentice Hall Software Series 'C' in a Nutshell by J. Sumey 2 of 27

Low-Level (Assembly) Programming pros: n object code is smaller and runs faster w important

Low-Level (Assembly) Programming pros: n object code is smaller and runs faster w important in embedded systems! n programmer has total control over system hardware cons: n need to know processor and hardware intimately more tedious & time consuming not portable! 'C' in a Nutshell by J. Sumey 3 of 27

High-Level Programming pros: n n source code is highly portable more streamlined development, quicker

High-Level Programming pros: n n source code is highly portable more streamlined development, quicker w increased programmer productivity n n n support of structured design techniques more readable code, easier maintenance better math handling support cons: n increased overhead 'C' in a Nutshell by J. Sumey 4 of 27

“Mixed” Approach can use HLL like ‘C’ for bulk of project and use assembly

“Mixed” Approach can use HLL like ‘C’ for bulk of project and use assembly for select parts n n n time-sensitive functions interrupt handling special instructions, ex: fuzzy logic creates linkage issues n n calling assembly routines from C parameter passing & return results 'C' in a Nutshell by J. Sumey 5 of 27

C Background created in ‘ 70 s by Dennis Ritchie at Bell Labs a

C Background created in ‘ 70 s by Dennis Ritchie at Bell Labs a general-purpose “systems” programming language, multi-domain n applications compilers operating systems platform & architecture independent standardized in late ‘ 80 s by ANSI “ANSI C” is actually known as a mid-level language most commonly used language in industry 'C' in a Nutshell by J. Sumey 6 of 27

Overview 1 a ‘typed’ language n n fundamental: characters, integers, floating-point derived: pointers, arrays,

Overview 1 a ‘typed’ language n n fundamental: characters, integers, floating-point derived: pointers, arrays, structures, unions “basic” arithmetic & logical operations only typical control-flow constructs n n statement grouping decision selection looping 'C' in a Nutshell by J. Sumey 7 of 27

Overview 2 functions: n n may return anything (or nothing) no nesting may be

Overview 2 functions: n n may return anything (or nothing) no nesting may be recursive may exist in separate source files compiled individually or combined into a single file variable scope (declarations, actually): n n n local to a function local to a source file, global to all functions within global to entire program 'C' in a Nutshell by J. Sumey 8 of 27

Overview 3 uses a “preprocessor” during compilation n macro substitution include files conditional compilation

Overview 3 uses a “preprocessor” during compilation n macro substitution include files conditional compilation depends on libraries for everything else! n n n input / output file access composite object manipulation w i. e. , arrays, lists, strings n dynamic memory allocation 'C' in a Nutshell by J. Sumey 9 of 27

I. Data Types & Operations - representation of information & how to manipulate it

I. Data Types & Operations - representation of information & how to manipulate it

Constants - 1 integers n use suffix to override default int w ex: 99999

Constants - 1 integers n use suffix to override default int w ex: 99999 L – forces interpretation as long w ex: 32767 U – forces unsigned interpretation n prefixes to override default base (10) w ex: 037 = 0 x 1 f = 31 w ex: 0 XFUL = ? ? ? w some compilers also support binary constants: #define MASK 0 b 11110000 floats n contain ‘. ’ or ‘e’, default is double w ex: 1 e-1 L – forces long interpretation 'C' in a Nutshell by J. Sumey 11 of 27

Constants - 2 characters n a single character within single quotes w ex: ‘A’

Constants - 2 characters n a single character within single quotes w ex: ‘A’ = 0 x 41 = 65 n can represent certain control characters via “escape sequences” w ex: ‘n’, ‘b’, ‘f’, ‘g’, ‘r’, ‘t’, ‘\’ n can also represent characters in octal & hex w ex: #define LF ‘12’ w ex: #define CR ‘ x 0 d’ 'C' in a Nutshell by J. Sumey 12 of 27

Constants - 3 sting literals n n zero or more characters within double quotes

Constants - 3 sting literals n n zero or more characters within double quotes terminating null byte (‘’) is assumed w ex: “a 21 character string” (requires how many bytes? ) gotcha: n ‘t’ “t” 'C' in a Nutshell by J. Sumey 13 of 27

Variables represent named storage locations in memory must be declared before use n associates

Variables represent named storage locations in memory must be declared before use n associates a data type to the variable letters, numbers, & underscore n n n must start with letter or ‘_’ library routines typically start variables with ‘_’ convention: all UPPERCASE for symbolic constants; lower or mixed upper/lower for variables minimum 31 characters significant don’t use reserved words (if, else, int, etc. ) 'C' in a Nutshell by J. Sumey 14 of 27

Data Types Basic data types: n char – holds a single character (ASCII) w

Data Types Basic data types: n char – holds a single character (ASCII) w typically consumes 1 byte per char w has same characteristics as ints n int – integer only number w typically 16 or 32 bits, depends on architecture n float – ‘single precision’ floating point w typically 32 bits, depends on architecture n double – ‘double precision’ floating point w typically 64 bits, depends on architecture 'C' in a Nutshell by J. Sumey 15 of 27

Data Type ‘Qualifiers’ modify the basic properties of the data type n long &

Data Type ‘Qualifiers’ modify the basic properties of the data type n long & short – apply to integers to force them to more or less dynamic range w ex: short int loopctr; w ‘int’ may be omitted n signed & unsigned – applies to chars & ints w ex: unsigned char uc; range of ‘uc’ is 0. . 255 w ex: signed char sc; range of ‘sc’ is -128. . +127 n long double – extended-precision floating point standard headers define sizes for given system n <limits. h> & <float. h> 'C' in a Nutshell by J. Sumey 16 of 27

Sample program: sizes. c #include <stdio. h> #include <limits. h> #include <float. h> main()

Sample program: sizes. c #include <stdio. h> #include <limits. h> #include <float. h> main() { printf( "n--- SIZES OF BASIC DATA TYPES ON A COLDFIRE v 1 ---nn" ); printf( "number of bits in a char: %in", CHAR_BIT ); printf( "range of a unsigned char: %u. . %un", 0, UCHAR_MAX ); printf( "range of a signed char: %i. . %in", SCHAR_MIN, SCHAR_MAX ); printf( "range of a plain ol char: %i. . %in", CHAR_MIN, CHAR_MAX ); puts( "" ); printf( "number of bits in a short: %in", sizeof(short)*8); printf( " range of a short integer: %i. . %in", SHRT_MIN, SHRT_MAX ); printf( " an unsigned short: %u. . %un", 0, USHRT_MAX ); puts( "" ); printf( " number of bits in a int: %in", sizeof(int)*8 ); printf( " range of a plain integer: %i. . %in", INT_MIN, INT_MAX ); printf( " an unsigned int: %u. . %un", 0, UINT_MAX ); puts( "" ); printf( " number of bits in a long: %in", sizeof(long)*8 ); printf( " range of a long integer: %li. . %lin", LONG_MIN, LONG_MAX ); printf( " an unsigned long: %lu. . %lun", 0 L, ULONG_MAX ); puts( "" ); printf( " number of bits in a long: %in", sizeof(long)*8 ); printf( " range of a long int: %lli. . %llin", LLONG_MIN, LLONG_MAX ); printf( " an unsigned long: %llu. . %llun", 0 LL, ULLONG_MAX ); puts( "" ); printf( " number of digits in a float: %in", FLT_DIG ); printf( " range of a plain ol’ float: %E. . %En", FLT_MIN, FLT_MAX ); puts( "" ); printf( "number of digits in a double: %in", DBL_DIG ); printf( " range of a plain ol’ double: %. 15 E. . %. 15 En", DBL_MIN, DBL_MAX ); } 'C' in a Nutshell by J. Sumey 17 of 27

Sample run on a Cold. Fire v 1 MCU --- SIZES OF BASIC DATA

Sample run on a Cold. Fire v 1 MCU --- SIZES OF BASIC DATA TYPES ON A COLDFIRE v 1 --number of bits in a range of a unsigned range of a plain ol char: 8 0. . 255 -128. . 127 0. . 255 number of bits in a short: 16 range of a short integer: -32768. . 32767 an unsigned short: 0. . 65535 number of bits in a int: 32 range of a plain integer: -2147483648. . 2147483647 an unsigned int: 0. . 4294967295 number of bits in a long: 32 range of a long integer: -2147483648. . 2147483647 an unsigned long: 0. . 4294967295 number of bits in a long: 64 range of a long int: -9223372036854775808. . 9223372036854775807 an unsigned long: 0. . 18446744073709551615 number of digits in a float: 6 range of a plain ol’ float: 1. 175494 E-38. . 3. 402823 E+38 number of digits in a double: 15 range of a plain ol’ double: 2. 225073858507201 E-308. . 1. 797693134862316 E+308 'C' in a Nutshell by J. Sumey 18 of 27

Data Types for Embedded Systems very useful in embedded systems: n byte-sized (8 -bit)

Data Types for Embedded Systems very useful in embedded systems: n byte-sized (8 -bit) data w Byte, uchar, uint 8, byte: 0. . 255 w s. Byte, schar, sint 8: -128. . +127 n 16 -bit data w Word, uint 16, word: 0. . 65535 w s. Word, sint 16: -32768. . +32767 n 32 -bit data w LWord, ulong, uint 32, dword: 0. . 4294967295 w s. LWord, slong, sint 32: -2147483648. . 2147483647 n Boolean w bool: TRUE/FALSE n these are defined in stdtypes. h, derivative. h, etc. 'C' in a Nutshell by J. Sumey 19 of 27

“Extended” Data Types additional data types derived from or extending the basic types: n

“Extended” Data Types additional data types derived from or extending the basic types: n n n array pointer structure union function will save for part III 'C' in a Nutshell by J. Sumey 20 of 27

Variable ‘Storage’ Attributes define where variables are stored and how they may be used

Variable ‘Storage’ Attributes define where variables are stored and how they may be used / accessed n n n auto (default) – in a “stack frame” register – kept in a processor register if possible const – a variable that doesn’t change after initialization w should be stored in ROM n volatile – a variable that can change “on its own” w I/O registers, semaphores n extern – a variable defined outside the module it is referenced from 'C' in a Nutshell by J. Sumey 21 of 27

Declarations variables must be declared before use specifies a data type to each variable

Declarations variables must be declared before use specifies a data type to each variable n n ex: int first, last, inc; ex: short Circuit; may also include an initializer n ex: char esc = ‘ x 1 b’; the “const” qualifier declares a read-only variable (cannot be subsequently changed) n ex: const float pi = 3. 14159; 'C' in a Nutshell by J. Sumey 22 of 27

Operators - 1 arithmetic n +, -, *, /, % (modulus, ints only) equality

Operators - 1 arithmetic n +, -, *, /, % (modulus, ints only) equality n == (equal), != (not equal) relational n <, <=, =>, > logical – normally used in if statements n && (and), || (or), ! (not) 'C' in a Nutshell by J. Sumey 23 of 27

Operators - 2 increment / decrement n n ++, -be careful of prefix vs.

Operators - 2 increment / decrement n n ++, -be careful of prefix vs. postfix use! bitwise n n perform bit manipulation on char/integers only & (AND), | (OR), ^ (EOR) << (shift left), >> (shift right) ~ (1’s complement) these operators can be very useful for embedded programming! n ex… 'C' in a Nutshell by J. Sumey 24 of 27

Assignment Operators many binary operators have a corresponding “assignment operator” n n n e

Assignment Operators many binary operators have a corresponding “assignment operator” n n n e 1 op= e 2 is equivalent to e 1 = e 1 op e 2 ex: step += 2 this works for +, -, *, /, %, <<, >>, &, ^, | increases efficiency in embedded programming! (how? ) n n ex: porta |= 4; ex: portb &= ~4; 'C' in a Nutshell by J. Sumey 25 of 27

Conditional Expressions uses the ternary operator “? : ” and three expressions n n

Conditional Expressions uses the ternary operator “? : ” and three expressions n n expr 1 ? expr 2 : expr 3 means: expr 2 if expr 1 is true (non-0), else expr 3 ex: z = (a < b) ? a : b; is equivalent to: if (a < b) z = a; else z = b; i. e. , z = min(a, b) !! 'C' in a Nutshell by J. Sumey 26 of 27

Precedence PREC. hi determines order of expression evaluation; hence result! association determines binding of

Precedence PREC. hi determines order of expression evaluation; hence result! association determines binding of operators may always be overridden with parens n ex: if (porta & 0 x 80 == 0) bomb = 17 / 0; OOPS! what’s really wrong here? lo 'C' in a Nutshell by J. Sumey OPERATOR ASSOC. () [] ->. l-to-r ! ~ ++ -- + - (unary) * & (type) sizeof r-to-l */% l-to-r + - (binary) l-to-r << >> l-to-r < <= >= > l-to-r == != l-to-r & l-to-r ^ l-to-r | l-to-r && l-to-r || l-to-r ? : r-to-l = op= r-to-l , l-to-r 27 of 27