C Program to find the size of a given file.

Write a C Program to find the size of a given file.
The function fseek() is used to set the file pointer at the specified position. In general, it is used to move the file pointer to the desired position within the file.
In this program we moved the file pointer to the end of file, and obtain the current position of the file pointer. Function rewind() also moves the file pointer to the beginning of the file, but syntax and number of parameters are different. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<process.h>
long file_size(FILE *fp)
{
long len;
//move file pointer to end of the file */
fseek(fp, 0L, 2);
//Obtain the current position of file pointer
len = ftell(fp);
return len;
}
void main(void)
{
FILE *fp;
char filename[20];
printf("nEnter the file name:nn");
scanf("%s",filename);

fp = fopen(filename, r);

if(fp == NULL)
{
printf("Error: file not found!nn");
exit(0);
}
printf("File size of %s is %ld bytesnn",filename, file_size(fp));
fclose(fp);
}

Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement address calculation sort.

Write a C Program to implement address calculation sort.
Address calculation sort is the sorting method which sorts the given array by using insertion method.
In this algorithm, a hash function is used and applied to the each key. Result of hash function is placed in the linked lists. The hash function must be a order preserving function which places the elements in linked lists are called as sub files. An item is placed into a sub-file in correct sequence by using any sorting method. After all the elements are placed into subfiles, the list is concatenated to produce the sorted list.
 Address calculation sort is used to efficiently store non-contiguous keys (account numbers, part numbers, etc.) that may have wide gaps in their alphabetic and numeric sequences.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>

#include<malloc.h>

#define MAX 20

struct node

{

int info ;

struct node *link;

};

struct node *head[10];

int n,i,arr[MAX];

main()

{

int i;

printf("Enter the number of elements in the list : ");

scanf("%d", &n);

for(i=0;i<n;i++)

{

printf("Enter element %d : ",i+1);

scanf("%d",&arr[i]);

}/*End of for */

printf("Unsorted list is :n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

printf("n");

addr_sort();

printf("Sorted list is :n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

printf("n");

}/*End of main()*/

addr_sort()

{

int i,k,dig;

struct node *p;

int addr;

k=large_dig();

for(i=0;i<=9;i++)

head[i]=NULL;

for(i=0;i<n;i++)

{

addr=hash_fn( arr[i],k );

insert(arr[i],addr);

}

for(i=0; i<=9 ; i++)

{

printf("head(%d) -> ",i);

p=head[i];

while(p!=NULL)

{

printf("%d ",p->info);

p=p->link;

}

printf("n");

}


i=0;

for(k=0;k<=9;k++)

{

p=head[k];

while(p!=NULL)

{

arr[i++]=p->info;

p=p->link;

}

}

}/*End of addr_sort()*/

/*Inserts the number in sorted linked list*/

insert(int num,int addr)

{

struct node *q,*tmp;

tmp= malloc(sizeof(struct node));

tmp->info=num;

/*list empty or item to be added in begining */

if(head[addr] == NULL || num < head[addr]->info)

{

tmp->link=head[addr];

head[addr]=tmp;

return;

}

else

{

q=head[addr];

while(q->link != NULL && q->link->info < num)

q=q->link;

tmp->link=q->link;

q->link=tmp;

}

}/*End of insert()*/

/* Finds number of digits in the largest element of the list */

int large_dig()

{

int large = 0,ndig = 0 ;

for(i=0;i<n;i++)

{

if(arr[i] > large)

large = arr[i];

}

printf("Largest Element is %d , ",large);

while(large != 0)

{

ndig++;

large = large/10 ;

}

printf("Number of digits in it are %dn",ndig);

return(ndig);

} /*End of large_dig()*/

hash_fn(int number,int k)

{

/*Find kth digit of the number*/

int digit,addr,i;

for(i = 1 ; i <=k ; i++)

{

digit = number % 10 ;

number = number /10 ;

}

addr=digit;

return(addr);

}/*End of hash_fn()*/




Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to demonstrate dynamic memory allocation example.

Write a C Program to demonstrate dynamic memory allocation example.
Dynamic memory allocation means you can allocate or relocate (manipulate) the memory at the run time, using malloc, calloc, and realloc functions.
Using malloc, We can allocate block of memory for a variable
Using calloc function, We can allocate multiple blocks of memory for a variable.
We can alter, reassign the allocated memory using the realloc function.
We can releasing the allocated memory using the free function. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include<stdio.h>
#include<stdlib.h>

int main() {
int* grades;
int sum = 0, i, numberOfStudents;
float average;


printf("Enter the number of students: ");
scanf("%d", &numberOfStudents);
getchar();

if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) {
printf("nError: Not enough memory to allocate grades arrayn");
exit(1);
}

printf("nEnter the grades of %d students (in separate lines):n", numberOfStudents);

for(i = 0; i < numberOfStudents; i++) {
scanf("%d", &grades[i]);
getchar();
}

/* calculate sum */
for(i = 0; i < numberOfStudents; i++)
sum += grades[i];

/* calculate the average */
average = (float) sum / numberOfStudents;

printf("nThe average of the grades of all students is %.2f",
average);

getchar();
return(0);
}

Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to generate and print prime numbers in a given range.

Write a C program to generate and print prime numbers in a given range. Also print the number of prime numbers.
Prime number is a whole number and greater than 1, which is divisible by one or itself.
Example: 2, 3, 5, 7, 11, 13………… Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
int M, N, i, j, flag, temp, count = 0;

clrscr();

printf("Enter the value of M and Nn");
scanf("%d %d", &M,&N);

if(N < 2)
{
printf("There are no primes upto %dn", N);
exit(0);
}
printf("Prime numbers aren");
temp = M;

if ( M % 2 == 0)
{
M++;
}
for (i=M; i<=N; i=i+2)
{
flag = 0;

for (j=2; j<=i/2; j++)
{
if( (i%j) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("%dn",i);
count++;
}
}
printf("Number of primes between %d and %d = %dn",temp,N,count);
}



Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to copy and concatenate strings without using standard functions.

C Program to copy and concatenate strings without using standard functions.
In this program , we copy one string from another, and without using the standard library function strcpy from string.h .
Here we appends the one string to another without using the strcat function. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>

void newStrCpy(char *,char *);
void newStrCat(char *,char *);

void main()
{
char str1[80],str2[80];
int opn;
do
{
printf("Press 1- NewStrCpy t 2- NewStrCatt 3- Exitt Your Option?");
scanf("%d",&opn);
switch(opn)
{
case 1: flushall();
printf("n Read the Source String n");
gets(str1);
newStrCpy(str2,str1);
printf(" Copied String: %sn",str2);
break;
case 2: flushall();
printf(" Read the First String n");
gets(str1);
printf(" Read the Second String n");
gets(str2);
newStrCat(str1,str2);
printf("Concatenated String: n");
puts(str1);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!n");
break;
}
}while(opn != 3);
} /* End of main() */

void newStrCpy(char *d,char *s)
{
while( (*d++ = *s++));
}
void newStrCat(char *s, char *t)
{
while(*s)
s++;
while(*s++ = *t++);
}

Read more c programs 
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 7-9.

K and R C, Solution to Exercise 7-9:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C Functions like isupper() can be implemented to save space or to save time. Explore both possibilities. Read more about C Programming Language .
isupper: return 1 (true) if c is an upper case letter
Normal C function:

int  isupper(char c)
{
if (c >= 'A' && c <= 'Z')
return 1;
else
return 0;
}

This simple code tests the character is upper or lower , If the character within the range of ASCII upper case letters it returns 1, otherwise 0.
To save space or to save time using the macros is the best possibility
C Code:

#define isupper(c)  ((c) > = 'A' && (c) <= 'Z') ? 1:0

Macro version of isupper is more efficient because, there is no overhead of the function call and it uses more space because the macro is expanded in line every time it is invoked.

Read more c programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 7-6.

K and R C, Solution to Exercise 7-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C Program to compare two files, printing the first line where they differ.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXLINE 100
/*comp: compare two file, printing the first line where they differ*/
main(int argc, char *argv[])
{
FILE *fp1, *fp2;
void filecomp(FILE *fp1, FILE *fp2);
if(argc != 3){
fprintf(stderr,"comp:need two file namesn");
exit(1);
} else {
if((fp1 = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "comp:can't open %sn",*argv);
exit(1);
} else if((fp2 = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "comp:can't open %sn",*argv);
exit(1);
}else {
filecomp(fp1,fp2);
fclose(fp1);
fclose(fp2);
exit(0);

}
}
}

//filecomp: compare two files -a line at a time
void filecomp (FILE *fp1, FILE *fp2)
{
char line1[MAXLINE], line2[MAXLINE];
char *lp1 = *lp2;
do{
lp1 = fgets(line1, MAXLINE, fp1);
lp2 = fgets(line2, MAXLINE, fp2);
if(lp1 == line1 && lp2 == line2){

if(strcmp(line1,line2) !=0) {
printf("First difference in linen%sn",line1);
lp1 = lp2 = NULL;
}
} else if(lp1 != line1 && lp2 == line2)
printf("end of first file at linen%sn",line2);
else if(lp1 == line1 && lp2 == line2)
printf("end of second file at line n%sn",line1);

}while(lp1 == line1 && lp2 == line2);
}



Read more c programs

C Basic
C File i/o
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 7-3.

K and R C, Solution to Exercise 7-3:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to Revise minprintf to handle more of the other facilities of printf. minprintf walks along the argument list and printf does the actual printing for the facilities supported.
To handle more of the other facilities of printf we collect in local fmt the % and any other characters until an alphabetic character -the format letter.local fmt is the format argument for printf. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdarg.h>
#include <stdio.h>
#include<ctype.h>
#define LOCALFMT 100

/* minprintf: minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
va_list ap;
char *p, *sval;
char localfmt[LOCALFMT];
int i;
int ival;
double dval;
unsigned uval;

va_start(ap, fmt); /* make ap point to the first unnamed arg */
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
i = 0;
localfmt[i++] = '%';
while(*p(p+1) && !isalpha(*(p+1)))
localfmt[i++] = *++p;
localfmt[i++] = *(p+1);
localfmt[i] = '/0';
switch (*++p) {
case 'd':
case 'i':
ival = va_arg(ap, int);
printf("%d", ival);
break;
case 'c':
ival = va_arg(ap, int);
putchar(ival);
break;
case 'u':
uval = va_arg(ap, unsigned int);
printf("%u", uval);
break;
case 'o':
uval = va_arg(ap, unsigned int);
printf("%o", uval);
break;
case 'x':
uval = va_arg(ap, unsigned int);
printf("%x", uval);
break;
case 'X':
uval = va_arg(ap, unsigned int);
printf("%X", uval);
break;
case 'e':
dval = va_arg(ap, double);
printf("%e", dval);
break;
case 'f':
dval = va_arg(ap, double);
printf("%f", dval);
break;
case 'g':
dval = va_arg(ap, double);
printf("%g", dval);
break;
case 's':
for (sval = va_arg(ap, char *); *sval; sval++)
putchar(*sval);
break;
default:
putchar(*p);
break;
}
}
va_end(ap);
}




Read more c programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 7-1.

K and R C, Solution to Exercise 7-1:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program that converts upper case to lower case or lower case to upper, depending on the name it is invoked with, as found in argv[0]. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<ctype.h>
#include<string.h>

//lower: converts upper case to lower case
//upper:converts upper case to lower case

main(int argc, *argv[])
{
int c;
if(strcmp(argv[0],"lower") == 0)
while((c = getchar()) != EOF)
putchar(tolower(c)) != EOF)
else
while(c = getchar()) != EOF)
putchar(toupper(c));
return 0;
}

Read more c programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 6-6.

K and R C, Solution to Exercise 6-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to implement a simple version of the #define processor(i.e , no arguments) suitable for use with C programs, based on the routines of this section, You may also find getch and ungetch helpful.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<ctype.h>
#include<string.h>

#define MAXWORD 100

struct nlist{
struct nlist *next;
char *name;
char *defn;
};
void error(int, char*);
int getch(void);
void getdef(void);
int getword(char *,int);
struct nlist *install(char *,char *);
struct nlist *lookup(char *);
void skipblanks(void);
void undef(char *);
void ungetch(int);
void ungets(char *);


//simple version of #define processor
main()
{
char w[MAXWORD];
struct nlist *p;
while(getword(w, MAXWORD) != EOF)
if(strcmp(w, "#") == 0)
getdef();
else if(!isalpha(w[0]))
printf("%s",w);
else if ((p == lookup(w)) == NULL)
printf("%s",w);
else
ungets(p->defn);
}

//getdef:get defination and install it
void getdef()
{
int c,i;
char def[MAXWORD], dir[MAXWORD], name[MAXWORD];
skipblanks();
if(!isalpha(getword(dir,MAXWORD)))
error(dir[0],"getdef:expecting a directve after #");
else if(strcmp(dir,"define") == 0) {
skipblanks();
if(!isalpha(getword(name, MAXWORD)))
error(name[0],"getdef:non alpha name expected"):
else{
skipblanks();
for(i = 0;i < MAXWORD-1; i++)
if((def[i] = getch()) == EOF || def[i] == 'n')
break;
def[i] = '';
if(i <= 0)
error('n',"getdef:non alpha in define");
else
install(name, def);
}
}else if(strcmp(dir,"undef") == 0) {
skipblanks();
if(!isalpha(getword(name, MAXWORD)))
error(name[0],"getdef:non alphain undef"):
else
undef(name);
}else
error(dir[0],"getdef:expecting a directive after #");
}

//undef:remove a name and defination from the table
int undef(char * name) {
struct nlist * np1, * np2;

if ((np1 = lookup(name)) == NULL) /* name not found */
return 1;

for ( np1 = np2 = hashtab[hash(name)]; np1 != NULL;
np2 = np1, np1 = np1->next ) {
if ( strcmp(name, np1->name) == 0 ) { /* name found */

/* Remove node from list */

if ( np1 == np2 )
hashtab[hash(name)] = np1->next;
else
np2->next = np1->next;

/* Free memory */

free(np1->name);
free(np1->defn);
free(np1);

return 0;
}
}

return 1; /* name not found */
}

//error:print error message and skip the rest of the line
void error(int c, char *s)
{
printf("error:%sn",s);
while(c != EOF && c != 'n')
c = getch();
}

//skipblanks:skip blan and tab characters
void skipblanks()
{
int c;
while((c = getch()) == ' ' ||c == 't')
;
ungetch(c);
}

/*ungets: push string back onto the input*/
void ungets(char s[])
{
int len=strlen(s);
void ungetch(int);
while (len >0)
ungetch(s[--len]);
}
/*note: modify the getword function to return
spaces so that the output resembles the input data.*/
Read more c programs

C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com