Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

55.functions Method2, Include statements, Creating our own header file, Conditional compilation statements

 

Method2:

#include<stdio.h>

float sum(float x,float y) /* defining the function before main() */

{

    return x+y;

}

void main()

{

    clrscr();

    printf("Sum %f",sum(12.50,15.75));

    getch();

}

 

Output:

Sum 28.250000

 

Include statements:

We know that C is a modular programming language.

The total application is not developed as a single file.

An application is developed as multiple files by multiple developers.

These files are inter connected using #include statement.

If the file is saved in the working directory then #inlcude "" is used to include an external file

If the file is saved in the include directory (c:\turboc2\include in case of turboc2) of compiler then #include <> is used to include an external file.

 

 

 

 

Including an external file (working directory)

 

Example:

Here we define functions in one file called "resource.c" and main() function is in another file called "user.c".

We will include "resource.c" to "user.c" using #include statement.

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Execution1:

Enter two numbers:

12.25

12.25

Equals

 

Execution2:

Enter two numbers:

12.25

25.67

Maximum number 25.670000

Minimum number 12.250000

 

Example explained:

Two functions of "resource.c" are included into "user.c" before the main() function. Hence there is no need to write prototypes to including functions.

 

Note: If we write include statement after the main() then it is mandatory to write prototypes to including functions.

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Header files:

It is a file created with the collection of reusable functions of same category.

These files are saved with .h extension.

Generally header files are saved under the include directory of C compiler.

#include <> statement is used to include the functions of any specified header file to the program.

Example:

stdio.h       //provides standard input and output functions

conio.h      //provides console input and output functions

math.h       //provides number of mathematical functions

string.h      //provides number of string manipulation functions

stdlib.h      //provides standard library functions.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Creating our own header file (User defined header file):

We can define out own header file with the commonly used functions.

These functions can be included into any program using #include <> statement.

 

Step1: Create a header file and save it with .h extension under the include directory of C compiler.

 

 

 

 

 

 

 

 

 


]]

Step2: Including the header file into the application.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Step3: Execution

Enter any number: 8

Factorial 40320

 

Conditional compilation statements:

There are some preprocessor conditional statements called conditional compilation statements.

These statements help to select a part of source code from the total code according to the condition and send to the compiler.

These are used to develop different versions of applications for different platforms using a single file.

C language supports #if, #ifdef, #else, #elif, #ifndef and #endif as conditional compilation statements.

Example1:

#include<stdio.h>

#define NICE 1

#ifdef  NICE

void main()

{

   int n;

   clrscr();

   printf("Enter any number:");

   scanf("%d",&n);

   if(n%2==0)

      printf("Even number");

   else

     printf("Odd number");

   getch();

}

#else

void main()

{

   int a,b;

   clrscr();

   printf("Enter two numbers:\n");

   scanf("%d%d",&a,&b);

   if(a==b)

     printf("Equals");

   else if(a>b)

     printf("Biggest number %d",a);

   else

     printf("Biggest number %d",b);

   getch();

}

#endif

 

 

Execution:

Enter any number: 25

Odd number

 

Example explained:

Because macro NICE is defined, the preprocessor sends part1 (#if part) to the compiler.

 

Example2:

Example1:

#include<stdio.h>

#ifdef  NICE

void main()

{

   int n;

   clrscr();

   printf("Enter any number:");

   scanf("%d",&n);

   if(n%2==0)

      printf("Even number");

   else

     printf("Odd number");

   getch();

}

#else

void main()

{

   int a,b;

   clrscr();

   printf("Enter two numbers:\n");

   scanf("%d%d",&a,&b);

   if(a==b)

     printf("Equals");

   else if(a>b)

     printf("Biggest number %d",a);

   else

     printf("Biggest number %d",b);

   getch();

}

#endif

 

Execution:

Enter two numbers:

45

34

Biggest number 45

 

Preprocessor operators:

#, ## are two preprocessor operators used with #define statements.

 

Stringize (#) operator: It keeps the argument in double quotations automatically.

 

 

 

 

 

 

 

 


                                                                                  

 

Output:

Hello world

 

Example explained:

Here we send hello world as argument to macro name. # puts the argument (hello world) in double quotations and replaces by the preprocessor.

 

 

 

 

 

 

 

pasting (##) operator: It concatenates the arguments to the macro

 

 

 

 

 

 

 

 

 

 

 

 


Output:

10

 

Example explained:

We send x, y as arguments to the macro add(x,y) which is concatenated by the preprocessor.

0 comments:

Post a Comment