SlideServe
Menu
Préférences
© SlideServe · v2026

Preprocessor

kiona avatar
14 diapositives · 473 Views · Sep 11, 2014

Preprocessor. All preprocessor directives or commands begin with a #. E.g. #include <stdio.h> C program → Modified C program → Object Code Can appear anywhere in the program No “;” in the end. preprocessor. compiler. Preprocessor Directives. Macro definition #define, #undef

Preprocessor - slide 1
01 / 14Preprocessor
1 / 17

Télécharger la présentation

Preprocessor
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Preprocessor • All preprocessor directives or commands begin with a #. • E.g. #include <stdio.h> C program → Modified C program → Object Code • Can appear anywhere in the program • No “;” in the end preprocessor compiler

  2. Preprocessor Directives • Macro definition • #define, #undef • File inclusion • #include • Conditional Compilation • #if, #ifdef, #ifndef, #elseif, #else • Others

  3. Advantages • Easy to • Develop program • Read programs • Modify programs • C code more transportable between different machine architectures

  4. #define • To define constants or any macro substitution.     #define <macro> <replacement name>   E.g.   #define FALSE 0 #define TRUE !FALSE • To undefined a macro. E.g. #undef FALSE • A macro must be undefined before being redefined to a different value.

  5. Define Functions • E.g. To get the maximum of two variables: #define max(A,B) ( (A) > (B) ? (A):(B)) • If in the C code:    x = max(q+r,s+t); • After preprocessing:    x = ( (q+r) > (s+t) ? (q+r) : (s+t));

  6. File inclusion • #include directive • Include the contents of another file at the point where the directive appears. • Why need file inclusion • Use built-in functions • E.g., printf(), rand(); • Reuse code struct date{ int day; char month[10]; int year; }; struct date{ int day; char month[10]; int year; }; struct date today; #include “defTime.h” → struct date today; defTime.h Your code

  7. File inclusion formats • #include <file> • Used for system header files • File contain function prototypes for library functions • <stdlib.h> , <math.h> , etc • #include "file" • Used for header files of your own program • looks for a file in the current directory first • then system directories if not found

  8. Standard C libraries • Build-in with your compiler • Detailed information • http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html • stdio.h • core input and output capabilities of C • printf function • scanf function

  9. Math Library Functions • Math library functions • perform common mathematical calculations • E.g. exp(), pow(), sqrt(), fabs(), sin(), cos(). • #include <math.h> • Format for calling functions • FunctionName(argument, …, argument); • All math functions return data type double • E.g.: printf( "%.2f", sqrt( 900.0 ) ); • Arguments may be constants, variables, or expressions • Compile • gcc yourfilename.c –lm –o yourfilename.exe

  10. stdlib.h • A variety of utility functions • rand function • A function to generate a pseudo-random integer number • Return value is in the range 0 to RAND_MAX • Example: #include <stdlib.h> int i = rand(); • memory allocation • process control

  11. string.h • All the string handling functions • strcpy • strcat • strcmp

  12. Custom header files • Steps for creating custom header files • Create a file with function prototypes • Save as a .h file. E.g.: filename.h • Load in other files with • #include "filename.h" • Advantage • Reuse functions and data structure declaration

  13. Conditional compilation • Useful when • machine-dependencies • Debugging • setting certain options at compile-time. • Expressions • #if expression • #ifdef expression (same as #if defined ) • #ifudef • #elif and #else • #endif

  14. Examples • Example: write programs that are portable to several machines or operating systems • #if defined(WINDOWS) • #elif defined(LINUX) • #elif defined(SOLARIS) • #endif • Example: Providing a default definition for a macro • #ifndef BUFFER_SIZE • #define BUFFER_SIZE 256 • #endif