|
1
|
|
|
2
|
- Syntax
- Grammar
- Blondes have black hair - correct
syntax
- Semantics
- Meaning
- delete object; // program crashes here
|
|
3
|
- Another name: primitive types or fundamental types
- Type - some abstraction with corresponding semantics (for example,
natural and real numbers)
- Represented as a sequence of bytes
- Signed or unsigned
|
|
4
|
- Signed types
- When interpreted as numbers can be negative or positive (-12 or 12)
- FF = 11111111 = -1 7F = 01111111
= 127
- Unsigned types
- Interpreted as positive numbers (pointers, because memory addresses
start from 0)
|
|
5
|
- Compiler and platform dependent:
- char (signed)
- wchar_t (signed) for Unicode
- short (signed)
- int (signed)
- long (signed)
- unsigned char
- unsigned short
- unsigned int
- unsigned long
- bool
- float (signed)
- double (signed)
- MSFT specific: __intN (n=8,16,32 or 64)
- __int8, __int64
|
|
6
|
- Operator
- Manipulate variables and build expressions: a + 2
- Operand
- Variable or other expression: (a + 2) * b
- Expression
- Some sentence that combines operands and operators; terminates by ;
- int a = 5; // initialization expression
- a = 6; // assignment expression
- Expression evaluation
- Applying operators to operands to yield some value
- Expression type
- The type of the evaluated value
|
|
7
|
- Declaration: creates a name, makes the name visible, so we can use it;
doesnt allocate memory
- extern int counter;
- counter = 10;
- Definition: declares a variable and allocates memory
- int counter;
- One Definition Rule (ODR): only one definition is allowed but multiple
same declarations
- extern int counter;
- extern int counter;
- int counter;
|
|
8
|
|
|
9
|
- Compilation unit
- C++ file plus included files (usually header files with declarations)
- ODR across compilation units
- Linker will complain if we have the same definition in different units
because we cannot allocate the memory twice for the same variable (which
memory location to use?)
|
|
10
|
- // main.c
- int maxCounter;
- MaxCounter = 10;
|
|
11
|
- Include file directive
- Define and undefine directives
- Conditional compilation
- Macros
- Other directives
|
|
12
|
- #include file
- Search current directory; search standard directories
- #include <file>
- Search only standard directories
|
|
13
|
- #define name
- #define DEBUG
- #define name text
- #define MAXSTR 256
- int maxSize = MAXSTR; // replaced by 256
- #undef name
- #undef DEBUG
|
|
14
|
- #define NewType BuiltInType
- #define BOOL int
- BOOL choice;
- #define HANDLE int
- HANDLE hFile;
- #define HWND int
- HWND window;
- #define DWORD unsigned long
- DWORD dwCounter;
|
|
15
|
- #define HANDLE int
- HANDLE hFile;
- #define HWND int
- HWND window;
- if (hFile == window)
- {
- hFile = window; // no warning
during compilation
- }
- double debt;
- int value;
- value = debt; // warning C4244: '=' : conversion from 'double' to 'int',
possible loss of data
|
|
16
|
- #ifdef name
- #ifdef DEBUG
- OutputDebugString(
);
- #endif
|
|
17
|
- #ifndef name
- #ifndef NDEBUG
- OutputDebugString(
);
- #endif
|
|
18
|
- #ifdef UNICODE
- #define TCHAR wchar_t
- #else
- #define TCHAR char
- #endif
- TCHAR character;
|
|
19
|
- Constant expression
- Any expression that consists only from constants
- #if 0
- // exclude code from compilation
- #endif
|
|
20
|
- Parameterized text replacement
- #define Name(param1, param2,
)
- !!! No space between Name and (
- #define PRINT(string) OutputDebugString(string)
- PRINT(Hello!);
|
|
21
|
- #define TRACE(module, string) if (debuggingEnabled) {\
- OutputDebugString(module);\
- OutputDebugString(: );\
- OutputDebugString(string);\
- OutputDebugString(\n); }
- TRACE(PROGRAM.EXE, Hello!);
|
|
22
|
- __DATE__
- __TIME__
- __LINE__
- __FILE__
- #define TRACE(string) if (debuggingEnabled) {\
- OutputDebugString(__FILE__);\
- OutputDebugString(: );\
- OutputDebugString(string);\
- OutputDebugString(\n); }
|
|
23
|
|
|
24
|
- Macros and macro definitions are substituted before compilation
- Names and macros introduced by define directives are usually fully
capitalized (TRACE, DEBUG, MAXSTR)
- Pay attention to conditional compilation directives
|