I have written a program that reads simple data declarations and responds with the amount of memory that would be allocated to that variable.

Each input line should consist of - A type name, which must be one of the following: char, int, short, long, float, or double. - One or more individual declaration specifications separated by commas. - A semicolon marking the end of line.

In my code I also add a use of file (see below).

**I would like to ask youhow can I use sizeof instead the way I coded the size of data types to be allocated. I also would like to ask you whether my use in file is right.**

```
#define _CRT_SECURE_NO_WARNINGS

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

void bytesPerValue(char str[], char* filename) ;
int theSizeOf(char *str);
 int strToNumber(char *str);

void main()
{
     char str[50];

     gets(str);

     bytesPerValue(str,"input.txt");

}

void bytesPerValue(char str[], char* filename) 
{
        int i = 0, j = 0;
        int temp = 1;
        int size;
        char* tempChar = (char*)malloc((strlen(str))*sizeof(tempChar));
        FILE *f=fopen(filename,"w");
        if (f==NULL)
            exit(1);
        while (str[i]!=' ' || str[i]=='*') //checking the type of the variables//
        {
                tempChar[j] = str[i];
                i++;
                j++;   
        }
        tempChar[j] = '\0';
        size = theSizeOf(tempChar);
        j = 0;
        i++;
        while (str[i] != ';')
        {

                if (isalpha(str[i]) || str[i]=='_') // for  variables and arrays//
                {
                        while (str[i] != ',' && str[i] != ';') //runs until ', ' or '; ' //
                        {
                                if (str[i]==' ')
                                {
                                        while (str[i]==' ')
                                                i++;
                                }

                                if (str[i] == '[') //checks if it is array//
                                {
                                        printf("%c", str[i]);
                                        i++;
                                        while (str[i] != ']')
                                        {
                                                tempChar[j] = str[i]; //copies the value in the string//
                                                i++;
                                                j++;
                                        }

                                        tempChar[j] = '\0';
                                        temp = strToNumber(tempChar); //converting to number in order to valuate the bytes//
                                }
                                printf("%c", str[i]);
                                i++;

                                if (isspace(str[i]))
                                {
                                        while (isspace(str[i]))
                                                i++;
                                }
                        }
                        fprintf(f," requires %d bytes \n", temp*(sizeof(temp)));
                }

                if (str[i] == '*') //for pointers//
                {
                        while (str[i] != ',' && str[i] != ';')
                        {
                                printf("%c", str[i]);
                                i++;
                                if (str[i]==' ')
                                {
                                        while (str[i]==' ')
                                                i++;
                                }
                        }
                        fprintf(f," requires %d bytes \n", 4);
                }
                if (str[i] != ';')
                        i++;
        }

 fclose(f);
}

int theSizeOf(char* str) // checking the size of the variable
{
        if (strcmp(str, "int")==0 || strcmp(str, "long")==0 || strcmp(str, "float")==0)
                return 4;
        else if (strcmp(str, "char")==0)
                return 1;
        else if (strcmp(str, "double")==0)
                return 8;
        else if (strcmp(str, "short")==0)
                return 2;
        else 
            return 0;
}

int strToNumber(char* str) //converting the string to number//
{
        int temp=1;
        int num=0;
        int t;
        int i;
        int length = strlen(str);
        for (i = length-1; i >= 0; i--)
        {
                t = str[i] - '0';
                num += t * temp;
                temp *= 10;
        }
        return num;
} 

```