



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of various practical issues in c programming, focusing on preprocessor directives, typedefs, multi-file development, and makefiles. Topics include #include statements, preprocessor symbols, conditional compilation, and more. Useful for both beginners and experienced programmers.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Preprocessor Directives, Typedefs, Multi-file Development, and Makefiles
Jonathan Misurda [email protected]
#include <stdio.h> #include “myheader.h”
#define PI 3. #define MAX 10
float f = PI; for(i=0;i<MAX;i++) …
#include <stdio.h> int main() { #if 0 printf(“this is not printed\n”); #endif printf(“This is printed\n”); return 0; }
#include <stdio.h> #define VERSION 5
int main() { #if VERSION < 5 printf(“this is not printed\n”); #endif printf(“This is printed\n”); return 0; }
#if … #elif … #else … #endif
#include <stdio.h> #define MACRO int main() { #if defined MACRO printf(“this is printed\n”); #endif printf(“This is also printed\n”); return 0; }
int main() { #if defined MACRO printf(“this is not printed\n”); #endif printf(“This is printed\n”); return 0; }
Typedef
typedef struct node { int i; struct node *next; } Node;
Node * head;
Struct with Instance
struct node { int i; struct node *next; } Node;
#include#include <stdio.h><stdlib.h> typedef void (FP)(int, int); void f(int a, int b) { }^ printf("%d\n",^ a+b) void g(int a, printf("%d\n", int b) ab) { } int main()FP {ar1 = f; FP ar2 = g; ar1(2,3);ar2(2,3); return 0; }
void qsort ( void base , size_t num , size_t size , int (comparator)(const void *, const void *) );
int compare_ints(const void *a,const void *b) { int *x = (int *)a; int *y = (int *)b;
return *x ‐ *y; }
void f(…) { static int x; … }
File B extern int x;
Will refer to the same memory location
a.c int x = 0; int f(int y) { return x+y; }
b.c #include <stdio.h> extern int x; int f(int); int main() { x = 5; printf("%d", f(0)); return 0; }
gcc a.c b.c
./a.out 5
a.c static int x = 0; static int f(int y) { return x+y; }
b.c #include <stdio.h> extern int x; int f(int); int main() { x = 5; printf("%d", f(0)); return 0; }
gcc a.c b.c
/tmp/cccyUCUA.o(.text+0x6): In function main': : undefined reference tox' /tmp/cccyUCUA.o(.text+0x19): In function main': : undefined reference tof' collect2: ld returned 1 exit status