Skip to main content

Posts

Showing posts with the label Preprocessor

Task performed by preprocessor

Processes preprocessor directives and expands macros. Remove comments and replacing them by a single space.   Divide the program into stream of tokens.  Joins any lines that end with a backslash character into a single line.  Replaces escape sequences by their equivalent internal representation. Format of Preprocessor Directive  Preprocessor directives are special instructions for the preprocessor.  They begin with a ‘#’ which must be the first non-space character on the line.  They do not end with semicolon.  Except for some #pragma directives, preprocessor directives can appear anywhere in program. Also Read Preprocessor Syntax Macros vs Functions Predefined Macro program C program to find largest of two numbers using Structure Define a macro EQUALSTR which accepts two strings and compare equality of both string

Macros Vs Functions

Macros Vs Functions Macros It contains few lines of code A macro is preprocessed A macro is expanded in the  code The program size increases Execution of macros is  faster since code is replaced No type checking is done for macros Usually used for small code  which appear many times Functions It contains several lines of code A function is compiled Function code is not inserted in  the code The program size does not  increase Functions execute slower Strict type checking is done for  functions. Usually used for large code that  needs to be executed many times. Conditional Compilation Directives These directives allow specific parts of the program to be included or  discarded based on certain condition. It causes the preprocessor to  conditionally suppress the compilation of portions of source code.  #if Syntax: #if constant-expression  It checks whether the constant expression is true(non-zero).  #ifdef  Syntax: #ifdef ide...

Preprocessor Syntax

Preprocessor Syntax #directive_name arguments  Example : #include <stdio.h> File Inclusion Directive The file inclusion directive is #include.  The #include directive instructs compiler to include/insert contents of the specified file at that position.  Syntax:  #include <filename>  OR   #include "filename“ First format searches for the file in standard directories only.  Second format first searches for the file in the current directory. If not found search continues in the standard directories. An included file can include other files. Example #include <stdio.h> #include<string.h>  /*group.h*/  #include<stdio.h>  #include<math.h>  #include “myfile.c”  /*mainprog.c*/ #include “group.h” main()  {  ----  ----  } Macro Substitution Directive #define is a macro substitution directive It defines symbol which represents a value   Macro name is substituted by code...

Predifined Macro Program

Predefined Macros Program #include<stdio.h>  #ifdef __STDC__  #define CONFORM “conforms”  #else  #define CONFORM “does not conform”  #endif  int main()  {  printf(“Line %d of file %s has been executed\n”,__LINE__,__FILE__); printf(“This file was compiled at %s on %s\n”,__TIME__,__DATE__); printf(“This program %s to ANSI standards\n”,CONFORM); }  Output:  Line 9 of file p1.c has been executed  This file was compiled at 13:36:39 on Mar 2 2021   This program conforms to ANSI standards Other Directives  #error   This directive causes the preprocessor to generate an error message and causes compilation to fail.  Syntax:  #error error_string  Example:  #if SIZE %2==0  #error SIZE should not be an even number  #endif  If error occurred program compilation will not complete and therefore no executable program will be created. Other Directives continued …   #pragma...

Define a macro EQUALSTR which accepts two strings and compare equality of both string

Define a macro EQUALSTR which accepts two strings and compare equality of both string #include<stdio.h>  #include<string.h>  #define EQUALSTR(str1,str2) strcmp(str1,str2)?0:1  void main()  {  char str1[10],str2[10];  printf("\n Enter the two strings: ");  gets(str1);  gets(str2);  if(EQUALSTR(str1,str2))  printf("\n Strings are equal");  else  printf("\n Strings are not equal");  } Also Read C program to find largest of two numbers using Structure Predefined Macro program Macros vs Functions Preprocessor Syntax Task Performed by Preprocessor

C Program to find largest of two numbers using a macro

C Program to find largest of two numbers using a macro #include<stdio.h>  #define large(a,b) ((a)>(b) ? (a):(b))  main()  {  int x,y;  printf(" \nEnter two numbers :");  scanf("%d%d",&x,&y);  printf("largest=%d",large(x,y));  }  Also Read Predefined Macro program Macros vs Functions Preprocessor Syntax Task Performed by Preprocessor Define a macro EQUALSTR which accepts two strings and compare equality of both string