#include
long flength(char *fname)
{
FILE *fptr;
long length = -1L;
fptr = fopen(fname, "rb");
if(fptr != NULL)
{
fseek(fptr, 0L, SEEK_END);
length = ftell(fptr);
fclose(fptr);
}
return length;
}
#ifdef TEST
main(int argc, char *argv[])
{
printf("Length of %s = %ld\n", argv[0], flength(argv[0]));
return 0;
}
#endif /* TEST */
Showing posts with label Memory Allocation. Show all posts
Showing posts with label Memory Allocation. Show all posts
a simple function using all ANSI-standard functions to determine the size of a file
Labels:
C Assignments,
Memory Allocation
Program to masking the given set of numbers.
#include
int main()
{
char mask;
char number[6];
char and, or, xor, inv, index;
number[0] = 0X00;
number[1] = 0X11;
number[2] = 0X22;
number[3] = 0X44;
number[4] = 0X88;
number[5] = 0XFF;
printf(" nmbr mask and or xor inv\n");
mask = 0X0F;
for (index = 0 ; index <= 5 ; index++)
{
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",
number[index], mask, and, or, xor, inv);
}
printf("\n");
mask = 0X22;
for (index = 0 ; index <= 5 ; index++)
{
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",
number[index], mask, and, or, xor, inv);
}
return 0;
}
/* OUTPUT of Program
nmbr mask and or xor inv
0 f 0 f f ffff
11 f 1 1f 1e ffee
22 f 2 2f 2d ffdd
44 f 4 4f 4b ffbb
ff88 f 8 ff8f ff87 77
ffff f f ffff fff0 0
0 22 0 22 22 ffff
11 22 0 33 33 ffee
22 22 22 22 0 ffdd
44 22 0 66 66 ffbb
ff88 22 0 ffaa ffaa 77
ffff 22 22 ffff ffdd 0
*/
Related Links :
Labels:
Basics of C,
C Assignments,
Memory Allocation
Program to create dynamic structures using malloc
#include
#include
#include
struct animal
{
char name[25];
char breed[25];
int age;
} *pet[12], *point; /* this defines 13 pointers, no variables */
int main()
{
int index;
/* first, fill the dynamic structures with nonsense */
for (index = 0 ; index < 12 ; index++)
{
pet[index] = (struct animal *)malloc(sizeof(struct animal));
if (pet[index] == NULL)
{
printf("Memory allocation failed\n");
exit (EXIT_FAILURE);
}
strcpy(pet[index]->name, "General");
strcpy(pet[index]->breed, "Mixed Breed");
pet[index]->age = 4;
}
pet[4]->age = 12; /* these lines are simply to */
pet[5]->age = 15; /* put some nonsense data into */
pet[6]->age = 10; /* a few of the fields. */
/* now print out the data described above */
for (index = 0 ; index < 12 ; index++)
{
point = pet[index];
printf("%s is a %s, and is %d years old.\n",
point->name, point->breed, point->age);
}
/* good programming practice dictates that we free up the */
/* dynamically allocated space before we quit. */
for (index = 0 ; index < 12 ; index++)
free(pet[index]);
return EXIT_SUCCESS;
}
/* OUTPUT
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 12 years old.
General is a Mixed Breed, and is 15 years old.
General is a Mixed Breed, and is 10 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
*/
Related Links :
Labels:
Basics of C,
C Assignments,
Memory Allocation
Subscribe to:
Posts (Atom)
If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE
|
More Useful Topics... |
|
History Of C..
In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
My 1st Program...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}
Next Step...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");
printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");
}

