300 likes | 492 Views
‘C’ in a Nutshell. A “crash course” in C... ...with designs for embedded systems by J. S. Sumey Part I: intro, variables, constants, operators. REFERENCE: The C Programming Language (2 nd ed.) Brian W. Kernighan Dennis M. Ritchie Prentice Hall Software Series.
E N D
‘C’ in a Nutshell A “crash course” in C... ...with designs for embedded systems by J. S. Sumey Part I: intro, variables, constants, operators
REFERENCE:The C Programming Language (2nd ed.)Brian W. KernighanDennis M. RitchiePrentice Hall Software Series 'C' in a Nutshell by J. Sumey
Low-Level (Assembly) Programming • pros: • object code is smaller and runs faster • important in embedded systems! • programmer has total control over system hardware • cons: • need to know processor and hardware intimately • more tedious & time consuming • not portable! 'C' in a Nutshell by J. Sumey
High-Level Programming • pros: • source code is highly portable • more streamlined development, quicker • increased programmer productivity • support of structured design techniques • more readable code, easier maintenance • better math handling support • cons: • increased overhead 'C' in a Nutshell by J. Sumey
“Mixed” Approach • can use HLL like ‘C’ for bulk of project and use assembly for select parts • time-sensitive functions • interrupt handling • special instructions, ex: fuzzy logic • creates linkage issues • calling assembly routines from C • parameter passing & return results 'C' in a Nutshell by J. Sumey
C Background • created in ‘70s by Dennis Ritchie at Bell Labs • a general-purpose “systems” programming language, multi-domain • applications • compilers • operating systems • platform & architecture independent • standardized in late ‘80s 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
Overview 1 • a ‘typed’ language • fundamental: characters, integers, floating-point • derived: pointers, arrays, structures, unions • “basic” arithmetic & logical operations only • typical control-flow constructs • statement grouping • decision • selection • looping 'C' in a Nutshell by J. Sumey
Overview 2 • functions: • 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): • 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
Overview 3 • uses a “preprocessor” during compilation • macro substitution • include files • conditional compilation • depends on libraries for everything else! • input / output • file access • composite object manipulation • i.e., arrays, lists, strings • dynamic memory allocation 'C' in a Nutshell by J. Sumey
I. Data Types & Operations - representation of information & how to manipulate it
Constants - 1 • integers • use suffix to override default int • ex: 999999999L – forces interpretation as long • ex: 32767U – forces unsigned interpretation • prefixes to override default base (10) • ex: 037 = 0x1f = 31 • ex: 0XFUL = ??? • floats • contain ‘.’ or ‘e’, default is double • ex: 1e-1L – forces long interpretation 'C' in a Nutshell by J. Sumey
Constants - 2 • characters • a single character within single quotes • ex: ‘A’ = 0x41 = 65 • can represent certain control characters via “escape sequences” • ex: ‘\n’, ‘\b’, ‘\f’, ‘\g’, ‘\r’, ‘\t’, ‘\\’ • can also represent characters in octal & hex • ex: #define LF ‘\012’ • ex: #define CR ‘\0x0d’ 'C' in a Nutshell by J. Sumey
Constants - 3 • sting literals • zero or more characters within double quotes • terminating null byte (‘\0’) is assumed • ex: “a 21 character string” • gotcha: • ‘t’ “t” 'C' in a Nutshell by J. Sumey
Variables • represent named storage locations in memory • must be declared before use • associates a data type to the variable • letters, numbers, & underscore • 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
Data Types • Basic data types: • char – holds a single character (ASCII) • typically consumes 1 byte per char • has same characteristics as ints • int – integer only number • typically 16 or 32 bits, depends on architecture • float – ‘single precision’ floating point • typically 32 bits, depends on architecture • double – ‘double precision’ floating point • typically 64 bits, depends on architecture 'C' in a Nutshell by J. Sumey
Data Type ‘Qualifiers’ • modify the basic properties of the data type • long & short – apply to integers to force them to more or less dynamic range • ex: short int loopctr; • ‘int’may be omitted • signed & unsigned – applies to chars & ints • ex: unsigned char uc; range of ‘uc’ is 0..255 • ex: signed char sc; range of ‘sc’ is -128..+127 • long double – extended-precision floating point • standard headers define sizes for given system • <limits.h> & <float.h> 'C' in a Nutshell by J. Sumey
Sample program: sizes.c #include <stdio.h> #include <limits.h> #include <float.h> main() { printf( "\n--- SIZES OF BASIC DATA TYPES ON A COLDFIRE v1 ---\n\n" ); printf( "number of bits in a char: %i\n", CHAR_BIT ); printf( "range of a unsigned char: %u..%u\n", 0, UCHAR_MAX ); printf( "range of a signed char: %i..%i\n", SCHAR_MIN, SCHAR_MAX ); printf( "range of a plane ol char: %i..%i\n", CHAR_MIN, CHAR_MAX ); puts( "" ); printf( "number of bits in a short: %i\n", sizeof(short)*8); printf( " range of a short integer: %i..%i\n", SHRT_MIN, SHRT_MAX ); printf( " an unsigned short: %u..%u\n", 0, USHRT_MAX ); puts( "" ); printf( " number of bits in a int: %i\n", sizeof(int)*8 ); printf( " range of a plane integer: %i..%i\n", INT_MIN, INT_MAX ); printf( " an unsigned int: %u..%u\n", 0, UINT_MAX ); puts( "" ); printf( " number of bits in a long: %i\n", sizeof(long)*8 ); printf( " range of a long integer: %li..%li\n", LONG_MIN, LONG_MAX ); printf( " an unsigned long: %lu..%lu\n", 0L, ULONG_MAX ); puts( "" ); printf( " number of bits in a long long: %i\n", sizeof(long long)*8 ); printf( " range of a long longint: %lli..%lli\n", LLONG_MIN, LLONG_MAX ); printf( " an unsigned long long: %llu..%llu\n", 0LL, ULLONG_MAX ); puts( "" ); printf( " number of digits in a float: %i\n", FLT_DIG ); printf( " range of a plane ol float: %E..%E\n", FLT_MIN, FLT_MAX ); puts( "" ); printf( "number of digits in a double: %i\n", DBL_DIG ); printf( " range of a plane ol double: %.15E..%.15E\n", DBL_MIN, DBL_MAX ); } 'C' in a Nutshell by J. Sumey
Sample run on a ColdFire v1 MCU --- SIZES OF BASIC DATA TYPES ON A COLDFIRE v1 --- number of bits in a char: 8 range of a unsigned char: 0..255 range of a signed char: -128..127 range of a plane ol char: 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 plane 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 long: 64 range of a long long int: -9223372036854775808..9223372036854775807 an unsigned long long: 0..18446744073709551615 number of digits in a float: 6 range of a plane ol float: 1.175494E-38..3.402823E+38 number of digits in a double: 15 range of a plane ol double: 2.225073858507201E-308..1.797693134862316E+308 'C' in a Nutshell by J. Sumey
Data Types for Embedded Systems • very useful in embedded systems: • byte-sized (8-bit) data • Byte, uchar, uint8, byte: 0..255 • sByte, schar, sint8: -128..+127 • 16-bit data • Word, uint, uint16, word: 0..65535 • sWord, sint, sint16: -32768..+32767 • 32-bit data • LWord, ulong, uint32, dword: 0..4294967295 • sLWord, slong, sint32: -2147483648..2147483647 • Boolean • bool: TRUE/FALSE • these are defined in stdtypes.h, derivative.h, etc. 'C' in a Nutshell by J. Sumey
“Extended” Data Types • additional data types derived from or extending the basic types: • array • pointer • structure • union • function • will save for part III 'C' in a Nutshell by J. Sumey
Variable ‘Storage’ Attributes • define where variables are stored and how they may be used / accessed • auto (default) – in a “stack frame” • register – kept in a processor register if possible • const – a variable that doesn’t change after initialization • should be stored in ROM • volatile – a variable that can change “on its own” • I/O registers, semaphores • extern – a variable defined outside the module it is referenced from 'C' in a Nutshell by J. Sumey
Declarations • variables must be declared before use • specifies a data type to each variable • ex: int first, last, inc; • may also include an initializer • ex: char esc = ‘\0x1b’; • the “const” qualifier declares a read-only variable • ex: const float pi = 3.14159; 'C' in a Nutshell by J. Sumey
Operators - 1 • arithmetic • +, -, *, /, % (modulus, ints only) • equality • == (equal), != (not equal) • relational • <, <=, =>, > • logical – normally used in if statements • && (and), || (or), ! (not) 'C' in a Nutshell by J. Sumey
Operators - 2 • increment / decrement • ++, -- • be careful of prefix vs. postfix use! • bitwise • 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! • ex… 'C' in a Nutshell by J. Sumey
Assignment Operators • many binary operators have a corresponding “assignment operator” • e1 op= e2 is equivalent to e1 = e1 op e2 • ex: step += 2 • this works for +, -, *, /, %, <<, >>, &, ^, | • increases efficiency in embedded programming! (how?) • ex: porta |= 4; • ex: portb &= ~4; 'C' in a Nutshell by J. Sumey
Conditional Expressions • uses the ternary operator “?:” and three expressions • expr1 ? expr2 : expr3means: expr2 if expr1 is true (non-0), else expr3 • 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
determines order of expression evaluation; hence result! association determines binding of operators may always be overridden with parens ex: if (porta & 0x80 == 0) bomb = 17 / 0;OOPS!what’s really wrong here? Precedence 'C' in a Nutshell by J. Sumey