Review Bahasa C C Compilation Stage source code

Review Bahasa C

C Compilation Stage source code (c ) IDE Compiler object code (obj) Libraries(lib) Linker executable code (bin) C Compilation Model

GCC Compilation Stage

C Compilation Stage Integrated Development Environment (IDE) : Editor + Compiler + Linker + Debugger + Downloader • Preprocessor : removing comments & interpreting special preprocessor directives denoted by #. #include -- includes contents of a named file. Files usually called header files. e. g • #include <math. h> -- standard library maths file. • #include <stdio. h> -- standard library I/O file #define -- defines a symbolic name or constant. Macro substitution. • #define MAX_ARRAY_SIZE 100 • C compiler : translate source code to assembly code • Assembler : generate object code • Linker : combine library files and link functions from other source files

Example of C codes #include <stdio. h> main() { printf("Hello, world!n"); return 0; } #include <stdio. h> /* print a few numbers, to illustrate a simple loop */ main() { int i; for(i = 0; i < 10; i = i + 1) { printf("i is %dn", i); } return 0; } The C Programming Language, by Brian Kernighan and Dennis Ritchie, 1995 2 nd Edition published in 1988 by Prentice-Hall, ISBN 0 -13 -110362 -8

C/C++ Keywords ( _ are Microsoft-specific )

Keil show C/C++ Keywords in blue

Basic Data Types & Operators • Types • char, int, long int, float, long float • Constant • Declarations (for variables) char c; int i; float f; int i 1, i 2; int j 1=10, j 2= 20; ► Operators + : addition - : subtraction * : multiplication / : division % : modulus (remainder) ~ : not & : and | : or ^ : exclusive or >> shift to right << shift to left

ANSI C/C++ Standard Data Types and Sizes

Operators • Bitwise operators &, (and), | (or), ^ (xor), ~ (one’s complement) • Shift operators << (left shift), >> (right shift) • Increment/Decrement ++, -- • Arithmetic operators +, -, *, /, % (modulus) • Assignment operators = • Compound assignment +=, -=, *=, /= • Relational operators ==, !=, >, <, <= • Logical operators && (and), || (or), ! (not)

Operator Precedence Levels From Highest To Lowest

Standard C and C++ Libraries #include <stdio. h> #include <string. h> #include <math. h> #include <iostream. h> #include <memory. h>

Program Control : if, if-else • If ► if ( input >4095) input = 0; If -Else if ( input >4095) input = 0; else input = input – 2048; if (keypad_number!=0) { GPA 0=1; GPA 1=1; GPA 2=1; } if (keypad_number!=0) { GPA 0=1; GPA 1=1; } else { GPA 0=0; GPA 1=0; }

Program Control : switch-case switch (number) { case 1 : GPA 1=1; break; case 2 : GPA 2=1; break; case 3 : GPA 3=1; break; case 4 : GPA 4=1; break; case 5 : GPA 5=1; break; case 6 : GPA 6=1; break; case 7 : GPA 7=1; break; case 8 : GPA 8=1; break; case 9 : GPA 9=1; break; default: GPA 0=1; } switch (number) { case ‘ 1’ : GPA 1=1; break; case ‘ 2’ : GPA 2=2; break; case ‘ 3’ : GPA 3=3; break; case ‘ 4’ : GPA 4=1; break; case ‘ 5’ : GPA 5=1; break; case ‘ 6’ : GPA 6=1; break; case ‘ 7’ : GPA 7=1; break; case ‘ 8’ : GPA 8=1; break; case ‘ 9’ : GPA 9=1; break; default: GPA 0=1; }

Loop Controls Ø for (i=0; i<max; i++) { Drv. GPIO_Set. Bit (E_GPA, i); printf(“%dn”, i); } while(1) { no = Scankey(); if (no==0) break; else printf (“%dn”, no); } Ø do-while do { no = Scankey(); if (no==0) break; else printf (“%dn”, no); } while (flag);
- Slides: 15