C programming in easy steps by McGrath Mike 1956-

C programming in easy steps by McGrath Mike 1956-

Author:McGrath, Mike, 1956-
Language: eng
Format: epub
Tags: C (Computer program language)
Publisher: Southam : In Easy Steps
Published: 2009-03-05T16:00:00+00:00


Don't forget

The program file and custom header file must be located in the same directory but are compiled with the usual command - the compiler reads the header file automatically because of the #include directive.

Hot tip

Notice the use of the %1s format specifier in this example - to read the next character entered by the user.

c

menu.c

Hot tip

An example demonstrating static variables can be found on page 28.

c

action.c

Restricting accessibility

The static keyword can be used to restrict the accessibility of functions to the file in which they are created, in exacdy the same way that static yariables ha\ r e restricted accessibility.

This is recommended practice in larger programs that are spread oyer multiple ".c" files to safeguard against accidental mis-use of functions. For instance, the squareO and multiplyO functions cant be direcdy called from the mainQ function in this example:

o

o e

o

e e

Begin a new program with a preprocessor instruction to

include the standard input/output library functions #include <stdio.h>

Declare a custom function prototype with no arguments void menuQ ;

Add a main function to call the custom function then

return a zero integer as required by the declaration int mainQ

{

menu() ; return 0

}

After the main function block, define the custom function

to pass a menu option as an argument to another function void menu()

int option ;

printf( "\n\tWhat would you like to do?" ) ; printf( "\n\t1. Square a number" ) ; printf( "\n\t2. Multiply two numbers" ) ; printf( "\n\t3. Exit\n" ) ; scanf( "%d" , &option ) ; action( option ) ;

}

Now begin a second program file with a preprocessor to include the standard input/output functions #include <stdio.h>

Next define two simple static functions static square( int a ) { return (a * a) ; } static multiply( int a, int b ){ return a * b ; }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.