The C Preprocessor - Lecture notes 4 PDF

Title The C Preprocessor - Lecture notes 4
Author Celeste Prussiani
Course Programming Fundamentals
Institution Edinburgh Napier University
Pages 2
File Size 69.5 KB
File Type PDF
Total Downloads 110
Total Views 441

Summary

Our preprocessor isn't a compiler. It simply substitutes text segments.#include <stdio> : when the preprocessor comes across an instruction with a # in front of it, it deletes the line and fetches the file between the brackets. If the file name is written between quote marks (&...


Description

Our preprocessor isn't a compiler. It simply substitutes text segments. #include : when the preprocessor comes across an instruction with a # in front of it, it deletes the line and fetches the file between the brackets. If the file name is written between quote marks ("stdio.h"), then the preprocessor looks for that file in the same directory as our C source code. stdio.h includes function prototypes: those state what input the functions take in and what they output. In C you need to define a function prototype before actually calling it. This wasn't necessary in Java. What you can do is you can shove your function prototype in a .h file and include it at the top. Conditional compilation The preprocessor is able to execute bits of code if a particular condition is met. This can be done stating, for example #ifdef _WIN32 This bit of code only runs if the code is compiled on Windows. Other conditions are debug, apple, linux… #ifdef DEBUG printf("Debug mode\n"); #endif This is not done at run-time, but a compile-time. Header files We need to ensure we don't include the same header file twice. You're basically including the same functions twice, so the program won't compile. To keep track you can use header guards, that means including an if statement that checks if the function has already been declared or not. #ifndef HELLO_GUARD #define HELLO_GUARD … #endif Another way to do it is including a #pragma statement at the top of every single header file. With header files you can split your code in multiple files. To compile multiple files, you can just list them after cl.

Libraries Libraries are blocks of precompiled code (object code) that you can link into your code in order to include every function there is. File IO C uses the console and files in the same way. Input in the console is called with stdin. You can also use fgets not only for console but for files. For example, if you have a text file you can use fgets to read through it (see lecture). You can use fprintf to print into a file....


Similar Free PDFs