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

Recommended Answers

All 3 Replies

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.

And let's not forget about alignment issues, recursive functions, data structures, and anything whose size depends on user input.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.