Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Preprocessor statements

c



+ Font mai mare | - Font mai mic



Preprocessor statements

The first statement in the example is a preprocessor directive. In days gone by, the C compiler used to have two phases: the preprocessor, followed by the real compiler. The preprocessor was a macro processor, whose job was to perform simple textual manipulation of the program before passing the modified text on to be compiled. The preprocessor rapidly became seen as an essential aspect of the compiler and so has now been defined as part of the language and cannot be bypassed.

The preprocessor only knows about lines of text; unlike the rest of the language it is sensitive to the end of a line and though it is possible to write multi-line preprocessor directives, they are uncommon and a source of some wonder when they are found. Any line whose first visible character is a # is a preprocessor directive.



In Example 1.1 the preprocessor directive #include causes the line containing it to be replaced completely by the contents of another file. In this case the filename is found between the < and > brackets. This is a widely used technique to incorporate the text of standard header files into your program without having to go through the effort of typing it all yourself. The <stdio.h> file is an important one, containing the necessary information that allows you to use the standard library for input and output. If you want to use the I/O library you must include <stdio.h>. Old C was more relaxed on this point.

Define statements

Another of the preprocessor's talents which is widely exploited is the #define statement. It is used like this:

#define IDENTIFIER replacement

which says that the name represented by IDENTIFIER will be replaced by the text of replacement whenever IDENTIFIER occurs in the program text. Invariably, the identifier is a name in upper-case; this is a stylistic convention that helps the reader to understand what is going on. The replacement part can be any text at all-remember the preprocessor doesn't know C, it just works on text. The most common use of the statement is to declare names for constant numbers:

#define PI 3.141592
#define SECS_PER_MIN 60
#define MINS_PER_HOUR 60
#define HOURS_PER_DAY 24

and to use them like this

circumf = 2*PI*radius;
if(timer >= SECS_PER_MIN)

the output from the preprocessor will be as if you had written this:

circumf = 2*3.141592*radius;
if(timer >= 60)

Summary

Preprocessor statements work on a line-by-line basis, the rest of C does not.

#include statements are used to read the contents of a specified file, typically to facilitate the use of library functions.

#define statements are typically used to give names for constants. By convention, the names are in upper case (capitalized).



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 654
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved