0% found this document useful (0 votes)
21 views25 pages

CHAPTER 5 - Preprocessor Directives

Uploaded by

Adlin Danish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views25 pages

CHAPTER 5 - Preprocessor Directives

Uploaded by

Adlin Danish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CHAPTER 5

PREPROCESSOR
DIRECTIVES
Outline
5.1 File Inclusion

5.2 Macro Definition


What is Preprocessor Directive

Instructions, processed before


the code is compiled.
Preprocessor Directive

File Inclusion Macro Definition


#include
one of the most common preprocessor
directives used C.
when file is included, the contents of the
include file are inserted at the current location
of source file.
File Inclusion

General format:
#include <header_file>
To include the file at the beginning of the
program
#include <stdio.h>
#include <math.h>
#include “string.h”
#include <math.h>

math.h is a header file which is commonly


used for mathematical operations.
Some functions of this header file uses
floating point numbers.
The functions which accepts angle are
accepted in terms of radians.
#include <math.h>…cont
Some of the standard member functions of
math.h header files are:

Function Name Description

pow(x,y) Returns power of x raise to y. i.e. xy

sqrt Returns square root of a number.


#include <math.h>…cont
Example program

#include <stdio.h>
#include <math.h>

void main()
{
printf("\n\t Log of 10 : %f",log(10));
printf("\n\n\t Square Root of 16 : %f",sqrt(16));
printf("\n\n\t Square of 4 : %f",pow(4,2));
printf("\n\n\t Sine of 10 : %f",sin(10));
}
#include <string.h>
'string.h' is a header file which includes the
declarations, functions, constants of string
handling utilities.
These string functions are widely used
today by many programmers to deal with
string operations.
Example of String
Operations on Strings(1)
➢ Length (number of characters in the string).
➢ Concatenation (adding two and more strings)
➢ Comparing two strings.
➢ Substring (Extract substring from a given string)
➢ Copy(copies one string over another)

Include string.h library header file in the program

# include <string.h>
Operations on Strings(2)
strlen()
Counts and returns the number of characters in a string.
The length does not include a null character.
/*To find the length of the string using strlen() function*/
#include <stdio.h>
#include <string.h>

void main()
{
char name[100]; Syntax:
int length;
printf("Enter the string: "); n=strlen(string);
gets(name);
length = strlen(name);
printf("\nNumber of characters %s in the string is = %d “,
name,length);
}
Operations on Strings(3)
strcpy()
Copies 1 string over another or assigns the contents of string2
to string1

strcpy(string1,string2) ;

strcpy(Name,”Zareen”);

Zareen is assigned to the string called name.

Name = “Zareen” Wrong!!!!


Operations on Strings(4)
strcat()
Joins 2 strings together. Concatenation: the characters of one
string to the end of other string

strcat(string1,string2) ;

strcpy(string1,”Yatie”);
strcpy(string2,”Ghadzi”);
printf(“%s”,strcat(string1,string2);

Output:???? YatieGhadzi
Operations on Strings(4)
Operations on Strings(5)
strcmp()
Compare 2 strings. Function returns a zero if 2 strings are
equal, or a non zero number if the strings are not the same.

strcmp(string1,string2) ;

strcmp(“GMI”,”GMI”);

return zero because 2 strings are equal

if(string1==string2) Wrong!!!!
Operations on Strings(5)
#include <stdio.h>
#include <string.h>

int main()
{
char s1[20]="BeginnersBook";
char s2[20]="BeginnersBook.COM";
if (strcmp(s1,s2)==0)
{
printf("string 1 and string 2 are equal\n");
}else
{
printf("string 1 and 2 are different\n");
}
return 0;
}
Operations on Strings(6)
strcmpi()
Compare 2 strings but not case sensitive. Function returns a
zero if 2 strings are equal, or a non zero number if the strings
are not the same.

strcmpi(string1,string2) ;

strcmpi(“Gmi”,”GMI”);

return zero because 2 strings are equal


#include <stdio.h>
#include <string.h>
int main()
{
char s1[20]="GMI";
char s2[20]="0123";
if (strcmpi(s1, s2)==0)
{
printf("string 1 and string 2 are equal\n");
}else
{
printf("string 1 and 2 are different\n");
}
return 0;
}
Macro Definition
usually associated with word processing
and spreadsheet programs.
save your time in carrying out your tasks
when the preprocessor encounters a
macro name, it replaces the macro name
with another string that previously defined.
Macro Definition
General format
#define identifier replacement_string

#define PI 3.14159
#define AREA width * length
#define FAHRENHEIT celcius * 9/5 + 32
RULES APPLY TO CREATING
MACROS
Macro name cannot contain spaces
Macro name is usually differentiated with
variables by using all-upper case letters.
Macro definition should not be terminated by a
semicolon
Macro cannot be used inside quotation marks
Backslash character(\) can be used at the end of
each line to extend the macro definition to more
than one line
Macros Example
//c program to add two numbers using macros
#include <stdio.h>
//define macro to find sum of two numbers
#define SUM(x,y)(x+y)

int main()
{
int num1,num2;
//input two numbers from user
printf("Enter any first numbers:");
scanf("%d",&num1);
printf("Enter any second numbers:");
scanf("%d",&num2);
//calculate and print sum using macro
printf("SUM(%d,%d=%d\n",num1,num2,SUM(num1,num2));
return 0;
}

You might also like