Industriële fabricage
Industrieel internet der dingen | Industriële materialen | Onderhoud en reparatie van apparatuur | Industriële programmering |
home  MfgRobots >> Industriële fabricage >  >> Industrial programming >> C Taal

C++ Preprocessor: Hoe Directives Je Code Vormgeven

Preprocessordirectives geven instructies aan de compiler om informatie te verwerken voordat de daadwerkelijke compilatie begint.

Alle directives beginnen met # en mogen alleen voorafgegaan worden door witruimte. Ze zijn geen C++-statements en eindigen dus niet met een puntkomma.

Je hebt #include al gezien: het voegt een headerbestand toe aan het bronbestand.

De #define Preprocessor

#define creëert symbolische constanten (macros). De vorm:

#define macro-naam vervangende-tekst

Alle volgende voorkomens van de macro in dat bestand worden voor compilatie vervangen door de vervangende tekst.

Voorbeeld: PI constant

#include <iostream>
using namespace std;
#define PI 3.14159

int main() {
    cout << "Waarde van PI: " << PI << endl;
    return 0;
}

Na preprocessing (gcc -E):

int main() {
    cout << "Waarde van PI: " << 3.14159 << endl;
    return 0;
}

Functie-achtige Macros

Macros kunnen argumenten nemen:

#include <iostream>
using namespace std;
#define MIN(a,b) (((a)<(b)) ? a : b)

int main() {
    int i = 100, j = 30;
    cout << "Het minimum is " << MIN(i, j) << endl;
    return 0;
}

Uitvoer: Het minimum is 30

Conditionele Compilatie

Met directives zoals #if, #ifdef, #ifndef, #else, #elif, #endif compileer je selectief.

Voorbeeld: NULL definiëren als nog niet gedaan:

#ifndef NULL
#define NULL 0
#endif

Debugging aan/uit met één macro:

#ifdef DEBUG
cerr << "Variabele x = " << x << endl;
#endif

Code uitcommentariëren met #if 0:

#if 0
code die niet gecompileerd wordt
#endif

Voorbeeld: Conditionele compilatie

#include <iostream>
using namespace std;
#define DEBUG
#define MIN(a,b) (((a)<(b)) ? a : b)

int main() {
    int i = 100, j = 30;
    #ifdef DEBUG
    cerr << "Trace: In main functie" << endl;
    #endif
    #if 0
    cout << MKSTR(HELLO C++) << endl;
    #endif
    cout << "Het minimum is " << MIN(i, j) << endl;
    #ifdef DEBUG
    cerr << "Trace: Verlaten main functie" << endl;
    #endif
    return 0;
}

Uitvoer:

Het minimum is 30
Trace: In main functie
Trace: Verlaten main functie

De # en ## Operatoren

# (stringizing) zet een token om in een string tussen aanhalingstekens.

#define MKSTR(x) #x
cout << MKSTR(HELLO C++) << endl; // wordt: cout << "HELLO C++" << endl;

Uitvoer: HELLO C++

## (token pasting) plakt twee tokens aan elkaar.

#define CONCAT(x, y) x ## y

CONCAT(HELLO, C++) wordt HELLO C++.

Voorbeeld: Token plakken

#include <iostream>
using namespace std;
#define concat(a, b) a ## b

int main() {
    int xy = 100;
    cout << concat(x, y); // wordt cout << xy;
    return 0;
}

Uitvoer: 100

Vooraf Gedefinieerde C++ Macros

NrMacro & Beschrijving
1__LINE__ – Huidig regelnummer tijdens compilatie.
2__FILE__ – Huidige bestandsnaam tijdens compilatie.
3__DATE__ – Compilatiedatum (maand/dag/jaar).
4__TIME__ – Compilatietijd (uur:minuut:seconde).

Voorbeeld

#include <iostream>
using namespace std;

int main() {
    cout << "__LINE__: " << __LINE__ << endl;
    cout << "__FILE__: " << __FILE__ << endl;
    cout << "__DATE__: " << __DATE__ << endl;
    cout << "__TIME__: " << __TIME__ << endl;
    return 0;
}

Mogelijke uitvoer:

__LINE__: 6
__FILE__: test.cpp
__DATE__: Feb 28 2011
__TIME__: 18:52:48

C Taal

  1. C# preprocessor directives: een praktische gids
  2. C++ datatypes begrijpen: fundamentals, modifiers en best practices
  3. C++ operators: complete gids met praktische voorbeelden
  4. C++ comments: best practices voor leesbare, onderhoudbare code
  5. C++ constructors: default, parameterized en copy constructors uitgelegd
  6. C++ Class Templates Beheersen: Een Praktische Gids
  7. De C-preprocessor (CPP) begrijpen: Hoe het de compilatie vereenvoudigt
  8. C++ Programmeertaal: Overzicht & Belangrijkste Kenmerken
  9. C++ Constanten & Literals Uitgelegd: Types, Syntax en Best Practices
  10. Uitgebreide Gids voor C++ Operators: Types, Gebruik en Best Practices
  11. Numerieke Typen in C++ Beheersen: Een Praktische Gids