Preprocessor Directives 367
11.13 THE PREDEFINED MACROS IN ANSI AND TURBO-C
a) ANS I C Predefined M acros The lists of pre-defined macros according to ANSI standard are as
per the Table 11.1. There are five pre-defined macros and are always available to the programmer for
use. They can not be undefined. Every macro name is defined with two underscores as prefix and
suffix. These macros are useful for finding system information such as date, time, and file name and
line number. A program is illustrated for testing these macros.
Table 11.5 Predefined Macros in ANSI -C
Predefined Macros
Function
__
Date
__
Displays system date in string format
__
Time
__
Displays system time in string format
__
Line
__
Displays line number as an integer
__
File
__
Displays current file name in string format
__
Stdc
__
In ANSI 'C' the value returned w ill be non-zero.
11.15 Write a program to use pre-defined macros o f ANSI C.
# include <stdio.h>
# include cconio. h>
# include <stdde£.h>
void main()
{
clrscr();
printf (" n D A T E : % $ " ,_ p A T E _ J ;
printf ( nT lM E : % s " ,_ T I M E _ ) ;
printf ( hFTLE N A M E : % s",_JF IL £_J;
printf (" n U N E N O . : % d " ^ _ U N E _ J ;
p rin tfCn_S T D C _ :% x " ,_ S T D C _ > ;
1
OUTPUT
DATE : Dec 05 2001
TIME : 21:07:39
FILE N A M E : PRE_MA~1.C
LINE NO. : 10
_STDC_ : 1
Explanation In the above program five macros are used in theprintf () statements. On its execution
the output of the program is displayed as shown- above. The program displays system date, time,
program file name and total lines in the program. The STDC indicates whether the version of C
compiler follows ANSI 'C' standard. Here, the result is one. Hence, the compiler follows ANSI C
standards-. The programmer should set ANSI keyword on option by selecting option menu of the
editor - compiler-source- ANSI keywords only on.
b) Turbo C Predefined Macros The pre-defined macros in Turbo c are listed in the table. The
programmer can use the macro
__
TURBOC
__
to find the version of Turbo. By setting option of code
368 Programming and Data Structures
generation in option menu one can test next two macros. For more information the user is advised to
view options in option menu.
Table 11.6 Predefined Macros in Turbo -C
Predefined Macros
Function
__
Turbo-C
___
Displays current turboc version
__
Pascal
__
1 if "Calling convention...Pascal option is chosen, otherwise
undefined.
__
Cdecl
__
1 if "Calling convention...C// option is chosen, otherwise
undefined.
__
MSDOS
__
The integer constant 1.
c) Mem ory M o del M acros The following six macros are defined based on the memory models
chosen by the user. -There are six memory models and only one is defined which is currently in use and
remaining all are undefined. The program is illustrated below to find the memory model. To change
the memory model user is advised to select option menu->co<inpiler -> model.
Table 11.7 Memory Model Predefined Macros
__
Tiny
__
__
Small
__
__
Medium
__
__
Compact
__
__
Large
__
__
Huge
__
11.16 Write a program to display the name of memory model that is currently in use.
# include <stdio.h>
# include <conio*h>
void main ()
{
clrscrO ;
Xifdef
__
T I N Y _
printf (" n T IN Y % d ",_ T IN Y _)/
#else
Mfdef
__
S M A LL
__
printf (" n S M A L L % d ",_ S M A L L _J;
Melse
M f d e f _ M E D I U M _
printf (" n M E D IU M % d ",_ M E D I U M _ ) ;
false
#ifdef
__
C O M P A C T _
printf (" n C O M P A C T % d ",
__
C O M P A C T _ J ;
false
#ifdef__LARGE
__
printf ("X n L A R G E % d",
__
L A R G E
false
printf (" n H U G E % d " ,_ H U G E _ ) ;
fandif
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.172.130