Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2009
Member
 
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?
Reply With Quote
  #2  
Old 13-02-2009
Member
 
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)
Reply With Quote
  #3  
Old 13-02-2009
Member
 
Join Date: May 2008
Posts: 2,008
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;
}
Reply With Quote
  #4  
Old 13-02-2009
Member
 
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
Reply With Quote
  #5  
Old 13-02-2009
Member
 
Join Date: May 2008
Posts: 2,293
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Finding out the size of an intger in C"
Thread Thread Starter Forum Replies Last Post
Need help finding drivers for IBM Quinn28 Operating Systems 1 14-12-2011 05:51 PM
Active Title Bar size changes notification icon size Ambak Operating Systems 3 30-07-2010 01:39 PM
Increasing the size of the Limit profile size GPO object past 30MB Stel Window 2000 Help 1 24-04-2007 03:14 PM
The logical size of this database is 16 GB. This database size is approaching the size limit of 18 GB HandelMan Small Business Server 3 18-04-2007 04:26 PM
Exchange warning: This database size is approaching the size limit of 18 GB. subhaash Small Business Server 1 04-04-2007 05:11 AM


All times are GMT +5.5. The time now is 09:15 PM.