You must use paraentheses to evaluate the expression correctly as . (member operator) has higher
precedence than * (indirection operator).
In Structure declaration only members of the structure are declared. No space is allocated
to member.
In Structure variable declaration, we declare a variable of the structure and that variable is allocated
memory.
All members in a structure are allocated memory whereas only the largest member of a union
is allocated memory.
The value is assigned to the item instead of the default value. If nothing is specified
enum starts assigning from zero onwards.
In case of angle brakets (<>) C searches for the file only in standard include directory.
In caes of double quotes C searches for header file in the current directory first and if it
is not found then searches for the file in the standard include directory.
A macro will improve performance because it will not have the overheads of calling a function.
Chapter 1 - Introduction To C
I. Fill in the blanks.
Chapter 2 - First C Program
I. Fill in the blanks
II. Write programs.
main()
{
printf(“P.Srikanth”);
printf(“\nG2, Neeharika Enclave, Vizag-16”);
}
main()
{
printf(“P.Srikanth”);
printf(“\nG2 \tNeeharika Enclave\tVizag-16”);
}
main()
{
printf(“\a \a \a \a\a Bell is over”);
}
Chapter 3 - Language Elements
I. Fill in the blanks.
II. Identify invalid identifiers.
III. Write program.
main()
{
int n1,n2;
/* Accept two numbers */
printf("Enter two numbers :");
scanf("%d%d",&n1,&n2);
printf("Octal of %d = %o Hex of %d = %x",n1,n1,n2,n2);
}
main()
{
float amt, disper,net;
/* accept amount and discount percentage */
printf("Enter amount and discount percentage : ");
scanf("%f%f", &amt,&disper);
net = amt - ( amt * disper / 100 );
printf("Net amount : %f ", net);
}
main()
{
int n1,n2;
/* accept two numbers */
printf("Enter two numbers ");
scanf("%d%d", &n1, &n2);
/* print numbers in the reverse order */
printf("Numbers in reverse order : %d %d ", n2, n1);
}
Chapter 4 - Operators
I. Fill in the blanks
II. Write Programs.
Sum, difference, product, quotient, and remainder.
main()
{
int n1,n2;
/* accept two numbers */
printf("Enter two numbers ");
scanf("%d%d", &n1, &n2);
/* print sum, differnce, product and quotient and remainder */
printf("\nSum = %d ", n1 + n2);
printf("\nDifference = %d ", n1 - n2);
printf("\nProduct = %d ", n1 * n2);
printf("\nQuotient = %d ", n1 / n2);
printf("\nRemainder = %d ", n1 % n2);
}
main()
{
int n;
/* accept a number */
printf("Enter a number ");
scanf("%d",&n);
printf("\nSqare of %d = %d ", n , n * n );
}
main()
{
float pamt, rate, interest;
int ny;
printf("Enter principal amount : ");
scanf("%f",&pamt);
printf("Enter interest rate : ");
scanf("%f",&rate);
printf("Enter number of years : ");
scanf("%d",&ny);
interest = pamt * rate * ny / 100;
printf("Total Simple Interest = %f", interest );
}
main()
{
int a,b;
printf("Enter value for a and b : ");
scanf("%d%d",&a,&b);
printf(" Result of = %d " , a * a + b * b);
}
Chapter 5 - Control Structure
Write Programs
main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
if ( n % 2 == 0 )
printf(" %d is even number");
else
printf(" %d is odd number");
}
/* using conditional expression */
main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf(" %d is %s ", n, n % 2 == 0 ? "Even" : "Odd");
}
main()
{
int marks,pm,subject;
printf("Enter subject code [1-4]: ");
scanf("%d",&subject);
printf("Enter marks : ");
scanf("%d",&marks);
switch(subject)
{
case 1:
pm = 40; break;
case 2:
pm = 50; break;
case 3:
pm = 45; break;
case 4:
pm = 55; break;
}
if (marks >= pm )
printf(" Passed");
else
printf(" Failed");
}
main()
{
int n1,n2;
printf("Enter two numbers : ");
scanf("%d%d",&n1,&n2);
if ( n1 == n2 )
printf("Both the numbers are equal");
else
if ( n1 > n2 )
printf("First number is bigger");
else
printf("Second number is bigger");
}
main()
{
int n1,n2,n3,big;
printf("Enter three numbers : ");
scanf("%d%d%d",&n1,&n2,&n3);
big = n1 > n2 ? n1 : n2;
big = n3 > big ? n3: big;
printf(" The biggest of %d %d %d is %d ", n1,n2,n3,big);
}
Chapter 6 - Looping Structures
I. Fill in the blanks
II.Write programs
main()
{
int i;
for ( i = 100 ; i >= 0 ; i --)
printf("%d\n", i);
}
main()
{
int i,n,sum=0;
printf("Enter a number: ");
scanf("%d", &n);
for ( i = 1 ; i <= n ; i ++)
sum += i;
printf(" Sum of 1 to %d = %d ", n,sum);
}
main()
{
int i;
for ( i = 100 ; i <= 200 ; i += 2)
printf("%d\n",i);
}
main()
{
int i=0,n;
float sum=0;
while (1) /* always true loop */
{
printf("Enter a number [0 to stop] : ");
scanf("%d", &n);
if ( n == 0 )
break; /* terminate loop */
sum += n;
i++;
}
printf(" Sum = %f Average = %f ",sum, sum / i);
}
main()
{
int i,n,big,small;
big = -32768; /* set big to smallest possible number */
small = 32767; /* set small to biggest possible number */
for ( i=1; i <= 10 ; i++)
{
printf("Enter a number : ");
scanf("%d", &n);
if(n > big )
big = n;
if ( n < small )
small = n;
}
printf(" The smallest = %d and biggest = %d ", small, big);
}
Sample output:
7 * 1 = 7
7 * 2 = 14
...
7 * 20 = 140
main()
{
int i;
for ( i = 1; i <= 20 ; i++)
printf(" 7 * %d = %d\n", i, 7 * i);
}
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
main()
{
int i,j;
for ( i = 1; i <= 5 ; i++)
{
printf("\n");
for ( j = 1 ; j <= 5 ; j ++)
printf("%5d", j);
}
}
Chapter 7 - Character Handling
I. Fill in the blanks.
II. Write programs.
#include <ctype.h>
#include <stdio.h>
main()
{
char ch;
int i,nu,nd,nl;
nu = nd = nl = 0;
for ( i = 1 ; i <= 10 ; i ++)
{
ch = getchar();
if (isupper(ch))
nu ++;
else
if (islower(ch))
nl ++;
else
if (isdigit(ch))
nd ++;
}
}
#include <ctype.h>
#include <stdio.h>
main()
{
char ch;
printf("Enter a character : ");
ch = getchar();
if ( isupper(ch))
putchar( tolower(ch));
else
if (islower(ch))
putchar(toupper(ch));
}
#include <ctype.h>
#include <stdio.h>
main()
{
char ch;
while (1)
{
printf("Enter a character : ");
ch = getchar();
fflush(stdin); /* clear keyboard buffer */
if ( ch == '*' )
break;
putch( tolower(ch));
}
}
Chapter 8 - Arrays
Write Programs
main()
{
int ar[20];
int i, sum = 0;
for ( i = 0 ; i < 20 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
sum += ar[i];
}
printf("Entered Numbers \n");
for ( i = 0 ; i < 20 ; i++)
{
printf("%d\n", ar[i]);
}
printf("Sum = %d Average = %d ", sum, sum / 20);
}
main()
{
int ar[10];
int i, j, temp;
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
/* sort array in descending order */
for ( i = 0 ; i < 9 ; i++)
{
for ( j = i+1; j < 10 ; j ++)
{
if ( ar[j] > ar[i])
{
/* interchange */
temp = ar[j];
ar[j] = ar[i];
ar[i] = temp;
}
} /* end of j loop */
} /* end of i loop */
/* display sorted array */
printf("Entered Numbers \n");
for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
}
main()
{
int ar[20];
int i,avg,sum=0;
for (i = 0 ; i < 20 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
sum += ar[i];
}
avg = sum / 10;
/* Display values that are below average of the array */
for ( i = 0 ; i < 20 ; i++)
{
if ( ar[i] < avg )
printf("%d \n", ar[i]);
}
}
main()
{
int ar[10];
int i,num, pos;
for (i = 0 ; i < 9 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
/* take number and position */
printf("Enter position and number : " );
scanf("%d%d", &pos, &num);
/* push all number from position to right */
for ( i = 9 ; i > pos ; i-- )
ar[i] = ar [i - 1];
/* insert new value at position */
ar[pos] = num;
/* Display array after insertion */
for ( i = 0 ; i < 10 ; i++)
{
printf("%d \n", ar[i]);
}
}
main()
{
int ar[10];
int i,temp;
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
/* interchange first 5 elements with last 5 elements */
for ( i = 0 ; i < 5 ; i-- )
{
temp = ar [i];
ar[i] = ar [9-i];
ar[9-i] = temp;
}
/* Display array after interchange */
for ( i = 0 ; i < 10 ; i++)
{
printf("%d \n", ar[i]);
}
}
Chapter 9 - Multidimensional Array
main()
{
int ar[5][5];
int i,j;
for (i = 0 ; i < 5 ; i++)
{
for( j = 0 ; j < 5 ; j ++)
{
printf("Enter number for [%d][%d] element : ",i,j);
scanf("%d", &ar[i][j]);
}
}
/* display array as a matrix */
for (i = 0 ; i < 5 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 5 ; j ++)
{
printf("%5d",ar[i][j]);
}
}
}
main()
{
int ar[4][4];
int i,j;
for (i = 0 ; i < 4 ; i++)
{
for( j = 0 ; j < 4 ; j ++)
{
if ( i == j )
ar[i][j] = 1;
else
ar[i][j] = 0;
}
}
/* display array as a matrix */
for (i = 0 ; i < 4 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 4 ; j ++)
{
printf("%5d",ar[i][j]);
}
}
}
main()
{
int ar[5][5];
int i,j,num;
for (i = 0 ; i < 5 ; i++)
{
for( j = 0 ; j < 5 ; j ++)
{
printf("Enter number for [%d][%d] element : ",i,j);
scanf("%d", &ar[i][j]);
}
}
printf("Enter a number : ");
scanf("%d", &num);
/* display all elements that are greater than the
given number */
for (i = 0 ; i < 5 ; i++)
{
for( j = 0 ; j < 5 ; j ++)
{
if ( ar[i][j] > num )
printf("%d \n",ar[i][j]);
}
}
}
#include <stdlib.h>
main()
{
int ar[6][6];
int i,j,temp;
for (i = 0 ; i < 6 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 6 ; j ++)
{
ar[i][j] = random(100);
printf("%5d",ar[i][j]);
}
}
/* interchange */
for (i = 0 ; i < 3 ; i++)
{
for( j = 0 ; j < 6 ; j ++)
{
temp = ar[i][j];
ar[i][j] = ar[5-i][j];
ar[5-i][j] = temp;
}
}
printf("\n\n");
for (i = 0 ; i < 6 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 6 ; j ++)
{
printf("%5d",ar[i][j]);
}
} /* end of i loop */
} /* end of main */
Chapter 10 - String Handling
I. Fill in the blanks
II. Write programs
main()
{
char st[20];
int i;
/* accept a string */
printf("Enter a string : ");
gets(st);
/* display it in upper case */
for ( i = 0 ; st[i] != '\0'; i++)
if ( st[i] >= 'a' && st[i] <= 'z' )
putch( st[i] - 32);
else
putch( st[i]);
}
main()
{
char st[20];
int i;
/* accept a string */
printf("Enter a string : ");
gets(st);
/* get length of the string */
for ( i = 0 ; st[i] != '\0'; i++) ;
/* display it in reverse order */
for ( i -- ; i >= 0 ; i --)
putch(st[i]);
}
#include <stdio.h>
main()
{
char st[20];
int i, count = 0;
char ch;
/* accept a string */
printf("Enter a string : ");
gets(st);
/* accept character */
printf("Enter a character : ");
ch = getchar();
/* count */
for ( i = 0 ; st[i] != '\0'; i++)
if ( st[i] >== ch)
count ++;
printf("No. of words : %d ", count);
}
#include <stdio.h>
main()
{
char st[20];
int i,count =0;
/* accept a string */
printf("Enter a string : ");
gets(st);
/* count words */
for ( i = 0 ; st[i] != '\0'; i++)
if ( st[i] == ' ' )
count ++;
printf(" No. of words : %d", count + 1);
}
#include <stdio.h>
main()
{
char st[20];
int i;
char sch,tch;
/* accept a string */
printf("Enter a string : ");
gets(st);
/* accept source and target character */
printf("Enter source character : ");
sch = getchar();
fflush(stdin); /* remove characters from keyboard buffer */
printf("Enter target character : ");
tch = getchar();
/* display it in upper order */
for ( i = 0 ; st[i] != '\0'; i++)
if ( st[i] == sch)
st[i] = tch;
printf(" String after conversion : %s ", st);
}
main()
{
char st[10][20];
int i, hpos=0;
/* take strings */
for (i = 0 ; i < 10 ; i ++)
{
printf("Enter a string :");
gets( st[i]);
if ( strcmp(st[i],st[hpos]) > 0 )
hpos = i;
}
printf(" %s is the biggest string", st[hpos]);
}
Chapter 11 - User-defined Functions
I. Fill in the blanks
II. Write Functions
int nextodd(int num)
{
if ( num % 2 == 0 )
return num + 1;
else
return num + 2;
}
void lowerstring( char st[100])
{
int i;
for ( i = 0 ; st[i] != '\0'; i ++)
putch( tolower( st[i]));
}
int prime(int num)
{
int i;
for ( i = 0 ; i <= num/2 ; i ++)
if ( num % i == 0 ) /* not a prime number */
return 0;
/* if control comes here, it means prime number */
return 1;
}
int nodigits( char st[100])
{
int count = 0, i;
for ( i = 0 ; st[i] != '\0'; i ++)
if ( isdigit (st[i]) )
count ++;
return count;
}
main()
{
printf(" Fact : %d ", fact(4));
}
int fact(int n)
{
if ( n > 1 )
return ( n * fact( n-1));
else
return 1;
}
Chapter 12 - Storage Classes
I. Fill in the blanks
fun()
{
int i;
if ( )
{
int i; /* outer i is hidden here */
}
}
Chapter 13 - Pointers
I. Fill in the blanks
II. Write programs
main()
{
int n1,n2;
printf("Enter two values : ");
scanf("%d%d", &n1,&n2);
printf("Values %d %d ", n1,n2);
printf("\n Addresses %p %p", &n1,&n2);
}
void settosmall(int * n1, int * n2)
{
if ( *n1 < *n2 )
*n2 = *n1;
else
*n1 = * n2;
}
void smallbig(int n1,int n2, int n3,int * big, int * small)
{
/* get biggest */
*big = n2 > n1 ? n2 : n1;
*big = n3 > *big ? n3 : *big;
/* get samllest */
*samll = n2 < n1 ? n2 : n1;
*samll = n3 < *samll ? n3 : *small;
}
main()
{
int n1, n2, n3, small, big;
smallbig(n1,n2,n3, &big, &small);
}
void smallbig(int * ar,int * big, int * small)
{
int i;
*big = *small = ar[0];
for( i = 1 ; i < 10 ; i ++)
{
if ( ar[i] > * big )
*big = ar[i];
if ( ar[i] < *small)
*small = ar[i];
}
void settozeroes(int * ar)
{
int i;
for( i = 0 ; i < 10 ; i ++)
{
ar[i] = 0;
}
}
Chapter 14 - Pointer vs. Array
Write programs
#include <stdlib.h>
main()
{
int a[20];
int * p;
int i;
clrscr();
/* fill array here */
for ( i = 0 ; i < 20 ; i ++)
a[i] = random(100);
/* display using in reverse order using array notaion */
for ( i = 19 ; i >= 0 ; i --)
printf("%d ", a[i]);
printf("\n Pointer \n");
/* display using in reverse order using pointer notaion */
p = &a[19]; /* make pointer pointing to 20th element */
for ( i = 19 ; i >= 0 ; i --, p-- )
printf("%d ", *p);
printf("\n Array pointer \n");
/* display using in reverse order using pointer notaion using array */
for ( i = 19 ; i >= 0 ; i -- )
printf("%d ", *(a+i));
}
void sortarray( int * ar, int nel)
{
int i,j;
/* sort array in descending order */
for ( i = 0 ; i < nel - 1 ; i++)
{
for ( j = i+1; j < nel ; j +=)
{
if ( ar[j] > ar[i])
{
/* interchange */
temp = ar[j];
ar[j] = ar[i];
ar[i] = temp;
}
} /* end of j loop */
} /* end of i loop */
}
main()
{
int ar[10];
int i, j, temp;
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
sortarray(ar,10);
/* display sorted array */
for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
}
void bigarray( int * a1, int * a2, int ** p)
{
int i,sum1,sum2;
sum1 = 0;
for ( i = 0 ; i < 10 ; i++)
{
sum1 += a1[i];
} /* end of i loop */
sum2 = 0;
for ( i = 0 ; i < 10 ; i++)
{
sum2 += a2[i];
} /* end of i loop */
if ( sum1 > sum2 )
*p = a1;
else
*p = a2;
}
main()
{
int a1[]= { 110,20,30,40,50,30,30,30,2,22};
int a2[]= {21,20,30,40,60,29,39,28,24,1};
int *p;
int i;
clrscr();
/* pass address of pointer as pointer is to be modified */
bigarray(a1,a2,&p);
/* display the big array */
for ( i = 0 ; i < 5 ; i ++)
printf("%d ", p[i]);
}
main()
{
int mn;
char * months[] = {"January", "Feburary", "March", "April",
"May", "June", "July", "August",
"September","October", "November", "December"};
printf("Enter month number :");
scanf("%d",&mn);
printf(" Month name : %s", months[ mn - 1]);
}
Chapter 15 - Structures
Write programs
#include <stdio.h>
struct student
{
int no;
char name[30];
int tm;
};
main()
{
struct student s[10], temp;
int i,j;
for ( i = 0 ; i < 10 ; i ++)
{
printf("Enter student number : ");
scanf("%d", & s[i].no);
fflush(stdin);
printf("Enter student name : ");
gets( s[i].name);
printf("Enter total marks : ");
scanf("%d",& s[i].tm);
}
/* sort the array */
for ( i = 0 ; i < 9 ; i ++)
for ( j = i + 1 ; j < 10 ; j ++)
if ( s[i].tm > s[j].tm)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
/* display sorted details */
clrscr();
for ( i = 0 ; i < 10 ; i ++)
{
printf("\n %5d %-20s %5d", s[i].no, s[i].name, s[i].tm);
}
} /* end of main*/
struct time
{
int h,m,s;
};
/* returns > 0 if t1 > t2
< 0 if t1 < t2
0 if t1 == t2
*/
int compare(struct time t1, struct time t2)
{
return (t1.h * 60 * 60 + t1.m * 60 + t1.s) - (t2.h * 60 * 60 + t2.m * 60 + t2.s);
}
main()
{
struct time t[10], st;
int i;
/* accept 10 times */
for ( i = 0 ; i < 10 ; i ++)
{
printf("Enter time in hh:mm:ss format :");
scanf("%d:%d:%d", & t[i].h, &t[i].m, &t[i].s);
}
/* get the smallest time */
st = t[0];
for ( i = 1; i < 10 ; i ++)
{
if ( compare( t[i], st ) < 0 ) /* if time is less than smallest time */
st = t [i];
}
printf("Smallest Time : %d:%d:%d", st.h, st.m, st.s);
}
struct time
{
int h,m,s;
};
struct time nextsecond(struct time t)
{
t.s ++; /* increment seconds */
if ( t.s > 59 )
{
t.s = 0;
t.m ++;
if ( t.m > 59 )
{
t.m = 0;
t.h ++;
if ( t.h > 23 )
t.h = 0;
}
}
return t;
}
(*p).x is to treat p as pointer and member x of the structure pointed by p is taken.
Chapter 16 - Union,typedef and enumeration
Fill in the blanks
typedef char * STRING;
Chapter 17 - Pre-processor commands
Answer the following
#define odd(n) n % 2
Chapter 18 - File handling
I.Fill in the blanks
Write programs.
#include <stdio.h>
#include <ctype.h>
main()
{
char fn[30];
int ch;
FILE * fp;
printf("Enter filename :");
gets(fn);
fp = fopen(fn,"r");
if ( fp == NULL)
{
printf("File not found.");
exit(1);
}
while ( !feof(fp) )
{
ch = fgetc(fp);
if ( isupper(ch))
putchar(ch);
}
fclose(fp);
} /* end of main */
#include <stdio.h>
main()
{
char sfn[30],tfn[30];
int ch;
FILE * sfp, * tfp;
printf("Enter source filename :");
gets(sfn);
printf("Enter target filename :");
gets(tfn);
sfp = fopen(sfn,"r");
if ( sfp == NULL)
{
printf("Source file not found.");
exit(1);
}
tfp = fopen(tfn,"w");
if ( tfp == NULL)
{
printf("Target file not found.");
exit(1);
}
while ( !feof(sfp) )
{
ch = fgetc(sfp);
fputc(ch, tfp);
}
fcloseall(); /* close both the files */
} /* end of main */
#include <stdio.h>
main()
{
char fn[30];
int ch;
FILE * fp;
int lines=0;
printf("Enter filename :");
gets(fn);
fp = fopen(fn,"r");
if ( fp == NULL)
{
printf("File not found.");
exit(1);
}
while ( !feof(fp) )
{
ch = fgetc(fp);
if ( ch == '\n')
lines ++;
}
fclose(fp);
printf("No. of lines : %d", lines);
} /* end of main */
#include <stdio.h>
#include <ctype.h>
main()
{
char sfn[30];
int ch;
FILE * sfp, * tfp;
printf("Enter source filename :");
gets(sfn);
sfp = fopen(sfn,"r");
if ( sfp == NULL)
{
printf("Source file not found.");
exit(1);
}
tfp = fopen("temp.txt","w");
if ( tfp == NULL)
{
printf("Temporary file creation failed.");
exit(1);
}
while ( !feof(sfp) )
{
ch = fgetc(sfp);
fputc( tolower(ch), tfp);
}
fcloseall(); /* close both the files */
/* remove source file */
remove(sfn);
/* rename temporary file to source file name */
rename("temp.txt", sfn);
} /* end of main */
#include <stdio.h>
main()
{
char line[100];
FILE * fp;
fp = fopen("lines.txt","w");
if ( fp == NULL)
{
printf("LINES.TXT could not be created.");
exit(1);
}
while (1)
{
printf("Enter a line and END to stop :");
gets(line);
if ( strcmp(line,"END") == 0 )
break;
fputs(line,fp);
}
fclose(fp); /* close the file */
} /* end of main */
Chapter 19 - Binary files and random access
I. Write programs
#include <stdio.h>
struct student
{
int sno;
char name[30];
int tf, fp;
};
main()
{
struct student s;
FILE * fp;
fp = fopen("student.dat","wb"); /* open in write and binary mode */
while(1)
{
fflush(stdin);
printf("Enter student number [0 to stop]: ");
scanf("%d", & s.sno);
if ( s.sno == 0 )
break;
fflush(stdin);
printf("Enter student name : ");
gets(s.name);
printf("Enter total fee : ");
scanf("%d", & s.tf);
printf("Enter fee paid : ");
scanf("%d", & s.fp);
/* write record to file */
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
}
#include <stdio.h>
struct student
{
int sno;
char name[30];
int tf, fp;
};
main()
{
struct student s;
FILE * fp;
int count;
fp = fopen("student.dat","rb"); /* open in read and binary mode */
/* read record */
count = fread(&s,sizeof(s),1,fp);
while( count != 0 )
{
if ( s.tf > s.fp )
printf("%d %s %d %d ", s.sno, s.name, s.tf, s.fp);
count = fread(&s,sizeof(s),1,fp);
}
fclose(fp);
}
#include <stdio.h>
struct student
{
int sno;
char name[30];
int tf, fp;
};
main()
{
struct student s;
FILE * fp;
int recno;
fp = fopen("student.dat","rb"); /* open in read and binary mode */
/* read record number */
printf("Enter record number. Record number starts with 0 : ");
scanf("%d",&recno);
/* move to the record with the given record number */
fseek ( fp, sizeof(s) * recno , 0);
/* read the record */
fread(&s,sizeof(s),1,fp);
printf("%d %s %d %d ", s.sno, s.name, s.tf, s.fp);
fclose(fp);
}
#include <stdio.h>
struct student
{
int sno;
char name[30];
int tf, fp;
};
main()
{
struct student s;
FILE * fp;
int recno;
fp = fopen("student.dat","r+b"); /* open in update and binary mode */
/* read record number */
printf("Enter record number. Record number starts with 0 : ");
scanf("%d",&recno);
printf("Enter Amount paid : ");
scanf("%d",&amt);
/* move to the record with the given record number */
fseek ( fp, sizeof(s) * recno , 0);
/* read the record */
fread(&s,sizeof(s),1,fp);
/* update structure in memory */
s.fp += amt;
/* move back to the previous record */
fseek ( fp, sizeof(s) * -1l, 1);
/* write new record back to file */
fwrite(&s,sizeof(s),1,fp);
printf("Updation is complete");
fclose(fp);
}
Chapter 20 - Command line arguments
I. Write program
#include <stdio.h>
main(int argc, char * argv[] )
{
int ch;
FILE * sfp, * tfp;
if ( argc < 3 )
{
/* display syntax of the program. Assuming file name is LOWER.C */
printf("LOWER source target");
exit(1);
}
sfp = fopen(argv[1],"r");
if ( sfp == NULL)
{
printf("Source file not found.");
exit(2);
}
tfp = fopen(tfn,"w");
if ( tfp == NULL)
{
printf("Target file not found.");
exit(3);
}
while ( !feof(sfp) )
{
ch = fgetc(sfp);
fputc( tolower(ch), tfp);
}
fcloseall(); /* close both the files */
} /* end of main */
/* program to search for a string a file and display
line where string is found along with line numbers */
#include <stdio.h>
main()
{
FILE * fp;
char line[100], word[30] , fn[30];
int ln=1;
/* take input from user */
printf("Enter filename : ");
gets(fn);
printf("Enter string to search for : ");
gets(word);
/* open file */
fp = fopen(fn,"r");
ln = 1;
while( !feof(fp))
{
fgets(line,100,fp);
if ( strstr(line,word) != NULL )
printf("%4d:%s", ln, line);
ln++;
}
fclose(fp);
}
Example: c:\>concat t.txt a.txt b.txt
Places contents of a.txt and b.txt one after another into t.txt.
#include <stdio.h>
main(int argc, char * argv[] )
{
int ch;
FILE * sfp, * tfp;
int i;
if ( argc < 3 )
{
/* display syntax of the program. Assuming file name is COPYALL.C */
printf("COPYALL target source ...");
exit(1);
}
/* open target file */
tfp = fopen(argv[1],"w");
if ( tfp == NULL)
{
printf("Target file open error.");
exit(2);
}
for ( i = 2; i < argc ;i ++)
{
/* open source file */
sfp = fopen( argv[i], "r");
if ( sfp == NULL)
{
printf("Error while opening file %s ", argv[i]);
exit(3);
}
while ( !feof(sfp) )
{
ch = fgetc(sfp);
fputc( ch, tfp);
}
/* close current source file */
fclose(sfp);
} /* end of for loop */
fclose(tfp); /* close target file */
} /* end of main */
Chapter 21 - Screen library
I. Fill in the blank
II. Write program.
#include <conio.h<>
main()
{
int i;
clrscr();
for ( i = 1; i <= 25 ;i ++)
{
gotoxy(39,i);
printf("%d",i);
}
}
#include <conio.h<>
main()
{
int i;
clrscr();
window(21,8,60,18);
gotoxy(20,5);
cprintf("P.Srikanth");
}
Chapter 22 - Graphics
I. Fill in the blanks
II. Write programs.
Hint: Use random() and kbhit() function. Please see online help for more details about kbhit() and random() functions.
/* draw dots continuosly at random locations */
#include <graphics.h>
#include <stdlib.h>
main()
{
int x,y,gm,gd;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
if ( graphresult() != 0 )
{
printf("Unable to shift to graphics mode.");
exit(1);
}
while(! kbhit())
{
x = random( getmaxx());
y = random( getmaxy());
putpixel(x,y,15);
delay(100);
}
}
#include <graphics.h>
main()
{
int x,y,i,gm,gd;
int points[22];
clrscr();
for (i = 0; i < 10; i +=2)
{
printf("Enter x,y :");
scanf("%d,%d",&x,&y);
points[i] = x;
points[i+1] = y;
}
/* place first point at the end to connect first and last points */
points[10] =points[0];
points[11] = points[1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
if ( graphresult() != 0 )
{
printf("Unable to shift to graphics mode.");
exit(1);
}
drawpoly(6,points);
getch();
closegraph();
}
#include <graphics.h>
main()
{
int x,y,r,gm,gd, maxradius;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
if ( graphresult() != 0 )
{
printf("Unable to shift to graphics mode.");
exit(1);
}
x = getmaxx() / 2;
y = getmaxy() / 2;
maxradius = x < y ? x :y;
for ( r = 10; r <= maxradius ; r += 5)
{
circle(x,y,r);
delay(2000);
}
getch();
closegraph();
}
Chapter 23 - Dynamic memory allocation
I. Fill in the blanks
II. Write Programs
/* Take a collection of strings and store them in an array of char *
Memory for strings is allocated dynamically */
main()
{
char * a[10];
char st[100];
int i;
/* take 10 strings */
clrscr();
for ( i = 0 ; i < 5 ; i ++)
{
printf("Enter a string :");
gets(st);
/* allocate memory for the given string */
a[i] = (char * ) malloc( strlen(st));
/* copy string into newly allocated space */
strcpy(a[i],st);
}
/* display all strings */
for ( i = 0 ; i < 5 ; i ++)
puts( a[i]);
}
Chapter 24 - Linked list
I. Write programs
#include <stdio.h>
/* create a structure for node */
typedef struct node
{
char line[100];
struct node * prev;
} NODE;
main()
{
NODE * root= NULL ,*current;
char fn[10],line[100];
FILE * fp;
clrscr();
printf("Enter filename :");
gets(fn);
fp = fopen(fn,"r");
while ( !feof(fp))
{
fgets(line,100,fp);
printf(line);
current = (NODE * ) malloc(sizeof(NODE));
if ( current == NULL)
{
printf("Memory allocation error. ");
exit(1);
}
/* copy data into node */
strcpy( current -> line, line);
current -> prev = root;
root = current;
} /* end of while */
fclose(fp);
/* display the content of the file */
current = root;
while ( current != NULL)
{
printf(current-> line);
current = current -> prev;
} /* end of while */
} /* end of main */
#include <alloc.h>
/* create a structure for node */
typedef struct node
{
char line[100];
struct node * next;
} NODE;
NODE * search(NODE * root, char name[20] )
{
NODE * current;
current = root;
while ( current != NULL)
{
if ( strcmp( current-> line, name) == 0 )
return current;
current = current -> next;
}
return NULL; /* value not found */
} /* end of function */