File Operations
File Operations
Macros are very common and often used in ‘C’ programming language to declare constants like strings,
addresses, or any other values such as maximum number of elements in an array and so on.
Before a C program is compiled, it is passed through another program is called preprocessor.
Preprocessor is a smart editor. It inserts, includes, excludes and replaces text based on commands
supplied by the programmer.
For those who are not familiar with them, Macros are declared by the #define keyword. Macros are
also used to define some basic functionality given a set of one or more typeless parameters, similarly to
an inline function in c++.
Preprocessor extends the power of ‘C’ programming language. Line that begin with # are called
preprocessing directives.
Use of #include
Let us consider very common preprocessing directive as below:
#include <stdio.h>
Here, “stdio.h” is a header file and the preprocessor replace the above line with the contents of
header file.
Use of #define
Preprocessing directive #define has two forms. The first form is,
#define identifier token_string
token_string part is optional but are used almost every time in program.
Example
#define c 299792458 /*speed of light in m/s */
The token string in above line 2299792458 is replaced in every occurrence of symbolic constant c.
‘C’ Program to find area of a cricle. [Area of circle=]
#include <stdio.h>
#define PI 3.1415 /* During preprocessing every occurrence of PI is replaced by 3.1415 */
main()
{ int radius;
float area;
printf(“Enter the radius: “);
scanf(“%d”,&radius);
area=PI*radius*radius;
printf(“Area=%.2f”,area);
}
Output
Enter the radius: 3
Area=28.27
# include<stdio.h> This format is used to direct the processor to include header file stdio.h from the
system library.
#include “filename.h” This format makes preprocessor look for the header files in the user defined
directory.
Benefits by using Macros
1. Code Maintainability: Using a macro to define a constant provides an easy and correct method to
ensure that if this value needs to be updated; all instances of this value will be automatically
updated. Otherwise, the program may behave incorrectly, and even crash.
2. Code Readability: Specifying a macro name is much more readable and understandable to someone
who needs to read the code, than a plain number or address.
Disadvantages by using Macros
1. Hard to Extend: If the macros become complex and a change is required, any errors you introduce
may yield vague compilation errors by the compiler, and always, the error line number points to the
start of the macro.
2. Hard to Debug: Debuggers often do not provide clear access and step ability to a code inside
macros.
Important Points about Macros
1. Macros are expanded from the top of the file, to the bottom.
2. Macros can be defined, undefined and then redefined.
3. Macros, unlike other code, are not being compiled. You can write junk stuff as a macro and the file
will be compiled. Due to the same reason, macros can contain any text, even if it is not declared or
defined yet.
4. Macros are expanded only when used in the code. If you’ll use a junk macro in the code, it will be
converted to junk which will fails your compilation.
5. Macros can span over only a single line. The only way to extend a macro to another line is by adding
a continuation token ‘\’ in the end of the line. No other character is allowed after the new line
marker. i.e., continuation to key(1) must immediately followed by a newline. If any white space is
present between (\) and newline then backslash is not a continuation token. EX: # define END\
printf(“Normal End”);
6. You can always inspect the expanded macros by using the same compilation command, with the
addition of the -E parameter. The output in this case, will be the preprocessed source code, where all
the macros are expanded.
Macro Functions: A function that accept parameters and return values are called macro functions. To
create a macro function, simply define a macro with a parameter that has whatever name you like, such as
my_Val. For example, one macro defined in the standard libraries is abs, which returns the absolute value
of its parameter. Let us define our own version, ABS below. (Note that we are defining it in upper case
not only to avoid conflicting with abs, but also because all macros should be defined in upper case, in the
GNU coding style).
# define ABS (my_Val) ((my_Val) <0)?-(my_Val): (my_Val)
This macros uses the ‘? …..’command to return a positive number, no matter what value is assigned
to my_Val is defined as a positive number. The macro returns the same number, and if my _Val is
defined as a negative number the macro returns its negative (which will be positive). If you write ABS (-
4), then the preprocessor will substitute -4 for my_Val. Macros can take more than one parameter, as in
the code example below.
One condition is that Macros are substituted in the program when it is used. This is potentially a
huge amount of code repetition. The advantage of a macro over an actual function is speed, no time is
taken up in passing control to a new function, because control never leaves the home function. The macro
just makes the function a bit longer.
A second condition function calls cannot be used as macro parameters. The following code will not
work:
ABS (cos (36))
Program-13
# include <stdio.h>
#define STRING1 “Spectrum\n”
#define STRING2 “must be all on one line!\n”
#define EXPRESSION1 1+2+3+4
#define EXPRESSION2 EXPRESSION1+10
#define ABS(x) ((x) <0?-(x): (x)
#define MAX(a, b) (a<b)?(b): (a)
#define BIGGEST(a, b, c) (MAX (a, b)<c)?(c) : (MAX(a, b))\
int main ()
{
printf (STRING1);
printf (STRING 2);
printf(“%d/n”, EXPRESSION1);
printf(“%d/n”, EXPRESSION2);
printf(“% d/n”, ABS(-10));
printf (“Biggest of 1, 2, and 3 is %d\n”, BIGGEST(1, 2,3));
return 0;
}
Output
Spectrum
must be all on one line!
10
20
10
Biggest of 1, 2 and 3 is 3
Macros with Argument: Preprocessing directive #define can be used to write macro definitions with
parameters as well in the form below:
#define identifier(identifier 1,.....identifier n) token_string
Again, the token string is optional but, are used in almost every case. Let us consider an example of
macro definition with argument.
#define area(r) (3.1415*r*r)
Here, the argument passed is r. Every time the program encounters area(argument), it will be replace
by (3.1415*argument*argument). Suppose, we passed (r1+5) as argument then, it expands as below,