is order urgent doubt

N

new to c

Hi!

I write the 2 codes

int i;
i = sizeof(long int);
printf("%i", i);

i = sizeof(int long);
printf("%i", i);

and the first code and second code print 4.

I write another 2 codes

i = sizeof(double int);
printf("%i", i);

i = sizeof(int double);
printf("%i", i);

and the first code print 4 and the second code print 8.

My doubt is why is different number? Is order urgent?
 
K

Keith Thompson

new to c said:
I write the 2 codes

int i;
i = sizeof(long int);
printf("%i", i);

i = sizeof(int long);
printf("%i", i);

and the first code and second code print 4.

Right. It won't necessarily be 4 on all systems, but both are
equivalent.

However, "int long" is very non-idiomatic. The compiler can handle it
with no problem, but human readers are going to stumble over it.

(Please ignore the long flame war that will now begin claiming that
anyone who knows C should be able to read "int long" without any
trouble. The fact that the C standard allows variations in the order
of the keywords is fairly obscure; a member of the standard committee
recently posted here saying he didn't even know about it.)
I write another 2 codes

i = sizeof(double int);
printf("%i", i);

i = sizeof(int double);
printf("%i", i);

and the first code print 4 and the second code print 8.

That's surprising. Your compiler should have rejected it, or at least
warned you about it. Are you sure that's *exactly* what you wrote?

[...]
 
T

teapot

Eric said:
I'm surprised you got any output at all, because neither
`double int' nor `int double' is a valid type name. You
should have received error messages from the compiler.


Not in C, but your compiler seems to be processing some
other language.

It appears to be pseudo legal in <unnamed compiler>.

In case OP is using said compiler, here are some quick observations --

(1) if `signed' and `unsigned' appear in the same declaration, only
the last one specified affects signedness.

[Based on earlier observations, <unnamed compiler> accepts
`signed main()' but rejects `unsigned main()'. Tests show that
`unsigned signed main()' is accepted but `signed unsigned main()'
is not, from that we can conclude that later modifiers affecting
signedness probably overrule earlier ones.]


(2) if more than one base type appears in a declaration, sometimes,
later conflicting types overrule earlier ones.

[The test method here uses the fact that doing something illegal
(assignment to object of incompatible type) produces a diagnostic
that reveals the actual type of the created object. Consider this
code snippet

void *foo;
double int bar; /* line 5 */
int double baz; /* line 6 */
foo = bar; /* line 7 */
foo = baz; /* line 8 */

5: W: multiple use of 'int'
6: W: multiple use of 'double'
7: E: operands of = have illegal types 'pointer to void' and 'int'
8: E: operands of = have illegal types 'pointer to void' and 'double'

From that we can conclude that `bar' probably has type `int' and
`baz' probaby has type `double'.

Using the same procedure it turns out that `long long double' is
seen as plain `double', but `long double int' is `long int'.
`long short' is `short' and `short long' is `long', `long short long'
is `long' and `short long short' is `short' -- the same rules
affecting signedness discovered in (1) probably also apply when
deciding short or longness.

There is, however, a small inconsistency in that `short short short'
is probably `short' but `long long long' is in category (3)]


(3) other combinations are flagged as errors and compilation stops --

[Same method used as before

void *foo;
unsigned double bar; /* line 5 */
double unsigned baz; /* line 6 */
foo = bar; /* line 7 */
foo = baz; /* line 8 */

5: E: invalid type specification
6: E: invalid type specification


In conclusion, <unnamed compiler> treats some illegal combinations
of types/types and types/modifiers as errors, others merely as
warnings; but they are warned about even without any flags
requesting extra warnings and <unnamed compiler> appears to be
conforming in these regards.

Answering the original question, `double int' is not a valid
type, but some implementations, in certain cases, appear to pick
one type if more than one is given. In <unnamed compiler>,

`double int' is seen as `int',

and

`int double' is seen as `double',

in both cases after issuing the required diagnostic (warning).
 
N

new to c

Keith said:
Right. It won't necessarily be 4 on all systems, but both are
equivalent.

However, "int long" is very non-idiomatic. The compiler can handle it
with no problem, but human readers are going to stumble over it.

(Please ignore the long flame war that will now begin claiming that
anyone who knows C should be able to read "int long" without any
trouble. The fact that the C standard allows variations in the order
of the keywords is fairly obscure; a member of the standard committee
recently posted here saying he didn't even know about it.)


That's surprising. Your compiler should have rejected it, or at least
warned you about it. Are you sure that's *exactly* what you wrote?

He warn "multiple use of int" on first code and "multiple use of double"
on second code but I think is bogus......only one int in first code and
only one double in second code.

I write big international program and need double integer. Why size is
different when other order?
 
B

Bartc

I write big international program and need double integer. Why size is
different when other order?

Try: long long int

If you're lucky, this will have size 8.

In C, 'double' is just a bigger version of 'float'
 
N

new to c

Bartc said:
Try: long long int

If you're lucky, this will have size 8.

Problem equal.

int i;
i = sizeof(long long int);
printf("%i", i);

i = sizeof(long int long);
printf("%i", i);

i = sizeof(int long long);
printf("%i", i);

First code warn nothing and print 8.

Second code warn "multiple use of 'long'" and print 4.

Third code warm "multiple use of 'longlong'" and print 8.

Why is different number? Is order urgent?
 
S

santosh

new said:
Problem equal.

int i;
i = sizeof(long long int);
printf("%i", i);

i = sizeof(long int long);
printf("%i", i);

i = sizeof(int long long);
printf("%i", i);

First code warn nothing and print 8.
Second code warn "multiple use of 'long'" and print 4.
Third code warm "multiple use of 'longlong'" and print 8.

Why is different number? Is order urgent?

As far as I can see, you shouldn't have got any of those warnings.
 
K

Keith Thompson

new to c said:
Problem equal.

You mean "the same problem".
int i;
i = sizeof(long long int);
printf("%i", i);

i = sizeof(long int long);
printf("%i", i);

i = sizeof(int long long);
printf("%i", i);

First code warn nothing and print 8.

Second code warn "multiple use of 'long'" and print 4.

Third code warm "multiple use of 'longlong'" and print 8.

Why is different number? Is order urgent?

You mean "important" or "significant", not "urgent".

Assuming the code you posted is the code you actually compiled and
ran, it appears that your compiler is buggy. What compiler are you
using?

Please post a small complete program that exhibits the problem.
Copy-and-paste the entire exact program; don't re-type it or summarize
it. Show us the compiler's warnings and the program's output
(copy-and-paste them as well).

Change each "%i" to "%i\n" (or "%d\n", which is equivalent but more
common).

Not all compilers support "long long", but most do (the feature was
added in C99, but a lot of pre-C99 compilers support it as an
extension).

All three chunks of code are exactly equivalent. For an integer type,
the order of the keywords is not significant; all of "long long int",
"long int long", and "int long long" mean exactly the same thing. If
your compiler is treating them differently, then there's a bug in your
compiler. (I think we've already told you this.)

As a matter of programming style, messing around with the order of the
keywords isn't a good idea. A properly working compiler won't care,
but using an order other than the usual one will just make your code
(slightly) harder to read, with no real benefit. (It's been argued
that using, for example, "long unsigned" rather than "unsigned long"
helps to remind you to use "%lu" rather than "%ul" for printf; I don't
find that argument convincing.)

Also, if you use the more common orderings, it's likely you can avoid
triggering your compiler's bugs (though I'd recommend finding a
compiler that works properly).
 
B

Bartc

Keith Thompson said:
You mean "the same problem".


You mean "important" or "significant", not "urgent".

Assuming the code you posted is the code you actually compiled and
ran, it appears that your compiler is buggy. What compiler are you
using?

Please post a small complete program that exhibits the problem.
Copy-and-paste the entire exact program; don't re-type it or summarize
it. Show us the compiler's warnings and the program's output
(copy-and-paste them as well).

I tried his code with 4 compilers. Three worked as expected (supporting long
long in any order and giving a size of 8). The fourth (lccwin) didn't seem
to
like 'long' following 'int'. (I don't know what compiler the OP used)
 
N

new to c

Keith said:
You mean "the same problem".


You mean "important" or "significant", not "urgent".

Assuming the code you posted is the code you actually compiled and
ran, it appears that your compiler is buggy. What compiler are you
using?

Please post a small complete program that exhibits the problem.
Copy-and-paste the entire exact program; don't re-type it or summarize
it. Show us the compiler's warnings and the program's output
(copy-and-paste them as well).

Change each "%i" to "%i\n" (or "%d\n", which is equivalent but more
common).

Not all compilers support "long long", but most do (the feature was
added in C99, but a lot of pre-C99 compilers support it as an
extension).

All three chunks of code are exactly equivalent. For an integer type,
the order of the keywords is not significant; all of "long long int",
"long int long", and "int long long" mean exactly the same thing. If
your compiler is treating them differently, then there's a bug in your
compiler. (I think we've already told you this.)

As a matter of programming style, messing around with the order of the
keywords isn't a good idea. A properly working compiler won't care,
but using an order other than the usual one will just make your code
(slightly) harder to read, with no real benefit. (It's been argued
that using, for example, "long unsigned" rather than "unsigned long"
helps to remind you to use "%lu" rather than "%ul" for printf; I don't
find that argument convincing.)

Also, if you use the more common orderings, it's likely you can avoid
triggering your compiler's bugs (though I'd recommend finding a
compiler that works properly).

I use wedit IDE.

I write this code :

#include <stdio.h>
int main(void)
{
int i;

i = sizeof(long int);
printf("sizeof(long int): %d\n", i);

i = sizeof(int long);
printf("sizeof(int long): %d\n", i);

i = sizeof(double int);
printf("sizeof(double int): %d\n", i);

i = sizeof(int double);
printf("sizeof(int double): %d\n", i);

i = sizeof(long long int);
printf("sizeof(long long int): %d\n", i);

i = sizeof(long int long);
printf("sizeof(long int long): %d\n", i);

i = sizeof(int long long);
printf("sizeof(int long long): %d\n", i);

return 0;
}

Compiler warn

Warning c:\New_Folder\Test.c: 3 old-style definition for 'main'
Warning c:\New_Folder\Test.c: 3 missing prototype for 'main'
Warning c:\New_Folder\Test.c: 12 multiple use of 'int'
Warning c:\New_Folder\Test.c: 15 multiple use of 'double'
Warning c:\New_Folder\Test.c: 21 multiple use of 'long'
Warning c:\New_Folder\Test.c: 24 multiple use of 'longlong'
Compilation + link time 24.2 sec, Return code: 0

Code print

sizeof(long int): 4
sizeof(int long): 4
sizeof(double int): 4
sizeof(int double): 8
sizeof(long long int): 8
sizeof(long int long): 4
sizeof(int long long): 8
 
K

Keith Thompson

new to c said:
Keith Thompson wrote: [...]
Please post a small complete program that exhibits the problem.
Copy-and-paste the entire exact program; don't re-type it or summarize
it. Show us the compiler's warnings and the program's output
(copy-and-paste them as well).
[...]
I use wedit IDE.

According to what I've been able to find from a quick Google search,
wedit is an IDE that can be configured to work with different
compilers. Which *compiler* are you using? (I don't recognize the
layout of the warning messages.)
I write this code :

#include <stdio.h>
int main(void)
{
int i;

i = sizeof(long int);
printf("sizeof(long int): %d\n", i);

i = sizeof(int long);
printf("sizeof(int long): %d\n", i);

i = sizeof(double int);
printf("sizeof(double int): %d\n", i);

i = sizeof(int double);
printf("sizeof(int double): %d\n", i);

i = sizeof(long long int);
printf("sizeof(long long int): %d\n", i);

i = sizeof(long int long);
printf("sizeof(long int long): %d\n", i);

i = sizeof(int long long);
printf("sizeof(int long long): %d\n", i);

return 0;
}

Compiler warn

Warning c:\New_Folder\Test.c: 3 old-style definition for 'main'
Warning c:\New_Folder\Test.c: 3 missing prototype for 'main'

That's just plain wrong. "int main(void)" is a prototype, not an
old-style definition. I'm quite surprised that any compiler would get
this wrong. Are you *sure* that the code you posted is the same as
the code the compiler is seeing? Maybe the IDE is causing confusion
about the code in the editor vs. the code in the source file.

Try inserting a single blank line at the very top of the file and
recompiling. It should change the line numbers in the warning
messages and have no other effect. If you do this experiment, don't
bother posting the full results; just let us know whether it behaves
as I described. If so, it tends to confirm that the compiler is
really seeing the code that you posted.
Warning c:\New_Folder\Test.c: 12 multiple use of 'int'

For "double int". A diagnostic is required, but the compiler is
confused.
Warning c:\New_Folder\Test.c: 15 multiple use of 'double'

For "int double". See above.
Warning c:\New_Folder\Test.c: 21 multiple use of 'long'

This is for "long int long". It would be reasonable if it were a C90
compiler that doesn't accept long long, but it didn't complain about
"long long int" 3 lines up.
Warning c:\New_Folder\Test.c: 24 multiple use of 'longlong'

And that's just silly. Perhaps "longlong" is some internal name that
leaked out into the warning message.

[snip]

It looks like you've got a seriously buggy compiler on your hands,
though it might be able to handle code that's carefully written to
avoid its shortcomings. We still don't know which compiler it is. If
you can find out, see if there's a newer version.
 
B

Ben Bacarisse

Keith Thompson said:
Which *compiler* are you using? (I don't recognize the
layout of the warning messages.)

It is lcc-win32. At least, this is something I've seen lcc-win32 get
wrong and error message look the same as the ones I see from
lcc-win32.
 
I

Ike Naar

I use wedit IDE.

I write this code :

#include <stdio.h>
int main(void)
{
int i;

i = sizeof(long int);
printf("sizeof(long int): %d\n", i);

i = sizeof(int long);
printf("sizeof(int long): %d\n", i);

i = sizeof(double int);
printf("sizeof(double int): %d\n", i);

i = sizeof(int double);
printf("sizeof(int double): %d\n", i);

i = sizeof(long long int);
printf("sizeof(long long int): %d\n", i);

i = sizeof(long int long);
printf("sizeof(long int long): %d\n", i);

i = sizeof(int long long);
printf("sizeof(int long long): %d\n", i);

return 0;
}

Compiler warn

Warning c:\New_Folder\Test.c: 3 old-style definition for 'main'
Warning c:\New_Folder\Test.c: 3 missing prototype for 'main'
Warning c:\New_Folder\Test.c: 12 multiple use of 'int'
Warning c:\New_Folder\Test.c: 15 multiple use of 'double'
Warning c:\New_Folder\Test.c: 21 multiple use of 'long'
Warning c:\New_Folder\Test.c: 24 multiple use of 'longlong'
Compilation + link time 24.2 sec, Return code: 0

Does it really take 24 seconds to compile such a tiny program?
Are you using very old equipment, and perhaps a very old (pre-ANSI) compiler?

Regards,
Ike
 
J

jacob navia

new said:
I use wedit IDE.

I write this code :

#include <stdio.h>
int main(void)
{
int i;

i = sizeof(long int);
printf("sizeof(long int): %d\n", i);

i = sizeof(int long);
printf("sizeof(int long): %d\n", i);

i = sizeof(double int);
printf("sizeof(double int): %d\n", i);

i = sizeof(int double);
printf("sizeof(int double): %d\n", i);

i = sizeof(long long int);
printf("sizeof(long long int): %d\n", i);

i = sizeof(long int long);
printf("sizeof(long int long): %d\n", i);

i = sizeof(int long long);
printf("sizeof(int long long): %d\n", i);

return 0;
}

Compiler warn

Warning c:\New_Folder\Test.c: 3 old-style definition for 'main'
Warning c:\New_Folder\Test.c: 3 missing prototype for 'main'
Warning c:\New_Folder\Test.c: 12 multiple use of 'int'
Warning c:\New_Folder\Test.c: 15 multiple use of 'double'
Warning c:\New_Folder\Test.c: 21 multiple use of 'long'
Warning c:\New_Folder\Test.c: 24 multiple use of 'longlong'
Compilation + link time 24.2 sec, Return code: 0

Code print

sizeof(long int): 4
sizeof(int long): 4
sizeof(double int): 4
sizeof(int double): 8
sizeof(long long int): 8
sizeof(long int long): 4
sizeof(int long long): 8

Hi.

I am the author of lcc-win. I have changed the warning messages to a
clearer one. Now your program produces the following warnings:
Warning td.c: 12 multiple types in a declaration. Last will be used: 'int'
Warning td.c: 15 multiple types in a declaration. Last will be used:
'double'
Warning td.c: 21 multiple types in a declaration. Last will be used: 'long'
Warning td.c: 24 multiple types in a declaration. Last will be used:
'longlong'

Maybe this will clarify what lcc-win is doing.
 
K

Keith Thompson

jacob navia said:
new to c wrote: [...]
I use wedit IDE.
I write this code :
#include <stdio.h>
int main(void)
{
int i;
i = sizeof(long int);
printf("sizeof(long int): %d\n", i);
i = sizeof(int long);
printf("sizeof(int long): %d\n", i);
i = sizeof(double int);
printf("sizeof(double int): %d\n", i);
i = sizeof(int double);
printf("sizeof(int double): %d\n", i);
i = sizeof(long long int);
printf("sizeof(long long int): %d\n", i);
i = sizeof(long int long);
printf("sizeof(long int long): %d\n", i);
i = sizeof(int long long);
printf("sizeof(int long long): %d\n", i);
return 0;
}
Compiler warn
Warning c:\New_Folder\Test.c: 3 old-style definition for 'main'
Warning c:\New_Folder\Test.c: 3 missing prototype for 'main'
Warning c:\New_Folder\Test.c: 12 multiple use of 'int'
Warning c:\New_Folder\Test.c: 15 multiple use of 'double'
Warning c:\New_Folder\Test.c: 21 multiple use of 'long'
Warning c:\New_Folder\Test.c: 24 multiple use of 'longlong'
Compilation + link time 24.2 sec, Return code: 0
Code print
sizeof(long int): 4
sizeof(int long): 4
sizeof(double int): 4
sizeof(int double): 8
sizeof(long long int): 8
sizeof(long int long): 4
sizeof(int long long): 8

I am the author of lcc-win. I have changed the warning messages to a
clearer one. Now your program produces the following warnings:
Warning td.c: 12 multiple types in a declaration. Last will be used: 'int'
Warning td.c: 15 multiple types in a declaration. Last will be used:
'double'
Warning td.c: 21 multiple types in a declaration. Last will be used: 'long'
Warning td.c: 24 multiple types in a declaration. Last will be used:
'longlong'

Maybe this will clarify what lcc-win is doing.

lcc-win appears to meet the standard's requirement of issuing at
least one diagnostic for any translation unit containing a constraint
violation or syntax error.

Some questions, though:

1. Why not make "double int" and "int double" fatal errors?
IMHO you're not doing the user any favors by accepting these
and arbitrarily guessing what was meant.

2. Why do you warn about "long int long" and "int long long", both
of which are perfectly legal? I consider them both poor style,
but that's not what the warnings say.

3. Why does one of your warnings refer to "longlong" (presumably
something internal to the compiler)?

4. Do you still treat "long int long" as "long"? If so, that's
just a bug; "long int long" is equivalent to "long long".
 
F

Flash Gordon

jacob navia wrote, On 03/06/08 07:21:
new to c wrote:

<snip>

Jacob, in addition to Keith's questions did you notice that the above is
wrong? As "long int long" is the same as "long long" the above should be 8.

Maybe this will clarify what lcc-win is doing.

Those warnings made it clearer in my opinion, but I agree with Keith's
comments.
 
J

jacob navia

Flash said:
jacob navia wrote, On 03/06/08 07:21:

<snip>

Jacob, in addition to Keith's questions did you notice that the above is
wrong? As "long int long" is the same as "long long" the above should be 8.

6.7.2 Type specifiers
Syntax
1 type-specifier:
void char short int long float double signed unsigned _Bool _Complex
_Imaginary struct-or-union-specifier enum-specifier typedef-name

Constraints
2 At least one type specifier shall be given in the declaration
specifiers in each declaration, and in the specifier-qualifier list in
each struct declaration and type name. Each list of type specifiers
shall be one of the following sets (delimited by commas, when there is
more than one set on a line); the type specifiers may occur in any
order, possibly
intermixed with the other declaration specifiers.
— void
— char
— signed char
— unsigned char
— short, signed short, short int, or signed short int
— unsigned short, or unsigned short int
— int, signed, or signed int
— unsigned, or unsigned int
— long, signed long, long int, or signed long int
— unsigned long, or unsigned long int
— long long, signed long long, long long int, or
signed long long int
— unsigned long long, or unsigned long long int
— float
— double
— long double
— _Bool
— float _Complex
— double _Complex
— long double _Complex
— float _Imaginary
— double _Imaginary
— long double _Imaginary
— struct or union specifier
— enum specifier
— typedef name

OK, then , I see:
— long long, signed long long, long long int, or signed long long int
— unsigned long long, or unsigned long long int

as legal declarations for long long variants.

long int long is a double declaration:
long int, and then long.
Those warnings made it clearer in my opinion, but I agree with Keith's
comments.

??? I do not see any message from Keith in this thread.
Could you clarify?
 
W

Walter Roberson

6.7.2 Type specifiers
Each list of type specifiers
shall be one of the following sets (delimited by commas, when there is
more than one set on a line); the type specifiers may occur in any
order, possibly
intermixed with the other declaration specifiers.

Notice that bit about "in any order". So long long int is the
same as long int long .
 
J

jacob navia

Walter said:
Notice that bit about "in any order". So long long int is the
same as long int long .

And why is not long int, then long?

What about

int int int m;

And any kind of nonsense you can think about?

long signed long int,
signed long int long,
signed long signed long,
signed int long signed long int
long int long int
long int int long

ad nauseum?

I just use the last one parsed.
 
A

Antoninus Twink

??? I do not see any message from Keith in this thread.
Could you clarify?

I believe Keith's news provider is under a UDP for flooding Usenet with
SPAM, so your news server may not be carrying his messages. Whether
that's a bad thing or not is for you to decide...

(I think he switched to aioe for a while, but gave up when they went
down for a couple of days - they're back now.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top