Industrial Training




C Tips Tricks-4



How to find the size of a text file without traversing it character by character?

Ans. The following program shows how to find the size of text file without traversing character by character basis:

#include "stdio.h"

main( )

{

char file[ 67 ] ;

long int size ;

FILE *fp ;

printf ( "\nEnter a filename: " ) ;

gets ( file ) ;

fp = fopen ( file, "r" ) ;

if ( fp == NULL )

{

puts ( "Unable to Open" ) ;

exit( ) ;

}

fseek ( fp, 0L, SEEK_END ) ;

size = ftell ( fp ) ;

printf ( "The Size of file \"%s\" is %ld", file, size ) ;

}

Use #pragma startup and #pragma exit preprocessor directives to specify a function which executes upon program startup before main( ) is called or program exits ( just before the program terminates). The syntax is as follows:

#pragma startup func_name <priority>

#pragma exit func_name <priority>

The declaration of the function should be as follows:

void func ( void ) ;

priority variable is optional. It is an integer in the range 64 to 255. ( 0-63 are used by C libraries). Highest priority is 0. The functions with higher priorities are called first at startup and last at exit. If priority is not specified, it defaults to 100.

We can use #pragma warn directive to check ‘Display Warning’ settings in the Option | Compiler | Messages dialog boxes. For example, if your source code contain the directives

#pragma warn +xxx

#pragma warn -yyy

#pragma warn .zzz

the xxx warning will be turned on, the yyy warning will be turned off, and the zzz warning will be restored to value it had when compilation of the file began. For example:

#pragma warn -rvl

will disable the warning ‘Function should return a value’. For the complete list of three letter abbreviation you can refer to online help.



The # Preprocessor

The # symbol can be used in front of a normal macro argument to convert the actual argument to string.

#define PRINT(X) printf ( #X " = %s", X ) ;

void main( )

{

char *name = "Jeff Prosise" ;

PRINT ( name )

}

The output of the above program is

name = "Jeff Prosise"



What you cannot do with 'register' storage class?

main( )

{

register int a, *p1, *p2 ;

int b ;

clrscr( ) ;

p1 = &a ;

p2 = &b ;

printf ( "%u %u", p1, p2 ) ;

getch( ) ;

}

Here, a is an integer variable, p1 and p2 are pointers. These all variables are of register storage class. This program would flash a compiler error because we are storing address of a register variable. Register variables don't have address. However, it's all right to store an address in a register variable. In other words, a register variable can be a pointer, but it can't be pointed to.

Cryptic Code!

char*s="char*s=%c%s%c;main( ) { printf ( s, 34, s, 34 ) ; }";

main( ) { clrscr( ) ;printf ( s, 34, s, 34 ) ; }

This is a classic example of self-reproducing program. Program that produces its own source code as its output.

(ASCII of double-quote character has the value 34.)

The first s in printf( ) prints the string 'char *s =' , then due to %c within s it prints double-quote character and then %s within the string prints the whole string then due to %c it prints doube-quote(34) for the second time and lastly remaining part of string after %c gets printed out.



Hi I am Pluto.