473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

what is the type of a #define const number

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
7 2390
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: johny smith | last post by:
I have never really understood the difference between 1.) #define NUMBER 1.653 vs. 2.) const double NUMBER = 1.653 I know that the #define is taken care of by the pre-processor and the...
9
by: deanfamily11 | last post by:
I have an ADT and I'd like to search for certain items in it. How can I accomplish this?
7
by: Peng Yu | last post by:
I'm not very clear about when to use #define and when to use const? #define number 4 const int number = 4; If I just want to define a global constant, which way of the above is better? ...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
6
by: Joseph Turian | last post by:
I want to create several types of double, e.g. D1 and D2. I want strong typing, such that a function defined for D1 will not accept a D2 argument. One solution is: struct D1 { double d; }; ...
13
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
2
by: Robert | last post by:
dear sir: I have read the c++ primer plus 5 book, and do the chapter 12 programming exercises question 5, but the average_wait have negative result. I don't understand why? here is the code,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.