Difference between revisions of "Week 2"
(3 intermediate revisions by the same user not shown) | |||
Line 46: | Line 46: | ||
This will print 50, not 60. It replaces SUM with a+b then operator precedence allows it to multiply first before addition. | This will print 50, not 60. It replaces SUM with a+b then operator precedence allows it to multiply first before addition. | ||
+ | |||
+ | |||
+ | <u>Macros</u> | ||
+ | |||
+ | ''#define SUM(x, y) (x+y)'' | ||
+ | |||
+ | ''printf("%d\n", SUM(i, j)*2);'' | ||
+ | |||
+ | ''printf("%d\n", SUM(a, b)*2);'' | ||
+ | |||
+ | Those two statements will have ''i+j'' and ''a+b'' instead. | ||
+ | |||
+ | Why not replace SUM(a, b) to a+b in the code instead of telling SUM to do it for you before compilation? | ||
+ | |||
+ | You might need to change several different things. Could also make it easier to manage code? | ||
+ | |||
+ | Another example: ''#define MAX(x, y) ((x)>(y)?(x):(y))'' | ||
+ | |||
+ | <u>Conditional Compilation</u> | ||
+ | |||
+ | With conditional compilation you can tell the compiler to only compile a section of the code. | ||
+ | |||
+ | ''#define COMPINT 1'' | ||
+ | ''#define COMPDOUBLE 2'' | ||
+ | ''#define COMP COMPINT'' | ||
+ | |||
+ | In the code: | ||
+ | |||
+ | ''#ifdef COMP == COMPINT'' | ||
+ | ''Code'' | ||
+ | ''Code'' | ||
+ | ''Code'' | ||
+ | ''#endif'' | ||
+ | |||
+ | ''#ifdef COMP == COMPDOUBLE'' | ||
+ | ''Code'' | ||
+ | ''Code'' | ||
+ | ''Code'' | ||
+ | ''#else'' | ||
+ | ''Code'' | ||
+ | ''Code'' | ||
+ | ''Code'' | ||
+ | ''#endif'' | ||
+ | |||
+ | It will only compile the first if statement and the else statement. | ||
+ | |||
+ | |||
+ | Thursday May 20, 2010 | ||
+ | |||
+ | Cursor Movement Low-Level Header Files: | ||
+ | |||
+ | '''Platforms: Headers: ''' | ||
+ | BCC: conio.h | ||
+ | VCC: windows.h and conio.h | ||
+ | Linux: ncurses.h | ||
+ | Unix: curses.h |
Latest revision as of 12:44, 20 May 2010
Reviewing IPC 144
The Question Mark Operator
c = a > b ? 100 : 200;
It replaces an if statement and is much faster. If a is greater than b then 100, else 200.
Please note: The types 100 and 200 must be the same type or else it will not work.
Function Calls
printf("%d %d, b, b = b + 1);
It returns 21 21. Why? Some compilers stack the arguments and read the last argument first. Therefore, it would see b as 21 as well.
a = printf("%d %d, b, b = b + 1);
printf("%d\n", a);
What is a?
6
printf returns the number of characters printed (scanf also returns the number of characters input - special note: scanf cannot return a number greater than the % symbols in your scanf statement).
A function must have one point of entry and one point of exit. Only one return statement per function.
The Include Statement
#include <stdio.h>
The hash tag (#) tells the compiler how to do things BEFORE compilation.
The #include keyword brings in the code from inside <stdio.h>
Header files are usually definitions of functions, classes, templates, etc.
The Define Statement
A #define statement is a simple search and replace in the current file.
#define SUM a+b
printf("%d\n", SUM*2);
This will print 50, not 60. It replaces SUM with a+b then operator precedence allows it to multiply first before addition.
Macros
#define SUM(x, y) (x+y)
printf("%d\n", SUM(i, j)*2);
printf("%d\n", SUM(a, b)*2);
Those two statements will have i+j and a+b instead.
Why not replace SUM(a, b) to a+b in the code instead of telling SUM to do it for you before compilation?
You might need to change several different things. Could also make it easier to manage code?
Another example: #define MAX(x, y) ((x)>(y)?(x):(y))
Conditional Compilation
With conditional compilation you can tell the compiler to only compile a section of the code.
#define COMPINT 1 #define COMPDOUBLE 2 #define COMP COMPINT
In the code:
#ifdef COMP == COMPINT
Code Code Code
#endif
#ifdef COMP == COMPDOUBLE
Code Code Code
#else
Code Code Code
#endif
It will only compile the first if statement and the else statement.
Thursday May 20, 2010
Cursor Movement Low-Level Header Files:
Platforms: Headers: BCC: conio.h VCC: windows.h and conio.h Linux: ncurses.h Unix: curses.h