IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

taille des types en C


Sujet :

C

  1. #1
    Membre éprouvé
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 821
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 821
    Points : 979
    Points
    979
    Par défaut taille des types en C
    Bonjour,

    Je programme sur des µControlleurs 8 et 32 bits et je viens de m'apercevoir que le type "unigned int" est de 2 octets sur le µC 8bits et 4 octets sur le µC 32bits : que dit la norme du C ? est-ce normal de ne pas avoir la même taille ? est-ce qu'il y a une règle qui permet de connaitre la taille des types facilement (ça dépend de quoi ?) ?

    merci d'avance,

  2. #2
    Membre régulier
    Inscrit en
    Juin 2008
    Messages
    91
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 91
    Points : 115
    Points
    115
    Par défaut
    Citation Envoyé par boboss123 Voir le message
    Je programme sur des µControlleurs 8 et 32 bits et je viens de m'apercevoir que le type "unigned int" est de 2 octets sur le µC 8bits et 4 octets sur le µC 32bits : que dit la norme du C ? est-ce normal de ne pas avoir la même taille ? est-ce qu'il y a une règle qui permet de connaitre la taille des types facilement (ça dépend de quoi ?) ?
    Bonjour,

    La doc du compilateur pour commencer.
    Pour ce type d'applications, le standard n'est pas appliqué en grande partie.
    Donc tes manuels de survie sont la doc du compilateur ainsi que la doc du composant.

  3. #3
    Membre éclairé Avatar de AuraHxC
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2006
    Messages
    652
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2006
    Messages : 652
    Points : 683
    Points
    683
    Par défaut
    Ce qui me chiffonne mais après je peux me tromper, c'est le fait que tes uint soit de 2 octets sur un 8 bits.
    Ça serait pas 2 octets sur 16 bits et 4 octets sur 32 bits ? ce qui me paraît assez logique.

  4. #4
    Membre émérite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2008
    Messages
    1 515
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 515
    Points : 2 505
    Points
    2 505
    Par défaut
    sizeof te donne la taille du type en nombre de char, et CHAR_BIT (dans limits.h) te donne la taille d'un char en nombre de bits.

  5. #5
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Septembre 2007
    Messages
    7 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 369
    Points : 23 623
    Points
    23 623
    Par défaut
    Selon Wikipédia, la règle serait la suivante (à prendre avec des pincettes car je ne retrouve pas le ou les paragraphes exacts dans la norme qui y font référence) :

    Novice C programmers may be confused as to how big these types are. The standard is specifically vague in this area:

    * sizeof(short int) ≤ sizeof(int) ≤ sizeof(long int)
    * short int must be at least 16 bits long.
    * long int must be at least 32 bits long.
    * long long int must be at least 64 bits long.

    The standard does not require that any of these sizes be necessarily different. It is perfectly valid, for example, if all four types are 64 bits long. In order to allow a simple and concise description of the sizes a compiler will apply to each of the four types[…]

    Et par ailleurs :

    A ‘‘plain’’ int object has the natural size suggested by the
    architecture of the execution environment (large enough to contain any value in the range
    INT_MIN to INT_MAX as defined in the header <limits.h>).
    Et dans <limits.h> :

    — minimum value for an object of type int
    -32767 // −(215 − 1)
    INT_MIN
    — maximum value for an object of type int
    +32767 // 215 − 1
    INT_MAX
    — maximum value for an object of type unsigned int
    65535 // 216 − 1
    UINT_MAX
    Donc, en gros, un short ≤ un int ≤ un long int ≤ un long long int, mais rien n'empêche de tous les définir sur 64 bits, par exemple. Un int doit a priori correspondre au format le plus « naturel » du micro-processeur, tant qu'il reste dans les limites (minimum et maximum) définies par limits.h.

    Ton micro-contrôleur utilise peut-être des registres 16 bits, et son bus d'adresse l'est peut-être aussi. De toutes façons, même si ce n'est pas le cas, le C impose quand même une taille minimum de 16 bits aux ints.

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 104
    Points : 1 750
    Points
    1 750
    Par défaut
    Selon Wikipédia, la règle serait la suivante (à prendre avec des pincettes car je ne retrouve pas le ou les paragraphes exacts dans la norme qui y font référence) :

    * short int must be at least 16 bits long.
    * long int must be at least 32 bits long.
    * long long int must be at least 64 bits long.
    Est-ce que ce ne serait pas ce passage ?

    5.2.4.2.1 Sizes of integral types <limits. h>

    The values given below shall be replaced by constant expressions suitable for use in #if
    preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following
    shall be replaced by expressions that have the same type as would an expression that is an object
    of the corresponding type converted according to the integral promotions. Their implementation-
    delined values shall be equal or greater in magnitude (absolute value) to those shown, with the
    same sign


    - number of bits for smallest object that is not a bit-field (byte)
    CHAR_BIT 8
    - minimum value for an object of type signed char
    SCHAR_MIN -127
    - maximum value for an object of type signed char
    SCHAR_MAX +127
    - maximum value for an object of type unsigned char
    UCHAR_MAX 255
    - minimum value for an object of type char
    CHAR_MIN see below
    - maximum value for an object of type char
    CHAR_MAX see below
    - maximum number of bytes in a multibyte character, for any supported locale
    MB_LEN_MAX 1
    - minimum value for an object of type short int
    SHRT_MIN -32767
    - maximum value for an object of type short int
    SHRT_MAX +32767
    - maximum value for an object of type unsigned short int
    USHRT_MAX 65535
    - minimum value for an object of type int
    INT_MIN -32767
    - maximum value for an object of type int
    INT_MAX +32767
    - maximum value for an object of type unsigned int
    UINT_MAX 65535
    - minimum value for an object of type long int
    LONG_MIN -2147483647
    - maximum value for an object of type long int
    LONG_MAX +2147483647
    - maximum value for an object of type unsigned long int
    ULONG_MAX 4294967295
    ~~~~

    Concernant
    sizeof(short int) ≤ sizeof(int) ≤ sizeof(long int)
    Là, je coince.

  7. #7
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    Citation Envoyé par Obsidian Voir le message
    De toutes façons, même si ce n'est pas le cas, le C impose quand même une taille minimum de 16 bits aux ints.
    ?? Ou ca ? Cela dépend entièrement de l'implementation...

    Extrait de la norme C

    Annex E
    (informative)

    Implementation limits

    1 The contents of the header <limits.h> are given below, in alphabetical order.
    The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign.
    The values shall all be constant expressions suitable for use in #if
    preprocessing directives. The components are described further in 5.2.4.2.1.

    #define CHAR_BIT 8
    #define CHAR_MAX UCHAR_MAX or SCHAR_MAX
    #define CHAR_MIN 0 or SCHAR_MIN
    #define INT_MAX +32767
    #define INT_MIN -32767
    #define LONG_MAX +2147483647
    #define LONG_MIN -2147483647
    #define LLONG_MAX +9223372036854775807
    #define LLONG_MIN -9223372036854775807
    #define MB_LEN_MAX 1
    #define SCHAR_MAX +127
    #define SCHAR_MIN -127
    #define SHRT_MAX +32767
    #define SHRT_MIN -32767
    #define UCHAR_MAX 255
    #define USHRT_MAX 65535
    #define UINT_MAX 65535
    #define ULONG_MAX 4294967295
    #define ULLONG_MAX 18446744073709551615
    [edit] avais pas vu le post de jeroman...
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  8. #8
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Citation Envoyé par Vincent Rogier Voir le message
    ?? Ou ca ? Cela dépend entièrement de l'implementation...
    Pour pouvoir garantir la plage [-32767, +32767], la taille est forcément supérieure ou égale à 16 bits.
    Mais effectivement, la norme n'impose pas de taille minimal, uniquement des plages de valeurs garanties.

  9. #9
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    Citation Envoyé par gl Voir le message
    Pour pouvoir garantir la plage [-32767, +32767], la taille est forcément supérieure ou égale à 16 bits.
    Mais effectivement, la norme n'impose pas de taille minimal, uniquement des plages de valeurs garanties.
    L'annexe E précise bien :

    The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign.


    Donc la plage n'est la plage [-32767, +32767] n'est pas garantie en fonction de l'implémentation...
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  10. #10
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    L'extrait présenté par Jeroman :
    5.2.4.2.1 Sizes of integral types <limits. h>
    ....
    Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign
    dit bien que la plage [-32767, +32767] est garantie pour un int. Fréquemment la borne minimum est plus grande en valeur absolue que la borne maximum ( ex :[-32768, 32767])

    Jeroman :
    Concernant
    sizeof(short int) <= sizeof(int) <= sizeof(long int)
    Là, je coince.
    C'est un résumé (qui suppose que sizeof donne le nombre de bytes qui participent à la valeur de la donnée) qui se déduit de ceci :
    6.2.5 Types
    ...
    8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.


    6.3.1.1 Boolean, characters, and integers
    Every integer type has an integer conversion rank defined as follows:
    — No two signed integer types shall have the same rank, even if they have the same representation.
    — The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.
    The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
    — The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
    .....
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  11. #11
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Citation Envoyé par Vincent Rogier Voir le message
    L'annexe E précise bien :

    The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign.
    L'annexe E n'est pas normatif.

    L'extrait fourni par jeroman est normatif et précise bien :

    Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign
    L'implémentation ne peut donc qu'agrandir la plage de valeur, pas la réduire.

    La plage [-32767, +32767] est donc bien la plage minimale garantie.

  12. #12
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    bon ben je vais jeter mon exemplaire de la norme (qui était un draft) à la poubelle...
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  13. #13
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Citation Envoyé par Vincent Rogier Voir le message
    bon ben je vais jeter mon exemplaire de la norme (qui était un draft) à la poubelle...
    Pourquoi ? Même dans les drafts assez anciens (n869), ce point était déjà décrit.

  14. #14
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    Citation Envoyé par gl Voir le message
    Pourquoi ? Même dans les drafts assez anciens (n869), ce point était déjà décrit.
    ooopss

    C'est moi qui n'est plus les yeux en face des trous..
    Mon exemplaire est un N1124 et effectivement le paragraphe 5.2.4.2.1 est bien le même....
    Je me demande pourquoi je suis allé directement à l'annexe E
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  15. #15
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Au passage, le dernier draft est le n1256 qui intègre en plus le TC3 .

  16. #16
    Membre éprouvé
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 821
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 821
    Points : 979
    Points
    979
    Par défaut
    donc si je veux avoir un code portable quelque soit le support, il faut que je considère les types avec les tailles décrites ci-dessous ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    char = 1 octet
    short = 2 octets
    int = 2 octets
    long = 4 octets
    long long = 8 octets
    => aussi, il faut donc faire attention que dans mes fonctions qu'il n'y ai jamais débordement lors des calculs. Par contre lors de décalages de bits, je ne risque pas d'avoir de problèmes en fonction de la taille réelle du type utilisé ?

    Quels sont les règles que vous vous fixez lorsque vous créez du code portable ?

  17. #17
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    non tu ne peux pas du tout présumer de la taille d'un integer (sauf char) , cela dépend de l'implémentation, de l'architecture...
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  18. #18
    Membre éclairé Avatar de valefor
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    711
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 711
    Points : 790
    Points
    790
    Par défaut
    Ceci dit, j'ai pris pour habitude de ne pas me soucier de la taille des types int standard.

    Je pars du principe qu'on dimensionne le type de processeur qu'on utilise au besoin, donc pour tous les calculs que je fais j'utilise un int ou un unsigned.

    Si je me soucie du nombre de bits, c'est souvent pour aligner une structure pour se mapper quelque part (en mémoire, sur une trame réseau, ...). Du coup j'utilise plutôt les types uint8_t, uint32_t (dans inttypes.h) etc.

    Ceci est mon point de vue et c'est certainement discutable.

  19. #19
    Membre chevronné
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 104
    Points : 1 750
    Points
    1 750
    Par défaut
    donc si je veux avoir un code portable quelque soit le support, il faut que je considère les types avec les tailles décrites ci-dessous ?
    Le problème, avec les compilateurs C pour µc, c'est le respect de la norme...

  20. #20
    Membre éprouvé
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 821
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 821
    Points : 979
    Points
    979
    Par défaut
    ok merci pour vos infos

    aussi j'ai un autre petit problème avec les types :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    // tourne sous µC 32 bits
    DWORD_VAL valTmp; // taille 4 octets
     
    ...
     
    printf("%02X%02X%02X%02X", valTmp.v[0], valTmp.v[1], valTmp.v[2], valTmp.v[3]);

    fichier de définition des types :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
     
     
    #ifndef __GENERIC_TYPE_DEFS_H_
    #define __GENERIC_TYPE_DEFS_H_
     
    /* Specify an extension for GCC based compilers */
    #if defined(__GNUC__)
    #define __EXTENSION __extension__
    #else
    #define __EXTENSION
    #endif
     
    #if !defined(__PACKED)
        #define __PACKED
    #endif
     
    /* get compiler defined type definitions (NULL, size_t, etc) */
    #include <stddef.h> 
     
    typedef enum _BOOL { FALSE = 0, TRUE } BOOL;    /* Undefined size */
    typedef enum _BIT { CLEAR = 0, SET } BIT;
     
    #define PUBLIC                                  /* Function attributes */
    #define PROTECTED
    #define PRIVATE   static
     
    /* INT is processor specific in length may vary in size */
    typedef signed int          INT;
    typedef signed char         INT8;
    typedef signed short int    INT16;
    typedef signed long int     INT32;
     
    /* MPLAB C Compiler for PIC18 does not support 64-bit integers */
    #if !defined(__18CXX)
    __EXTENSION typedef signed long long    INT64;
    #endif
     
    /* UINT is processor specific in length may vary in size */
    typedef unsigned int        UINT;
    typedef unsigned char       UINT8;
    typedef unsigned short int  UINT16;
    /* 24-bit type only available on C18 */
    #if defined(__18CXX)
    typedef unsigned short long UINT24;
    #endif
    typedef unsigned long int   UINT32;     /* other name for 32-bit integer */
    /* MPLAB C Compiler for PIC18 does not support 64-bit integers */
    #if !defined(__18CXX)
    __EXTENSION typedef unsigned long long  UINT64;
    #endif
     
    typedef union
    {
        UINT8 Val;
        struct
        {
            __EXTENSION UINT8 b0:1;
            __EXTENSION UINT8 b1:1;
            __EXTENSION UINT8 b2:1;
            __EXTENSION UINT8 b3:1;
            __EXTENSION UINT8 b4:1;
            __EXTENSION UINT8 b5:1;
            __EXTENSION UINT8 b6:1;
            __EXTENSION UINT8 b7:1;
        } bits;
    } UINT8_VAL, UINT8_BITS;
     
    typedef union 
    {
        UINT16 Val;
        UINT8 v[2] __PACKED;
        struct __PACKED
        {
            UINT8 LB;
            UINT8 HB;
        } byte;
        struct __PACKED
        {
            __EXTENSION UINT8 b0:1;
            __EXTENSION UINT8 b1:1;
            __EXTENSION UINT8 b2:1;
            __EXTENSION UINT8 b3:1;
            __EXTENSION UINT8 b4:1;
            __EXTENSION UINT8 b5:1;
            __EXTENSION UINT8 b6:1;
            __EXTENSION UINT8 b7:1;
            __EXTENSION UINT8 b8:1;
            __EXTENSION UINT8 b9:1;
            __EXTENSION UINT8 b10:1;
            __EXTENSION UINT8 b11:1;
            __EXTENSION UINT8 b12:1;
            __EXTENSION UINT8 b13:1;
            __EXTENSION UINT8 b14:1;
            __EXTENSION UINT8 b15:1;
        } bits;
    } UINT16_VAL, UINT16_BITS;
     
    /* 24-bit type only available on C18 */
    #if defined(__18CXX)
    typedef union
    {
        UINT24 Val;
        UINT8 v[3] __PACKED;
        struct __PACKED
        {
            UINT8 LB;
            UINT8 HB;
            UINT8 UB;
        } byte;
        struct __PACKED
        {
            __EXTENSION UINT8 b0:1;
            __EXTENSION UINT8 b1:1;
            __EXTENSION UINT8 b2:1;
            __EXTENSION UINT8 b3:1;
            __EXTENSION UINT8 b4:1;
            __EXTENSION UINT8 b5:1;
            __EXTENSION UINT8 b6:1;
            __EXTENSION UINT8 b7:1;
            __EXTENSION UINT8 b8:1;
            __EXTENSION UINT8 b9:1;
            __EXTENSION UINT8 b10:1;
            __EXTENSION UINT8 b11:1;
            __EXTENSION UINT8 b12:1;
            __EXTENSION UINT8 b13:1;
            __EXTENSION UINT8 b14:1;
            __EXTENSION UINT8 b15:1;
            __EXTENSION UINT8 b16:1;
            __EXTENSION UINT8 b17:1;
            __EXTENSION UINT8 b18:1;
            __EXTENSION UINT8 b19:1;
            __EXTENSION UINT8 b20:1;
            __EXTENSION UINT8 b21:1;
            __EXTENSION UINT8 b22:1;
            __EXTENSION UINT8 b23:1;
        } bits;
    } UINT24_VAL, UINT24_BITS;
    #endif
     
    typedef union
    {
        UINT32 Val;
        UINT16 w[2] __PACKED;
        UINT8  v[4] __PACKED;
        struct __PACKED
        {
            UINT16 LW;
            UINT16 HW;
        } word;
        struct __PACKED
        {
            UINT8 LB;
            UINT8 HB;
            UINT8 UB;
            UINT8 MB;
        } byte;
        struct __PACKED
        {
            UINT16_VAL low;
            UINT16_VAL high;
        }wordUnion;
        struct __PACKED
        {
            __EXTENSION UINT8 b0:1;
            __EXTENSION UINT8 b1:1;
            __EXTENSION UINT8 b2:1;
            __EXTENSION UINT8 b3:1;
            __EXTENSION UINT8 b4:1;
            __EXTENSION UINT8 b5:1;
            __EXTENSION UINT8 b6:1;
            __EXTENSION UINT8 b7:1;
            __EXTENSION UINT8 b8:1;
            __EXTENSION UINT8 b9:1;
            __EXTENSION UINT8 b10:1;
            __EXTENSION UINT8 b11:1;
            __EXTENSION UINT8 b12:1;
            __EXTENSION UINT8 b13:1;
            __EXTENSION UINT8 b14:1;
            __EXTENSION UINT8 b15:1;
            __EXTENSION UINT8 b16:1;
            __EXTENSION UINT8 b17:1;
            __EXTENSION UINT8 b18:1;
            __EXTENSION UINT8 b19:1;
            __EXTENSION UINT8 b20:1;
            __EXTENSION UINT8 b21:1;
            __EXTENSION UINT8 b22:1;
            __EXTENSION UINT8 b23:1;
            __EXTENSION UINT8 b24:1;
            __EXTENSION UINT8 b25:1;
            __EXTENSION UINT8 b26:1;
            __EXTENSION UINT8 b27:1;
            __EXTENSION UINT8 b28:1;
            __EXTENSION UINT8 b29:1;
            __EXTENSION UINT8 b30:1;
            __EXTENSION UINT8 b31:1;
        } bits;
    } UINT32_VAL;
     
    /* MPLAB C Compiler for PIC18 does not support 64-bit integers */
    #if !defined(__18CXX)
    typedef union
    {
        UINT64 Val;
        UINT32 d[2] __PACKED;
        UINT16 w[4] __PACKED;
        UINT8 v[8]  __PACKED;
        struct __PACKED
        {
            UINT32 LD;
            UINT32 HD;
        } dword;
        struct __PACKED
        {
            UINT16 LW;
            UINT16 HW;
            UINT16 UW;
            UINT16 MW;
        } word;
        struct __PACKED
        {
            __EXTENSION UINT8 b0:1;
            __EXTENSION UINT8 b1:1;
            __EXTENSION UINT8 b2:1;
            __EXTENSION UINT8 b3:1;
            __EXTENSION UINT8 b4:1;
            __EXTENSION UINT8 b5:1;
            __EXTENSION UINT8 b6:1;
            __EXTENSION UINT8 b7:1;
            __EXTENSION UINT8 b8:1;
            __EXTENSION UINT8 b9:1;
            __EXTENSION UINT8 b10:1;
            __EXTENSION UINT8 b11:1;
            __EXTENSION UINT8 b12:1;
            __EXTENSION UINT8 b13:1;
            __EXTENSION UINT8 b14:1;
            __EXTENSION UINT8 b15:1;
            __EXTENSION UINT8 b16:1;
            __EXTENSION UINT8 b17:1;
            __EXTENSION UINT8 b18:1;
            __EXTENSION UINT8 b19:1;
            __EXTENSION UINT8 b20:1;
            __EXTENSION UINT8 b21:1;
            __EXTENSION UINT8 b22:1;
            __EXTENSION UINT8 b23:1;
            __EXTENSION UINT8 b24:1;
            __EXTENSION UINT8 b25:1;
            __EXTENSION UINT8 b26:1;
            __EXTENSION UINT8 b27:1;
            __EXTENSION UINT8 b28:1;
            __EXTENSION UINT8 b29:1;
            __EXTENSION UINT8 b30:1;
            __EXTENSION UINT8 b31:1;
            __EXTENSION UINT8 b32:1;
            __EXTENSION UINT8 b33:1;
            __EXTENSION UINT8 b34:1;
            __EXTENSION UINT8 b35:1;
            __EXTENSION UINT8 b36:1;
            __EXTENSION UINT8 b37:1;
            __EXTENSION UINT8 b38:1;
            __EXTENSION UINT8 b39:1;
            __EXTENSION UINT8 b40:1;
            __EXTENSION UINT8 b41:1;
            __EXTENSION UINT8 b42:1;
            __EXTENSION UINT8 b43:1;
            __EXTENSION UINT8 b44:1;
            __EXTENSION UINT8 b45:1;
            __EXTENSION UINT8 b46:1;
            __EXTENSION UINT8 b47:1;
            __EXTENSION UINT8 b48:1;
            __EXTENSION UINT8 b49:1;
            __EXTENSION UINT8 b50:1;
            __EXTENSION UINT8 b51:1;
            __EXTENSION UINT8 b52:1;
            __EXTENSION UINT8 b53:1;
            __EXTENSION UINT8 b54:1;
            __EXTENSION UINT8 b55:1;
            __EXTENSION UINT8 b56:1;
            __EXTENSION UINT8 b57:1;
            __EXTENSION UINT8 b58:1;
            __EXTENSION UINT8 b59:1;
            __EXTENSION UINT8 b60:1;
            __EXTENSION UINT8 b61:1;
            __EXTENSION UINT8 b62:1;
            __EXTENSION UINT8 b63:1;
        } bits;
    } UINT64_VAL;
    #endif /* __18CXX */
     
    /***********************************************************************************/
     
    /* Alternate definitions */
    typedef void                    VOID;
     
    typedef char                    CHAR8;
    typedef unsigned char           UCHAR8;
     
    typedef unsigned char           BYTE;                           /* 8-bit unsigned  */
    typedef unsigned short int      WORD;                           /* 16-bit unsigned */
    typedef unsigned long           DWORD;                          /* 32-bit unsigned */
    /* MPLAB C Compiler for PIC18 does not support 64-bit integers */
    __EXTENSION
    typedef unsigned long long      QWORD;                          /* 64-bit unsigned */
    typedef signed char             CHAR;                           /* 8-bit signed    */
    typedef signed short int        SHORT;                          /* 16-bit signed   */
    typedef signed long             LONG;                           /* 32-bit signed   */
    /* MPLAB C Compiler for PIC18 does not support 64-bit integers */
    __EXTENSION
    typedef signed long long        LONGLONG;                       /* 64-bit signed   */
    typedef union
    {
        BYTE Val;
        struct __PACKED
        {
            __EXTENSION BYTE b0:1;
            __EXTENSION BYTE b1:1;
            __EXTENSION BYTE b2:1;
            __EXTENSION BYTE b3:1;
            __EXTENSION BYTE b4:1;
            __EXTENSION BYTE b5:1;
            __EXTENSION BYTE b6:1;
            __EXTENSION BYTE b7:1;
        } bits;
    } BYTE_VAL, BYTE_BITS;
     
    typedef union
    {
        WORD Val;
        BYTE v[2] __PACKED;
        struct __PACKED
        {
            BYTE LB;
            BYTE HB;
        } byte;
        struct __PACKED
        {
            __EXTENSION BYTE b0:1;
            __EXTENSION BYTE b1:1;
            __EXTENSION BYTE b2:1;
            __EXTENSION BYTE b3:1;
            __EXTENSION BYTE b4:1;
            __EXTENSION BYTE b5:1;
            __EXTENSION BYTE b6:1;
            __EXTENSION BYTE b7:1;
            __EXTENSION BYTE b8:1;
            __EXTENSION BYTE b9:1;
            __EXTENSION BYTE b10:1;
            __EXTENSION BYTE b11:1;
            __EXTENSION BYTE b12:1;
            __EXTENSION BYTE b13:1;
            __EXTENSION BYTE b14:1;
            __EXTENSION BYTE b15:1;
        } bits;
    } WORD_VAL, WORD_BITS;
     
    typedef union
    {
        DWORD Val;
        WORD w[2] __PACKED;
        BYTE v[4] __PACKED;
        struct __PACKED
        {
            WORD LW;
            WORD HW;
        } word;
        struct __PACKED
        {
            BYTE LB;
            BYTE HB;
            BYTE UB;
            BYTE MB;
        } byte;
        struct __PACKED
        {
            WORD_VAL low;
            WORD_VAL high;
        }wordUnion;
        struct __PACKED
        {
            __EXTENSION BYTE b0:1;
            __EXTENSION BYTE b1:1;
            __EXTENSION BYTE b2:1;
            __EXTENSION BYTE b3:1;
            __EXTENSION BYTE b4:1;
            __EXTENSION BYTE b5:1;
            __EXTENSION BYTE b6:1;
            __EXTENSION BYTE b7:1;
            __EXTENSION BYTE b8:1;
            __EXTENSION BYTE b9:1;
            __EXTENSION BYTE b10:1;
            __EXTENSION BYTE b11:1;
            __EXTENSION BYTE b12:1;
            __EXTENSION BYTE b13:1;
            __EXTENSION BYTE b14:1;
            __EXTENSION BYTE b15:1;
            __EXTENSION BYTE b16:1;
            __EXTENSION BYTE b17:1;
            __EXTENSION BYTE b18:1;
            __EXTENSION BYTE b19:1;
            __EXTENSION BYTE b20:1;
            __EXTENSION BYTE b21:1;
            __EXTENSION BYTE b22:1;
            __EXTENSION BYTE b23:1;
            __EXTENSION BYTE b24:1;
            __EXTENSION BYTE b25:1;
            __EXTENSION BYTE b26:1;
            __EXTENSION BYTE b27:1;
            __EXTENSION BYTE b28:1;
            __EXTENSION BYTE b29:1;
            __EXTENSION BYTE b30:1;
            __EXTENSION BYTE b31:1;
        } bits;
    } DWORD_VAL;
     
    /* MPLAB C Compiler for PIC18 does not support 64-bit integers */
    typedef union
    {
        QWORD Val;
        DWORD d[2] __PACKED;
        WORD w[4] __PACKED;
        BYTE v[8] __PACKED;
        struct __PACKED
        {
            DWORD LD;
            DWORD HD;
        } dword;
        struct __PACKED
        {
            WORD LW;
            WORD HW;
            WORD UW;
            WORD MW;
        } word;
        struct __PACKED
        {
            __EXTENSION BYTE b0:1;
            __EXTENSION BYTE b1:1;
            __EXTENSION BYTE b2:1;
            __EXTENSION BYTE b3:1;
            __EXTENSION BYTE b4:1;
            __EXTENSION BYTE b5:1;
            __EXTENSION BYTE b6:1;
            __EXTENSION BYTE b7:1;
            __EXTENSION BYTE b8:1;
            __EXTENSION BYTE b9:1;
            __EXTENSION BYTE b10:1;
            __EXTENSION BYTE b11:1;
            __EXTENSION BYTE b12:1;
            __EXTENSION BYTE b13:1;
            __EXTENSION BYTE b14:1;
            __EXTENSION BYTE b15:1;
            __EXTENSION BYTE b16:1;
            __EXTENSION BYTE b17:1;
            __EXTENSION BYTE b18:1;
            __EXTENSION BYTE b19:1;
            __EXTENSION BYTE b20:1;
            __EXTENSION BYTE b21:1;
            __EXTENSION BYTE b22:1;
            __EXTENSION BYTE b23:1;
            __EXTENSION BYTE b24:1;
            __EXTENSION BYTE b25:1;
            __EXTENSION BYTE b26:1;
            __EXTENSION BYTE b27:1;
            __EXTENSION BYTE b28:1;
            __EXTENSION BYTE b29:1;
            __EXTENSION BYTE b30:1;
            __EXTENSION BYTE b31:1;
            __EXTENSION BYTE b32:1;
            __EXTENSION BYTE b33:1;
            __EXTENSION BYTE b34:1;
            __EXTENSION BYTE b35:1;
            __EXTENSION BYTE b36:1;
            __EXTENSION BYTE b37:1;
            __EXTENSION BYTE b38:1;
            __EXTENSION BYTE b39:1;
            __EXTENSION BYTE b40:1;
            __EXTENSION BYTE b41:1;
            __EXTENSION BYTE b42:1;
            __EXTENSION BYTE b43:1;
            __EXTENSION BYTE b44:1;
            __EXTENSION BYTE b45:1;
            __EXTENSION BYTE b46:1;
            __EXTENSION BYTE b47:1;
            __EXTENSION BYTE b48:1;
            __EXTENSION BYTE b49:1;
            __EXTENSION BYTE b50:1;
            __EXTENSION BYTE b51:1;
            __EXTENSION BYTE b52:1;
            __EXTENSION BYTE b53:1;
            __EXTENSION BYTE b54:1;
            __EXTENSION BYTE b55:1;
            __EXTENSION BYTE b56:1;
            __EXTENSION BYTE b57:1;
            __EXTENSION BYTE b58:1;
            __EXTENSION BYTE b59:1;
            __EXTENSION BYTE b60:1;
            __EXTENSION BYTE b61:1;
            __EXTENSION BYTE b62:1;
            __EXTENSION BYTE b63:1;
        } bits;
    } QWORD_VAL;
     
    #undef __EXTENSION
     
    #endif /* __GENERIC_TYPE_DEFS_H_ */

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    taille des types (sizeof):
            char: 1
            short: 2
            int: 4
            long: 4
            longlong: 8
            BYTE: 1
            BYTE_VAL: 1
            WORD: 2
            WORD_VAL: 2
            WORD_VAL.v[0]: 1
            DWORD: 4
            DWORD_VAL: 4
            DWORD_VAL.v[0]: 1
            QWORD: 8
            QWORD_VAL: 8
            QWORD_VAL.v[0]: 1
    => je ne sais pas pourquoi mais pour une certaine valeur, le printf m'affiche "FFFFCED0FFFF" : de quoi ça peut venir (normalement ça ne devrait afficher que des chaines de 8 caractères) ?

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Question autours des tailles des types
    Par chronos_ dans le forum Langage
    Réponses: 1
    Dernier message: 06/08/2013, 11h01
  2. Différence entre taille des types
    Par geek21 dans le forum Débuter
    Réponses: 3
    Dernier message: 18/08/2009, 15h34
  3. Réponses: 2
    Dernier message: 22/05/2008, 23h23
  4. Réponses: 12
    Dernier message: 01/03/2007, 11h28
  5. la taille des types de base
    Par hansaplast dans le forum C++
    Réponses: 4
    Dernier message: 27/04/2006, 15h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo