Introduction to c



Download 0.91 Mb.
Page8/13
Date28.05.2018
Size0.91 Mb.
#51782
1   ...   5   6   7   8   9   10   11   12   13

union tag


{

type member-1;

type member-2;

…………..


…………..

type member-n;



};

The major difference between structures and unions is in terms storage. In structures each member has its own storage location, whereas all the members of a union use the same location.



e.g.:- union data

{

int x;


float y;

char ch;


};

This definition does not declare any variables. You may declare a variable either by placing the variable’s name at the end of the definition or by using a separate declaration statement.



e.g.:- union data item1;

Although a union may contain many members of different types, it can handle only one member at a time. When a union is declared, the compiler automatically creates (allocates) a variable large enough to hold the largest type in the union.

To access the union element, use the dot (.) or arrow () operators as for structures. If you are operating on the union variable directly, use the dot operator. If the union variable is accessed through a pointer, use the arrow operator.

e.g.: (1) item1.y = 56.457;

e.g.: (2) function(union data *ptr)

{

ptr->x =10’

printf(“Ptrx = %d”, ptrx);

}

The program can use only one member at a time because only one location is allocated for a union variable. During accessing, the program can access only the member whose value is currently stored. When a different member is assigned a new value, the new value supersedes the previous member’s value.



SYMBOLIC CONSTANTS / MACROS

Macro substitution is a process where an identifier in a program is replaced by a predefined string. Macro substitution is accomplished by using #define pre-processor statement.



syntax : # define identifier string(value)

e.g.: #define MAX 100

All #define directives must be placed before the function main (). It is customary but not mandatory to use all uppercase letter for macro names used in #define statement, only to give a visual idea that they are not ordinary variables. No semicolons is used after the last argument, since #define is a directive and not a ‘C’ statement. The #define statement can be used for character (enclosed within single quotes) and string constants (enclosed within double quotes). #define can be also used for defining aliases for ‘C’ keywords. One #define directive can be used to define only one symbolic constant/macro. So if you want to define more than one symbolic constant/macro use multiple #define statements and place them before main (). When the program is compiled the symbolic constant/macro is replaced by its value. This is called compile-time substitution. Symbolic constants enclosed in double quotes are not substituted. In a #define directive statement for a string constant, if the string is too long to fit in a single line, it can be continued on the next line by typing an escape character (\) at the end of first line. A #define statement can not contain a newline character.



e.g.: (1)

# include

#define FALSE 0

# define PI 3.1415926

# define CAPITAL “DELHI”

main()


{

printf(“%d”, FALSE);

printf(“%f”, PI);

printf(“CAPITAL = %s”, CAPITAL);



}

e.g.: (2)

#define EQUALS ==

#define AND &&

#define OR ||

#define NOT_EQUAL !=

#define START main(){

#define END }

#define MOD %

#define INCREMENT ++
START

………….


If(total EQUALS 100 AND eng NOT_EQUAL 25)

INCREMENT count;

………….

………..


END

MACROS WITH ARGUMENTS

syntax : # define identifier(arguments) string

The arguments are formal arguments like in the formal arguments of in a function header.



e.g.:- # define CUBE(x) ((x)*(x)*(x))

The above macro can be called as CUBE(5),



printf("%d", CUBE(5) );

e.g.s:-

# define MAX(a,b) (((a) > (b)) ? (a) : (b))

# define MIN(a,b) (((a) < (b)) ? (a) : (b))

# define ABS(x) (((x) > 0) ? (x) : (-(x)))

# define ISUPPER (ch) ((ch)>='A' && (ch)<='Z') ? 1 : 0

NESING OF MACROS

# define M 5

# define N M+1

# define SQ ((x)*(x))

# define CUBE(x) (SQ(x) * (x))

ENUMERATIONS

syntax : enum { 1> [ = ] , 2> [ = ], .... } ;

Defines a set of constants of type int. <identifier1> is the name of a constant that can optionally be assigned the value of <const>. If <const> is missing, then a value is assumed to be the value of the previous constant in the list + 1. If this is the first constant in the list, the default value is 0.



e.g. :

enum {LASTMODE=-1, BW40=0, C40, BW80, C80, MONO=7};

enum {ZERO, ONE, TWO, THREE, FOUR, FIVE};

Sample Program :

# include

main()

{

int ch;


enum {ADD=1, MODIFY, DELETE, LIST, EXIT};

while(1)


{

clrscr();

printf(" 1. ADD RECORDS ");

printf(" 2. MODIFY RECORDS ");

printf(" 3. DELETE RECORDS ");

printf(" 4. LIST RECORDS");

printf(" EXIT PROGRAM ");

printf("Enter Your Choice : "):

scanf("%d", &ch);

switch(ch)



{

case ADD :

statements 1;

case MODIFY :

statements 2;

case DELETE :

statements 3;

case LIST :

statements 4;

case EXIT :

exit(1) ;

}

}

}

SCREEN HANDLING IN ‘C’

The four most common adapters available on IBM PC-compatibles are the monochrome Display Adapter(MDA), the Color Graphics Adapter(CGA), the Enhanced Grahics Adapter(EGA) and the Visual Graphics Array(VGA).

The Monochrome Adapter can work only with text in 80x25 format. ie., it can only display 25 lines of text, 80 columns per line. The CGA and others have several modes of operation including 40 and 80 column text as well as graphics operations.

Mode Type Dimensions Adapters

0 Text, B/W 40x25 CGA, EGA

1 Text, 16 colors 40x25 CGA, EGA

2 Text, B/W 80x25 CGA, EGA

3 Text, 16 colors 80x25 CGA, EGA

4 Graphics, 4 colors 320x200 CGA, EGA

5 Graphics, 4 gray tones 320x200 CGA, EGA

6 Graphics, B/W 640x200 CGA, EGA



7 Text, B/W 80x25 CGA, EGA

If you have a CGA/EGA/VGA adapter, the default display is in text mode with black background and lightgray foreground. TURBO C provides facilities to change the background and foreground colors with the following functions, where the prototypes are in the conio.h header file.

The function textbackground() selects new text background color.

prototype : void textbackground(int newcolor);

The function textcolor() selects new foreground character color in the text mode.



prototype : void textcolor(int newcolor);

The enumerations type COLORS defined in the header file conio.h of TURBO C can be used for standard CGA/EGA/VGA video colors/attributes and have the following values BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE. You can use the integers 0 through 15 to represent any of the above colors. Only the first seven colors (0 through 6) are allowed for text background.

In order to display text on the screen using the new background and foreground attributes, use the function cprintf() rather than the printf() function. This is because the printf() ignores the current color settings.

Adding 128 or BLINK to the foreground color makes the displayed characters blink in the text mode.



e.g.:

# include

# include

main()


{

textbackground(BLUE);

textcolor(YELLOW+BLINK);

cprintf(“%s”,name);

getch();

}

The characters displayed on the screen are held in the RAM in portion reserved for Video Adapters. The base address of video memory for the Monochrome adapters is B0000000H and for the CGA and EGA adapters the base is at B8000000H. Each character displayed on the screen requires two bytes of video memory. The first byte holds the actual character and the second holds its screen attribute. To create lighting fast displays, programmer can access the video RAM.

Reading and writing the Video RAM requires the use of far pointers. For applications to operate correctly for each adapter, they need to know which video adapter is used in the system. The easiest way to do this is to use ROM-BIOS interrupt 16, function 15 which returns the current video mode. The following function finds out which video mode is being used currently, and returns a pointer to the video memory accordingly.

function viddetect to find out the current video mode:-

char far *viddetect()



{

_AH=15;


geninterrupt(16);

if( (_AL!=2) && (_AL!=3) && (_AL!=7) )



{

printf(“Video must be in 80 column text mode”);

exit(1);

}

if(_AL==7)

return (char far *) 0xB0000000;

else


return (char far *) 0xB8000000;

}

In text mode, their row and column numbers references individual character locations on the screen. There are 25 rows from 0 to 24 and 80 columns from 0 to 79. The upper left corner of the screen is location 0,0 in Video RAM and 1,1 on the screen.

The same can be achieved by directly accessing the Video Ram using the following formula:

scr_pos = vid_mem + (160 * l ) + (2 * c);

Where l and c are the line and column numbers respectively.

GRAPHICS IN ‘C’


Before you can use graphics functions you have to place the computer into appropriate video mode. TURBO C provides two functions detectgraph() and initgraph() to check the allowed mode and to initialise your computer to a required mode. They are prototyped in GRAPHICS.H as

void far detectgraph(int *graphicsdriver, int *graphicsmode);

void far inittgraph(int *graphicsdriver, int *graphicsmode, char *pathtodriver);

The detectgraph() determines the presence of graphics hardware in the system and choose the highest resolusion for the adapter. The initgraph() function actually initializes the graphics system by loading a graphics driver from disk.

With detectgraph(), *graphicsdriver gets the value of the graphics driver. If no graphics hardware is detected, *graphicsdriver is set to –2. *graphicsmode specifies the graphics mode. With initgraph(), *graphicsdriver specifies the graphics driver to be used and *grphicsmode specifies the graphics mode. *pathtodriver specifies the directory path where initgraph() will look for the graphics driver.



Download 0.91 Mb.

Share with your friends:
1   ...   5   6   7   8   9   10   11   12   13




The database is protected by copyright ©ininet.org 2024
send message

    Main page