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

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

Pre 2010

Uploaded on 09/02/2009

koofers-user-1ci
koofers-user-1ci 🇺🇸

5

(1)

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Practical C Issues:
Preprocessor Directives, Typedefs, Multi-file
Development, and Makefiles
JonathanMisurda
#include
Copiesthecontentsofthespecifiedfileinto
thecurrentfile
<>indicatetolookinaknownlocationfor
includes
““indicatetolookinthecurrentdirectoryor
specifiedpath
#include <stdio.h>
#include “myheader.h”
#define
Textual SymbolReplacements
#define PI3.1415926535
#define MAX10
float f=PI;
for(i=0;i<MAX;i++)
#define Macros
Textualreplacementswithparameters:
Good:
#define MAX(a,b)(a>b)?a:b
Notsogood:
#define SWAP(a,b){int t=a;a=b;b=t;}
#if
#if<conditionthatcanbeevaluatedbythe
preprocessor>
Whatdoespreprocessorknow?
Valuesof#definedvariables
Constants
Example
#include<stdio.h>
int main()
{
#if0
printf(“this isnotprinted\n”);
#endif
printf(“This isprinted\n”);
return0;
}
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