Problem i C

C, C++, Pascal, Assembly, Raspberry, Java, Matlab, Python, BASIC, SQL, PHP, etc.
blueint
Inlägg: 23238
Blev medlem: 4 juli 2006, 19:26:11
Kontakt:

Re: Problem i C

Inlägg av blueint »

OT: datan = 0xFUL;

:D
Användarvisningsbild
sodjan
EF Sponsor
Inlägg: 43178
Blev medlem: 10 maj 2005, 16:29:20
Ort: Söderköping
Kontakt:

Re: Problem i C

Inlägg av sodjan »

Eller 0xFULL (finns det inte en "unsigned long long" ?). :-)

Hur som helst, problemet tycks vara löst. Var helt enkelt
tydligare i koden så att kompilatorn inte missförstår... :-)
Användarvisningsbild
kimmen
Inlägg: 2042
Blev medlem: 25 augusti 2007, 16:53:51
Ort: Stockholm (Kista)

Re: Problem i C

Inlägg av kimmen »

Literalen 0xFF blir av typen (signed) int. (standarden garanterar detta) 0xFFU blir unsigned int på samma sätt.

Men int:s omfång är enligt standarden inte garanterat till mer än +-0x7FFF så tal utanför det omfånget skrivna som literaler får olika typ beroende på plattform. Man får i princip den minsta typen som rymmer talet (men aldrig mindre än int).
svanted
Inlägg: 5084
Blev medlem: 30 augusti 2010, 21:20:38
Ort: Umeå

Re: Problem i C

Inlägg av svanted »

btw....
nedan belyser en sida av problemet...

Kod: Markera allt

#include <stdio.h>
void main( void )
{
        printf("-4278190080 = 0x%x\n", -4278190080);
        printf("16777216 = 0x%x\n", 16777216);

}

-4278190080 = 0x1000000
16777216 = 0x1000000
Användarvisningsbild
sodjan
EF Sponsor
Inlägg: 43178
Blev medlem: 10 maj 2005, 16:29:20
Ort: Söderköping
Kontakt:

Re: Problem i C

Inlägg av sodjan »

Var tvungen att prova med min kompilator... :-)

Kod: Markera allt

$ cc mult5

        printf("-4278190080 = 0x%x\n", -4278190080);
.......................................^
%CC-W-OUTTYPELEN, In this statement, this argument to printf is of "a signed long long" type and is not
appropriate for the conversion specifier "%x". The value might be truncated or formatted in an unintended manner.
at line number 4 in file USER:[JANNE]MULT5.C;1
$ 
$ link mult5
%LINK-W-WRNERS, compilation warnings
        in module MULT5 file USER:[JANNE]MULT5.OBJ;2
$ 
$ run mult5
-4278190080 = 0x1000000
16777216 = 0x1000000
Användarvisningsbild
arvidb
Inlägg: 4537
Blev medlem: 8 maj 2004, 12:56:24
Ort: Stockholm

Re: Problem i C

Inlägg av arvidb »

Var också tvungen att testa. :)

Kod: Markera allt

$ gcc --version
gcc (Gentoo 4.7.3-r1 p1.3, pie-0.5.5) 4.7.3

$ gcc -Wall mult5.c -o mult5
mult5.c:2:6: warning: return type of 'main' is not 'int' [-Wmain]
mult5.c: In function 'main':
mult5.c:4:9: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'long int' [-Wformat]
gcc varnar om formatet (%x) även utan -Wall.
Användarvisningsbild
sodjan
EF Sponsor
Inlägg: 43178
Blev medlem: 10 maj 2005, 16:29:20
Ort: Söderköping
Kontakt:

Re: Problem i C

Inlägg av sodjan »

Hm, jag tycker kanske att gcc är lite fåordig där... :-)
Jag gillar "User Action" på den första varningen...

Kod: Markera allt

$ cc mult5/warn=(enab=level6,verbose)

        printf("-4278190080 = 0x%x\n", -4278190080);
........................................^
%CC-I-INTCONSTSIGNED, This integer constant value will be given the type long long int.  This is compatible
with the C99 standard. Older versions of the compiler would have given this unsigned long int type.
at line number 4 in file USER:[JANNE]MULT5.C;1
Description: With the introduction of the long long int type, the C99 standard changed the rules for how the
type of certain integer constants are determined. Unsuffixed decimal constants which are too large for long int,
but could fit in an unsigned long int are given the type long long int in C99. Prior to C99 these would be given
unsigned long int type. 
User Action: Be aware of this difference. 

void main( void )
^
%CC-I-MAINNOTINT, Strict standard C extension: The declaration of the "main" function has a return type
other than int.
at line number 2 in file USER:[JANNE]MULT5.C;1
Description: Standard C requires that the "main" function be defined with a return type of int.  HP C will
accept other return types, but the program does not conform to the C standard.  The status value returned
to the environment may not be what you expect, and other C compilers may not accept the definition as written. 
User Action: Define the "main" function with a return type of int for maximal portability. 

        printf("-4278190080 = 0x%x\n", -4278190080);
.......................................^
%CC-W-OUTTYPELEN, In this statement, this argument to printf is of "a signed long long" type and is not
appropriate for the conversion specifier "%x".  The value might be truncated or formatted in an unintended manner.
at line number 4 in file USER:[JANNE]MULT5.C;1
Description: The compiler has detected an output conversion specifier that does not match its corresponding
argument. This might not have been what you intended. 
User Action: Modify either the argument or the conversion specifier so that they match. 

        printf("-4278190080 = 0x%x\n", -4278190080);
........^
%CC-I-IGNORECALLVAL, In this statement, the value returned from the function "printf(...)" is not used - if
this is intended, it should be cast to "void".
at line number 4 in file USER:[JANNE]MULT5.C;1
Description: A function that returns a value has been invoked, yet the value was not used. This might not
have been what you intended. 
User Action: Cast the function to void to suppress the message. 
Användarvisningsbild
stekern
Inlägg: 453
Blev medlem: 2 november 2008, 08:24:18
Ort: Esbo, Finland

Re: Problem i C

Inlägg av stekern »

sodjan skrev:Hm, jag tycker kanske att gcc är lite fåordig där... :-)
Inte till den milda grad som din pratkvarn till kompilator, men... ;)
gcc har blivit mer informativ i senare versioner (en konsekvens av 'konkurrensen' med clang/llvm).

Kod: Markera allt

$ or1k-elf-gcc --version
or1k-elf-gcc (GCC) 4.9.0

$ or1k-elf-gcc mult5.c -o mult5
mult5.c: In function ‘main’:
mult5.c:4:3: warning: this decimal constant is unsigned only in ISO C90
   printf("-4278190080 = 0x%x\n", -4278190080);
Skriv svar