Wednesday, 19 March 2014

C Program for Count chars, spaces, tabs and newlines in a file

/* Count chars, spaces, tabs and newlines in a file */
# include "stdio.h"
main( )
{
FILE  *fp ;
char  ch ;
int  nol = 0, not = 0, nob = 0, noc = 0 ;
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
noc++ ;
if ( ch == '  ' )
nob++ ;
if ( ch == '\n' )
nol++ ;
if ( ch == '\t' )
not++ ;
}
fclose ( fp ) ;
printf ( "\nNumber of characters = %d", noc ) ;
printf ( "\nNumber of blanks = %d", nob ) ;
printf ( "\nNumber of tabs = %d", not ) ;
printf ( "\nNumber of lines = %d", nol ) ;
}

C Program for Using pointer to access array elements

/* Using pointer to access array elements */
main( )
{
char  name[ ] = "Klinsman" ;
char  *ptr ;
ptr = name ;  /* store base address of string */
while ( *ptr != `\0' )
{
printf ( "%c", *ptr ) ;
ptr++ ;
}
}

C Program for Memory map of structure elements


/* Memory map of structure elements */
main( )
{
struct book
{ char  name ;
float  price ;
int  pages ;
} ;
struct book  b1 = { 'B', 130.00, 550 } ;
printf ( "\nAddress of name = %u", &b1.name ) ;
printf ( "\nAddress of price = %u", &b1.price ) ;
printf ( "\nAddress of pages = %u", &b1.pages ) ;
}

C Program for Demonstration of call by reference

/* Demonstration of call by reference */
main( )
{
int  i ;
int  marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
for ( i = 0 ; i <= 6 ; i++ )
disp ( &marks[i] ) ;
}
disp ( int  *n )
{
printf ( "%d ", *n ) ;
}

C Program for Macro expansion

/* Macro expansion */
#define AND &&
#define OR  ||
main( )
{
int  f = 1, x = 4, y = 90 ;
if ( ( f < 5 ) AND ( x <= 20 OR y <= 45 ) )
printf ( "\nYour PC will always work fine..." ) ;
else
printf ( "\nIn front of the maintenance man" ) ;
}