C: conversion rules, kennt jemand ein Beispiel dazu?

Gast #2563979
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
#2563981
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.
Gast #2563984
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
#2563985
>     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.

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 14 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.