Basic C language
Basic C
For C, generally a two part process of compiling source files(.c) to object files(.o), then linking the .o files into executables.
C Pre-Processor(CPP)
- C source files first pass through “macro preprocessor”, before compiler sees code
- CPP commands begins with “#”
- #include “file.h” // Inserts file.h into output
- #include
// Looks for file in standard location - define M_PI 3.14 // Define constant
- #if / #endif // Conditional inclusion of text
Structs in C
typedef struct{
int length_in_seconds;
int year_recorded;
}Song
Switch
switch(expressions){
case const1: statements
case const2: statements
default: statements
}
What evaluates to FALSE in C
- 0(integer)
- NULL(a special kind of pointer that is also 0: more on this later)
- No explicit Boolean type in old-school C
- Often u see #define bool (int)
- Then #define false 0
- Alternative approach: include a header file #include
to provide a boolean type
- Basically anything where all the bits are 0 is false
Our Tip of the Day.. Valgrind
- Valgrind
- Use a <= instead of a < in initializing an array in a completely different part of the program.
Types of Pointers
-
*void ** is a type that can point to anything
-
Use *void ** sparingly to help avoid programs bugs, and security issues, and other bad thing
-
U can have pointers to pointers
-
U can even have pointers to functions…
int (*fn)(void *,void *) = &foo // fn is a function that accepts two void* pointers and returns an int // and initially pointing to the function foo // (*fn)(x,y) will then call the function
More C Pointer Dangers
- Declaring a pointer just allocates space to hold the pointer-it does not allocate the thing being pointed to.
- Local variables in C are not initialized, they may contain anything.