C Programming Tutorial Part 2
0

Within the first a part of our ongoing C programming tutorial sequence, we briefly touched on the preprocessing stage. On this tutorial, we’ll talk about it in a bit of extra element so that you’ve a primary concept about it earlier than studying different C programming elements.

Preprocessing is normally the primary stage of program compilation, where-in something starting with a ‘#’ will get processed. Let’s take a primary instance code.

#embrace <stdio.h>

int foremost (void)
{
    printf("n Hello World n");
    return 0;
}

Sure, this is identical piece of code we used within the first a part of this text sequence. Right here, it is the primary line that begins with a hash ‘#’. In layman’s phrases, the road ‘#embrace <stdio.h>’ makes certain the content material of header file ‘stdio.h’ are included on this program throughout compilation.

So what occurs within the preprocessing stage is, this line will get changed with the precise content material of the header file. Here is how the code takes care of the preprocessing stage:

C Code File

So you possibly can see, every thing above the few traces of code we wrote (have a look at the underside of the screenshot) is the contents of stdio.h. Now, I will not go in particulars of how I managed to see the code after preprocessing, however this was to provide you a transparent concept on what occurs throughout preprocessing.

Transferring on, traces that embrace a header file aren’t the one ones that start with a hash (#). There’s additionally one thing known as Macros that additionally start with #. So it is vital to debate them right here (a minimum of their fundamentals).

A Macro is nothing however a reputation (normally in capital letters) that refers to a bit of code. Macros are outlined utilizing the ‘#outline’ directive. Here is an instance:

#embrace <stdio.h>
#outline PRINT_HELLO_WORLD printf("n Hello World n");
int foremost (void)
{
    PRINT_HELLO_WORLD
PRINT_HELLO_WORLD
    return 0;
}

So we outlined a macro PRINT_HELLO_WORLD with worth printf(“n Hello World n”); . Now, wherever we’ll use this macro title within the code, it can get changed with its worth through the preprocessing stage. Here is the code after pre-processing:

C Program Macro

So that you see that each occurrences of the macro have been changed by its worth on the pre-processing stage. In fact, there are a number of different particulars associated to macros that you need to learn about to be able to efficiently use them. We are going to talk about these particulars in a Macro devoted a part of this tutorial sequence.

Transferring on, the third sort of traces starting with # are #ifdef and #endif. These are used for conditional compilation. To offer you a fast concept, there are occasions if you’d not need part of code to get compiled primarily based on a sure situation, it is in circumstances like these, these directives are used.

Here is an instance:

#embrace <stdio.h>
#outline PRINT_HELLO_WORLD printf("n Hello World n");

int foremost (void)
{
#ifdef CONDITION
    PRINT_HELLO_WORLD
#endif
    PRINT_HELLO_WORLD
    return 0;
}

Within the code above, since ‘CONDITION’ is not outlined anyplace, so the primary prevalence of ‘PRINT_HELLO_WORLD’ shall be omitted from code on the preprocessing stage solely. Here is the proof:

Code condition

So you possibly can see just one printf assertion after the preprocessing stage.

Conclusion

On this tutorial, we mentioned preprocessing to provide C programming novices a primary concept of what occurs throughout this stage. Now, transfer on to half three to be taught additional about C programming fundamentals.

The best way to Set Up an Nginx Ingress with Cert-Supervisor on DigitalOcean Kubernetes

Previous article

Google isn’t the corporate that we should always have handed the Web over to

Next article

You may also like

Comments

Leave a Reply

More in Linux