CHAPTER 5 - Preprocessor Directives
CHAPTER 5 - Preprocessor Directives
PREPROCESSOR
DIRECTIVES
Outline
5.1 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>
#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>
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”);
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”);
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”);
#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;
}