Answers 861
F
int var = 99;
void print_func(void);
int main( void )
{
int var = 77;
printf( “Printing in function with local and global:”);
printf( “\nThe Value of var is %d”, var );
print_func( );
return 0;
}
void print_func( void )
{
printf( “\nPrinting in function only global:”);
printf( “\nThe value of var is %d\n”, var );
}
- There is only one problem with a_sample_function(). Variables can be declared
at the beginning of any block, so the declarations of crtlandstarare fine. The
other variable,ctr2, is not declared at the beginning of a block; it needs to be. The
following is the corrected function within a complete program.
Note: If you’re using a C++ compiler instead of a C compiler, the listing with a
bug might run and compile. C++ has different rules concerning where variables
can be declared. However, you should still follow the rules for C, even if your
compiler lets you get away with something different.
#include <stdio.h>
void a_sample_function( );
int main( void )
{
a_sample_function();
return 0;
}
void a_sample_function( void )
{
int ctr1;
for ( ctr1 = 0; ctr1 < 25; ctr1++ )
printf( “*” );
puts( “\nThis is a sample function” );
{
char star = ‘*’;
int ctr2; /* fix */
puts( “\nIt has a problem\n” );
49 448201x-APP F 8/13/02 11:22 AM Page 861