Docsity

C Programming: Preprocessor, Typedefs, Multi-file Dev, and Makefiles Guide - Prof. Jonatha, Study notes of Computer Science

Toggle sidebar
bg1
Practical C Issues:
Preprocessor Directives, Typedefs, Multi-file
Development, and Makefiles
Jonathan๎˜ƒMisurda
#include
โ€ขCopies๎˜ƒthe๎˜ƒcontents๎˜ƒof๎˜ƒthe๎˜ƒspecified๎˜ƒfile๎˜ƒinto๎˜ƒ
the๎˜ƒcurrent๎˜ƒfile
โ€ข<๎˜ƒ>๎˜ƒindicate๎˜ƒto๎˜ƒlook๎˜ƒin๎˜ƒa๎˜ƒknown๎˜ƒlocation๎˜ƒfor๎˜ƒ
includes
โ€ขโ€œโ€œindicate๎˜ƒto๎˜ƒlook๎˜ƒin๎˜ƒthe๎˜ƒcurrent๎˜ƒdirectory๎˜ƒor๎˜ƒ
specified๎˜ƒpath
#include <stdio.h>
#include โ€œmyheader.hโ€
#define
โ€ขTextual Symbol๎˜ƒReplacements
#define PI๎˜ƒ3.1415926535
#define MAX๎˜ƒ10
float f๎˜ƒ=๎˜ƒPI;
for(i=0;i<MAX;i++)๎˜ƒโ€ฆ
#define Macros
โ€ขTextual๎˜ƒreplacements๎˜ƒwith๎˜ƒparameters:
โ€ขGood:
โ€“#define MAX(a,๎˜ƒb)๎˜ƒ(a๎˜ƒ>๎˜ƒb)๎˜ƒ?๎˜ƒa๎˜ƒ:๎˜ƒb
โ€ขNot๎˜ƒso๎˜ƒgood:
โ€“#define SWAP(a,b)๎˜ƒ{int t=a;๎˜ƒa=b;๎˜ƒb=t;}
#if
โ€ข#if๎˜ƒ<condition๎˜ƒthat๎˜ƒcan๎˜ƒbe๎˜ƒevaluated๎˜ƒby๎˜ƒthe๎˜ƒ
preprocessor>
โ€ขWhat๎˜ƒdoes๎˜ƒpreprocessor๎˜ƒknow?๎˜ƒ
โ€“Values๎˜ƒof๎˜ƒ#defined๎˜ƒvariables
โ€“Constants
Example
#include๎˜ƒ<stdio.h>
int main()
{
#if๎˜ƒ0
printf(โ€œthis is๎˜ƒnot๎˜ƒprinted\nโ€);
#endif
printf(โ€œThis is๎˜ƒprinted\nโ€);
return๎˜ƒ0;
}
pf3
pf4
pf5

Partial preview of the text

Download C Programming: Preprocessor, Typedefs, Multi-file Dev, and Makefiles Guide - Prof. Jonatha and more Study notes Computer Science in PDF only on Docsity!

Practical C Issues:

Preprocessor Directives, Typedefs, Multi-file Development, and Makefiles

Jonathan Misurda [email protected]

#include

  • Copies the contents of the specified file into the current file
  • < > indicate to look in a known location for includes
  • โ€œ โ€œ indicate to look in the current directory or specified path

#include <stdio.h> #include โ€œmyheader.hโ€

#define

  • Textual Symbol Replacements

#define PI 3. #define MAX 10

float f = PI; for(i=0;i<MAX;i++) โ€ฆ

#define Macros

  • Textual replacements with parameters:
  • Good:
    • #define MAX(a, b) (a > b)? a : b
  • Not so good:
    • #define SWAP(a,b) {int t=a; a=b; b=t;}

#if

  • #if
  • What does preprocessor know?
    • Values of #defined variables
    • Constants

Example

#include <stdio.h> int main() { #if 0 printf(โ€œthis is not printed\nโ€); #endif printf(โ€œThis is printed\nโ€); return 0; }

Example 2

#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; }

#else

#if โ€ฆ #elif โ€ฆ #else โ€ฆ #endif

#ifdef

  • #if defined
    • Checks to see if a macro has been defined, but doesnโ€™t care about the value
    • A defined macro might expand to nothing, but is still considered defined

Example

#include <stdio.h> #define MACRO int main() { #if defined MACRO printf(โ€œthis is printed\nโ€); #endif printf(โ€œThis is also printed\nโ€); return 0; }

#undef

  • Undefines a macro: #include <stdio.h> #define MACRO #undef MACRO

int main() { #if defined MACRO printf(โ€œthis is not printed\nโ€); #endif printf(โ€œThis is printed\nโ€); return 0; }

Shortcuts

  • #if defined โ†’ #ifdef
  • #if !defined โ†’ #ifndef

Structures

Typedef

typedef struct node { int i; struct node *next; } Node;

Node * head;

Struct with Instance

struct node { int i; struct node *next; } Node;

Function Pointers

#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; }

Function Pointers As Parameters

void qsort ( void base , size_t num , size_t size , int (comparator)(const void *, const void *) );

Comparator

int compare_ints(const void *a,const void *b) { int *x = (int *)a; int *y = (int *)b;

return *x โ€ *y; }

Multi-file Development

  • Want to break up a program into multiple files
    • Easier to maintain
    • Multiple authors
    • Quicker compilation
    • Modularity

Static Local Scope

  • Scope: Local
  • Lifetime: โ€œGlobalโ€ (life of program)

void f(โ€ฆ) { static int x; โ€ฆ }

File Scope

  • โ€œGlobal Variablesโ€ are actually limited to the file
  • extern maybe be used to import variables from other files File A int x;

File B extern int x;

Will refer to the same memory location

Example

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; }

Compiling

gcc a.c b.c

./a.out 5

Static

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; }

Compiling

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

Header Files

  • Usually only contain declarations
    • Variables
    • Functions
    • #defined macros
  • Paired with an implementation file