Results 1 to 5 of 5

Thread: Finding out the size of an intger in C

  1. #1
    Join Date
    Feb 2009
    Posts
    10

    Finding out the size of an intger in C

    Hi,
    I want to know the size on an integer.
    I know how to do this for a string but when i use this for integer with while look it stops at 0 number.

    Any idea for the same?

  2. #2
    Join Date
    May 2008
    Posts
    115

    Re: Finding out the size of an intger in C

    int[] array isn't an object, so the size isn't readily available (unless you make use of a datastructure like a vector).

    Luckily there is the sizeof() operator. Here sizeof() returns the amount of bytes such an array occupies, when we dived this by the size of one unit (sizeof(int)) of the array we get the number of elements in it. (or at least the number of elements we reserver the room for)

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Finding out the size of an intger in C

    Try this!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
    int a[2];
    
    /* Get the consecutive memory addresses */
    unsigned long b = &a[0];
    unsigned long c = &a[1];
    
    /* Find the difference between the addresses */
    unsigned int size = labs(b-c);
    
    printf("Size of int is %u \n", size);
    return 0;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Finding out the size of an intger in C

    When the parameter is a datatype.
    For Eg:
    Code:
     sizeof(int), sizeof(double)
    #define GetSize(x) (char*)((x*)10 + 1) - (char*)10

    When the parameter is a variable.
    For Eg:

    Code:
    int a;
    float b;
     sizeof(a), sizeof(b)
    #define GetSize(x) (char*)(&x + 1) - (char*)&x

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Finding out the size of an intger in C

    Code:
    #include <stdio.h>
    
    struct node {
    	int x;
    	int y;
    };
    
    unsigned int find_size ( void* p1, void* p2 )
    {
    	return ( p2 - p1 );
    }
    
    int main ( int argc, char* argv [] )
    {
    	struct node data_node;
    	int x = 0;
    
    	printf ( "\n The size :%d", 
    			find_size ( (void*) &data_node, 
    				(void*) ( &data_node + 
    1 ) ) );
    	printf ( "\n The size :%d", find_size ( (void*) &x, 
    				(void*) ( &x + 1 ) ) );
    }
    It will work for any data type

Similar Threads

  1. Need help finding drivers for IBM
    By Quinn28 in forum Operating Systems
    Replies: 1
    Last Post: 14-12-2011, 05:51 PM
  2. Active Title Bar size changes notification icon size
    By Ambak in forum Operating Systems
    Replies: 3
    Last Post: 30-07-2010, 01:39 PM
  3. Replies: 1
    Last Post: 24-04-2007, 03:14 PM
  4. Replies: 3
    Last Post: 18-04-2007, 04:26 PM
  5. Replies: 1
    Last Post: 04-04-2007, 05:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,888,722.59150 seconds with 16 queries