Forum: PC-Programmierung C: conversion rules, kennt jemand ein Beispiel dazu?


von Daniel (Gast)


Lesenswert?

Hi,

die Regeln ...
1
    1)If both operands have the same type, then no further
2
      conversion is needed.
3
4
    2)Otherwise, if both operands have signed integer types or both
5
      have unsigned integer types, the operand with the type of
6
      lesser integer conversion rank is converted to the type of
7
      the operand with greater rank.
8
9
    3)Otherwise, if the operand that has unsigned integer type has
10
      rank greater or equal to the rank of the type of the other
11
      operand, then the operand with signed integer type is converted
12
      to the type of the operand with unsigned integer type.
13
14
    4)Otherwise, if the type of the operand with signed integer type
15
      can represent all of the values of the type of the operand with
16
      unsigned integer type, then the operand with unsigned integer type
17
      is converted to the type of the operand with signed integer type.
18
19
    5)Otherwise, both operands are converted to the unsigned integer
20
      type corresponding to the type of the operand with signed
21
      integer type.


1,2,3 sind mir bekannt
4 kenne ich nicht, und kann mir kein Beispiel konstruiren.
5 ebenso

Kann jemand Beispiele für 4 und 5 geben?

Grüße,
Daniel

von Karl H. (kbuchegg)


Lesenswert?

Daniel schrieb:

>     4)Otherwise, if the type of the operand with signed integer type
>       can represent all of the values of the type of the operand with
>       unsigned integer type, then the operand with unsigned integer type
>       is converted to the type of the operand with signed integer type.
>

> 4 kenne ich nicht, und kann mir kein Beispiel konstruiren.

Annahme:
  sizeof( unsigned char ) == 1
  sizeof( int ) == 2

  unsigned char c = 200;
  int i;

  i = c;


i ist signed und seine sizeof ist gross genug, so dass es alle Werte, 
die mit einem unsigned char möglich sind, darstellen kann.

von Daniel (Gast)


Lesenswert?

Hallo Karl,

hatte Brett vor dem Kopf :)

d.h. aber auch, dass im Code (-1LL < 1UL) immer true ist, wenn
sizeof(long long) > sizeof(unsigned long), wohingegen
(-1LL < 1LL) unabhängig von Bitbreite der Typen immer false ist.

Gruß,
Daniel

von Karl H. (kbuchegg)


Lesenswert?

>     5)Otherwise, both operands are converted to the unsigned integer
>       type corresponding to the type of the operand with signed
>       integer type.


Das müsste den Fall regeln

   sizeof( unsigned int ) == 4
   sizeof( long ) == 4

   unsigned int i = 200;
   long j = -200;

   if( i < j )


Der 'rank' von long ist höher als der von int, auch wenn beide gleiche 
sizeof haben. Daher greift Regel 3 nicht. Regel 4 greift ebenfalls 
nicht, da durch die sizeof klar ist, dass ein long eben nicht alle mit 
einem unsigned int darstellbaren Werte darstellen kann.

Regel 5 besagt aber in so einem Fall, dass der unsigned int zu einem 
unsigned long konvertiert wird, und das das long zu einem unsigned long 
konvertiert wird. Der Vergleich im Beispiel wird also als unsigned long 
Vergleich durchgeführt.

Im Endeffekt läuft es bei (fast) allen Regeln darauf hinaus: Treffen 
signed und unsigned aufeinander, dann 'gewinnt' unsigned. Es sei denn 
der signed Wert hat eine groß genuge sizeof, so dass alle Werte des 
unsigned damit darstellbar sind.

von Karl H. (kbuchegg)


Lesenswert?

Daniel schrieb:

> d.h. aber auch, dass im Code (-1LL < 1UL) immer true ist, wenn
> sizeof(long long) > sizeof(unsigned long), wohingegen
> (-1LL < 1LL) unabhängig von Bitbreite der Typen immer false ist.

Ich würde sagen: korrekt in beiden Fällen.

von Daniel (Gast)


Lesenswert?

Danke nochmal Karl.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.