Test/C2/Testing/English
Title of script:Preprocessor Directives in C
Author:Ashwini Patil
Keywords:Video tutorial, #include, #define, #if, #undef, #else, #endif
|
|
---|---|
Display Slide 1 | Welcome to the Spoken Tutorial on Preprocessor Directives in C |
Display Slide 2 | In this tutorial, we will learn about
|
Display Slide 3
System Requirements |
For this tutorial I am using
|
Display Slide 4
Prerequisites
spoken-tutorial.org |
To follow this tutorial you should be familiar with C tutorials.
If not, for relevant tutorials please visit spoken hyphen tutorial dot org. |
I will start with an introduction toPreprocessor Directives. | |
Display Slide 5-6
Introduction |
|
Display Slide 7
Syntax:
|
There are different types ofPreprocessor Directives.
1) Defining preprocessor directives.
Macro:Macro is a code block with a given name.
To use the code block we have to use the name of the macro. #undef:This is used to undefine the definedmacro Syntax:
|
Display Slide 8
Syntax:
|
2)Header Files inclusion
#include <stdio.h>:The functions and code ofstdio.h headeris included in the main program. Syntax:
|
Display Slide 9 | 3)Conditional compilation
Check for condition before compilation #ifdef:This checks whether a macro is defined in the program or not. If yes then the if clause is statements are added in the program. #else:If the macro is not defined the else part is added. #endif:This ends the preprocessor conditions. |
Let us go through an example code. | |
Point to the filename. | Note that our filename isarea.c
In this program, we will calculate the area of a circle. |
|
Here we have defined a constant value for radiusand pi using#define keyword.
Then we calculate the area of the circle and print the result. |
Type:
gcc area.c -o area ./area |
Let us execute the program.
On the terminal. Type: gcc area.c -o area ./area |
Highlight Output | The output is:
Radius of circle : 15 Value of pi : 3.140000 Area of the circle : 706.500000 |
Now let us see an example onconditional directives.
We will use the same example. | |
Type:
{ close the brackets after the lastprintf statement } |
I will add a few lines here.
In main function type:
(opening bracket) { close the brackets after the lastprintf statement } |
type:
{ printf(“Radius of the circle is not defined”); }
|
Now after the closing bracket type:
(opening bracket){ printf(“Radius of the circle is not defined”); (closing bracket)}
|
On the terminal | Click onSave.
Let us execute again. On the terminal press the up-arrow key twice. Again press the up-arrow key twice. |
Highlight the output | We see the same output.
Radius of circle : 15 Value of pi : 3.140000 Area of the circle : 706.500000 |
Comment
//#define radius 15. |
Now come back to the terminal.
Let us comment the second line //#define radius 15. Click onSave. On the terminal. Execute again. |
Highlight Output | We see,
Radius of circle not defined. |
Now let us see the use of#undef derivative.
Come back to out program Now insideifcondition,I will undefine theradius. And redefine it with a new value | |
Type
|
Type:
|
Highlight output | Now click onSave.
Let us execute on terminal. Press the uparrow key twice Again Press the uparrow key twice You can see the output as: Radius of circle : 20 Value of pi : 3.140000 Area of the circle : 1256.000000 |
This brings us to the end of this tutorial.
Let us summarize. | |
Display Slide 13
Summary |
In this tutorial we learnt,
|
Display Slide 14 | As an assignment, write a program to calculate the factorial of a number.
Define the number using#define keyword |
Display Slide 15 | Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Display Slide 16
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Display Slide 15
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini from IIT Bombay.
Thank you for joining. |