By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,437 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,437 IT Pros & Developers. It's quick & easy.

what is the type of a #define const number

P: n/a
the code is as follows:

#include<stdio.h>
int arr[]={1,2,3,4,5,6,7};
#define size (sizeof(arr)/sizeof(arr[0]))
int main()
{
int index = -1;
int x = 0;
int t;
if( index < size - 1 )
x = arr[index+1];
printf("%d",x);
return 0;
}

then the value of x is 0, not 1. Maybe the type of "index" is promoted
to unsigned int ?so the type of "size" is unsigned int?How can I
decide the type of a const number by #define macro?Is there anyone
can tell me?
Thank you!

Mar 14 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
mi*********@gmail.com wrote:
the code is as follows:

#include<stdio.h>
int arr[]={1,2,3,4,5,6,7};
#define size (sizeof(arr)/sizeof(arr[0]))
int main()
{
int index = -1;
int x = 0;
int t;
if( index < size - 1 )
x = arr[index+1];
printf("%d",x);
return 0;
}

then the value of x is 0, not 1. Maybe the type of "index" is promoted
to unsigned int ?so the type of "size" is unsigned int?
Actually, yes. 'sizeof' "returns" size_t, which is unsigned in most
implementations. So, promoted to 'size_t', not necessarily unsigned int.
"size" gets replaced with "(sizeof(arr)/sizeof(arr[0]))", so by the time
there are any types in the program, there is no "size"...
>How can I
decide the type of a const number by #define macro?Is there anyone
can tell me?
Any macro just substitutes one expression with another. You need to
see what your code looks like after preprocessing to understand what
your expression looks like and what type it has.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #2

P: n/a
On Mar 14, 6:17 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
miaohua1...@gmail.com wrote:
the code is as follows:
#include<stdio.h>
int arr[]={1,2,3,4,5,6,7};
#define size (sizeof(arr)/sizeof(arr[0]))
int main()
{
int index = -1;
int x = 0;
int t;
if( index < size - 1 )
x = arr[index+1];
printf("%d",x);
return 0;
}
then the value of x is 0, not 1. Maybe the type of "index" is promoted
to unsigned int ?so the type of "size" is unsigned int?

Actually, yes. 'sizeof' "returns" size_t, which is unsigned in most
implementations. So, promoted to 'size_t', not necessarily unsigned int.
"size" gets replaced with "(sizeof(arr)/sizeof(arr[0]))", so by the time
there are any types in the program, there is no "size"...
How can I
decide the type of a const number by #define macro?Is there anyone
can tell me?

Any macro just substitutes one expression with another. You need to
see what your code looks like after preprocessing to understand what
your expression looks like and what type it has.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
The disadvantage of macro is that it doesnot follow the strict type
checking

To run the code, write the statement as

#define size (int)(sizeof(arr)/sizeof(arr[0]))
OR
if( index < int(size - 1) )

Mar 14 '07 #3

P: n/a
yashwant pinge wrote:
On Mar 14, 6:17 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>miaohua1...@gmail.com wrote:
>>the code is as follows:
>>#include<stdio.h>
int arr[]={1,2,3,4,5,6,7};
#define size (sizeof(arr)/sizeof(arr[0]))
int main()
{
int index = -1;
int x = 0;
int t;
if( index < size - 1 )
x = arr[index+1];
printf("%d",x);
return 0;
}
>>then the value of x is 0, not 1. Maybe the type of "index" is
promoted to unsigned int ?so the type of "size" is unsigned int?

Actually, yes. 'sizeof' "returns" size_t, which is unsigned in most
implementations. So, promoted to 'size_t', not necessarily unsigned
int. "size" gets replaced with "(sizeof(arr)/sizeof(arr[0]))", so by
the time there are any types in the program, there is no "size"...
>>How can I
decide the type of a const number by #define macro?Is there anyone
can tell me?

Any macro just substitutes one expression with another. You need to
see what your code looks like after preprocessing to understand what
your expression looks like and what type it has.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

The disadvantage of macro is that it doesnot follow the strict type
checking
How does type *checking* would play here? '(sizeof(a)/sizeof(b))'
expression has a very specific type.
To run the code, write the statement as

#define size (int)(sizeof(arr)/sizeof(arr[0]))
OR
if( index < int(size - 1) )
That's dangerous because 'size_t' and 'int' are not necessarily the
same size, which means not all values of 'size_t' can be represented
by an 'int'.

Why not simply do

const int size = sizeof(arr) / sizeof(arr[0]);

and be rid of the macro altogether?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #4

P: n/a
On 14 Mar, 13:48, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
yashwant pinge wrote:
#define size (int)(sizeof(arr)/sizeof(arr[0]))
OR
if( index < int(size - 1) )

That's dangerous because 'size_t' and 'int' are not necessarily the
same size, which means not all values of 'size_t' can be represented
by an 'int'.

Why not simply do

const int size = sizeof(arr) / sizeof(arr[0]);

and be rid of the macro altogether?
Did you mean

const size_t size = sizeof(arr) / sizeof(arr[0]);

or did I miss something?

Gavin Deane

Mar 14 '07 #5

P: n/a
Gavin Deane wrote:
On 14 Mar, 13:48, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>yashwant pinge wrote:
>>#define size (int)(sizeof(arr)/sizeof(arr[0]))
OR
if( index < int(size - 1) )

That's dangerous because 'size_t' and 'int' are not necessarily the
same size, which means not all values of 'size_t' can be represented
by an 'int'.

Why not simply do

const int size = sizeof(arr) / sizeof(arr[0]);

and be rid of the macro altogether?

Did you mean

const size_t size = sizeof(arr) / sizeof(arr[0]);

or did I miss something?
I did mean 'int' because that's how the OP wanted this value to be
defined. The problem the OP described was due to the fact that some
other value was promoted to the type of size (instead of keeping it
the same). Begs the question why the language doesn't have some kind
of "protection against standard conversions/promotions". It could be
beneficial [in rare cases] to have (WARNING: not C++!)

auto size = sizeof(arr) / sizeof(arr[0]); // 'size' gets its type
// from the expression
....
int protected blah; // "protected" from promotions/conversions
....
if (blah < size) // requires promoting 'blah', flagged as error
....

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #6

P: n/a
Victor Bazarov wrote:
Actually, yes. 'sizeof' "returns" size_t, which is unsigned in most
implementations
It must be some unsigned integral type in ALL implementations.
Mar 15 '07 #7

P: n/a
yashwant pinge wrote:
>
The disadvantage of macro is that it doesnot follow the strict type
checking
A macro that defines an integral constant has a well defined type, just
as any other form of integral constant does.
>
To run the code, write the statement as

#define size (int)(sizeof(arr)/sizeof(arr[0]))
OR
if( index < int(size - 1) )
That has nothing to do with the fact that size is a macro. It's simply
because the type of size is size_t. The same problem would occur if size
were explicitly defined to have type size_t:

size_t size = sizeof(arr)/sizeof(arr[0]);

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 20 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.