count data types in c
Hello everybody!
I want to write a program in c that has to open a file (a c program) and count how many char or double declarations have been made in it, so as to calculate the amount of memory that char and double data types will occupy in memory. I started the program like this:
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp;
char c[20];
if ((fp=fopen("d:\\program.c","r"))==NULL)
{
printf("to arxeio den yparxei\n");
exit(0);
}
else {
printf("file opened successfully! Contents:\n");
while (fscanf(fp,"%s",c)!=EOF)
{
if (strcmp(c,"char")==0)
{
}
printf("String: %s \n",c);
}
}
system("pause");
}
but don't have any idea how to continue. Does anyone have an idea? Thank you in advance for reading my message...
vanalex
vanalex
Junior Poster in Training
74 posts since Jan 2008
Reputation Points: 10
Solved Threads: 10
Do you have simple C programs to pass into your program.
Because "any C program" can get real messy to deal with.
int main ( int argc, char *argv[] ) {
char array[10]; /* array of char */
printf( "Sizeof char = %d\n", sizeof( char ) );
return 0;
}
There's only one char you should be paying attention to, and that's complicated by being an array.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
And let's not forget about alignment issues, recursive functions, data structures, and anything whose size depends on user input.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
You may type some lexical analyzer, which will identify a declaration part of each variable.
But for construction like type casting it will not work.
So, for clear solution you have to write syntax analyzer, and grammar of C.
Protuberance
Junior Poster in Training
90 posts since Aug 2009
Reputation Points: 78
Solved Threads: 17