Test/C2/Testing/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script:Preprocessor Directives in C

Author:Ashwini Patil

Keywords:Video tutorial, #include, #define, #if, #undef, #else, #endif


Visual Cue
Narration
Display Slide 1 Welcome to the Spoken Tutorial on Preprocessor Directives in C
Display Slide 2 In this tutorial, we will learn about
  • Preprocessor Directives
  • #include
  • #define
  • #if
  • #else
  • #undefwith the help of examples.
Display Slide 3

System Requirements

For this tutorial I am using
  • Ubuntu Operating System version 11.10
  • gcc  Compiler version 4.6.1 on Ubuntu
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

  • The preprocessor Directivesmodifies a source file before compilation.
  • All preprocessor Directivesbegins with a #symbol.
  • It instructsCcompiler to do pre-processing before compilation.
  • It makes the programs easier to read, modify and develop
  • It makes the code portable and helps to customize the code.
Display Slide 7








Syntax:

  1. Preprocessor Directive
There are different types ofPreprocessor Directives.

1) Defining preprocessor directives.

  1. define:This macro defines constant values.

    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:

  1. Preprocessor Directive
Display Slide 8


Syntax:

  1. Preprocessor Directive <header file>
2)Header Files inclusion

       #include <stdio.h>:The functions and code ofstdio.h headeris included in the main program.

Syntax:

  1. Preprocessor Directive <header file>
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.

  1. define radius 15
  1. define pi 3.14
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:
  1. ifdef radius

{

close the brackets after the lastprintf statement

}

I will add a few lines here.

In main function type:

  1. ifdef radius

(opening bracket) {

close the brackets after the lastprintf statement

}

type:
  1. else

{

   printf(“Radius of the circle is not defined”);

}

  1. endif
Now after the closing bracket type:
  1. else

(opening bracket){

   printf(“Radius of the circle is not defined”);

(closing bracket)}

  1. endif
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


  1. undef radius
  1. define radius 20
Type:


  1. undef radius
  1. define radius 20
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,
  • Preprocessor Directives
  • #include
  • #define
  • #if
  • #else
  • #undef
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.

Contributors and Content Editors

Ashwini