Download presentation
Presentation is loading. Please wait.
Published byJacob Washington Modified over 9 years ago
1
‘C’ in a Nutshell A “crash course” in C......with designs for embedded systems by J. S. Sumey Part I: intro, variables, constants, operators
2
'C' in a Nutshell by J. Sumey2 of 27 REFERENCE: The C Programming Language (2 nd ed.) Brian W. Kernighan Dennis M. Ritchie Prentice Hall Software Series
3
'C' in a Nutshell by J. Sumey3 of 27 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!
4
'C' in a Nutshell by J. Sumey4 of 27 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
5
'C' in a Nutshell by J. Sumey5 of 27 “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
6
'C' in a Nutshell by J. Sumey6 of 27 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
7
'C' in a Nutshell by J. Sumey7 of 27 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
8
'C' in a Nutshell by J. Sumey8 of 27 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
9
'C' in a Nutshell by J. Sumey9 of 27 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
10
I. Data Types & Operations -representation of information & how to manipulate it
11
'C' in a Nutshell by J. Sumey11 of 27 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 = ??? some compilers also support binary constants: #define MASK 0b11110000 floats contain ‘.’ or ‘e’, default is double ex: 1e-1L – forces long interpretation
12
'C' in a Nutshell by J. Sumey12 of 27 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’
13
'C' in a Nutshell by J. Sumey13 of 27 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”
14
'C' in a Nutshell by J. Sumey14 of 27 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.)
15
'C' in a Nutshell by J. Sumey15 of 27 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
16
'C' in a Nutshell by J. Sumey16 of 27 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 &
17
'C' in a Nutshell by J. Sumey17 of 27 Sample program: sizes.c #include 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 long int: %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 ); }
18
'C' in a Nutshell by J. Sumey18 of 27 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
19
'C' in a Nutshell by J. Sumey19 of 27 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.
20
'C' in a Nutshell by J. Sumey20 of 27 “Extended” Data Types additional data types derived from or extending the basic types: array pointer structure union function will save for part III
21
'C' in a Nutshell by J. Sumey21 of 27 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
22
'C' in a Nutshell by J. Sumey22 of 27 Declarations variables must be declared before use specifies a data type to each variable ex: int first, last, inc; ex: short Circuit; may also include an initializer ex: char esc = ‘\0x1b’; the “const” qualifier declares a read-only variable (cannot be subsequently changed) ex: const float pi = 3.14159;
23
'C' in a Nutshell by J. Sumey23 of 27 Operators - 1 arithmetic +, -, *, /, % (modulus, ints only) equality == (equal), != (not equal) relational, > logical – normally used in if statements && (and), || (or), ! (not)
24
'C' in a Nutshell by J. Sumey24 of 27 Operators - 2 increment / decrement ++, -- be careful of prefix vs. postfix use! bitwise perform bit manipulation on char/integers only & (AND), | (OR), ^ (EOR) > (shift right) ~ (1’s complement) these operators can be very useful for embedded programming! ex…
25
'C' in a Nutshell by J. Sumey25 of 27 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;
26
'C' in a Nutshell by J. Sumey26 of 27 Conditional Expressions uses the ternary operator “?:” and three expressions expr1 ? expr2 : expr3 means: 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) !
27
'C' in a Nutshell by J. Sumey27 of 27 Precedence 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? PREC.OPERATORASSOC. hi() [] ->.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 ?:r-to-l = op=r-to-l lo,l-to-r
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.