facebook like button

11 June, 2011

program 63: getting size of data-type in C

The need:
  This is a program just to show how sizeof() function can be used to determine memory space occupied by any variable or data-type.
The code:
--------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct list
{
    char name[20];
    int age;
    float height;
};
typedef struct list person;
int main()
{
    person p;
    int i;
    char c;
    int size_of_person;
    size_of_person=sizeof(person);
    printf("memory occupied by person = %d bytes\n",size_of_person);
    printf("memory occupied by person = %d bytes\n",sizeof(person));
    printf("memory occupied by variable p = %d bytes\n\n",sizeof(p));
    printf("memory occupied by integer = %d bytes\n",sizeof(int));
    printf("memory occupied by variable i = %d bytes\n\n",sizeof(i));
    printf("memory occupied by character = %d bytes\n",sizeof(char));
    printf("memory occupied by vairable c = %d bytes\n\n",sizeof(c));
    return 0;
}
--------------------------------------------
The approach:
This program is one of the simplest programs which give intro of using sizeof function. First a user defined data-type person is defined. After that sizeof() function is used in different ways, how it can be directly used in printf statement and how it can return a value to an integer variable. Other than this there is nothing in the program.


No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply