C언어에서 보장하는 int 데이터형의 크기가 시스템의 워드 크기에 못미칠때에...

gurumong의 이미지

C언어에서 int 와 short의 크기를 최소한 -32767~32767(16비트)를 표준이(C99) 보장한다고 알고있습니다
그리고 int(16비트)는 대부분의 시스템에서 워드 크기이기 때문에 가장 효율적이라고도 알고있구요

만약 어떤 시스템의 워드 크기가 16비트 보다 작다고 한다면(예를 들면 8비트와 같이)
그러한 시스템에서의 임플리멘테이션이 C언어의 표준을 따르려면
무조건적으로 int가 -32767~32767(16비트) 크기보다 크도록 구현해야합니까?

혹시 표준에 이러한 시스템에 대한 표준을 따로 가지고 있지 않습니까?

zzzzz의 이미지

int형의 최소 크기는 말씀하신 대로 -32767~32767이 맞습니다. 8비트 시스템이라 해도 int형은 그것과 동일하거나 그 이상의 표현 범위를 가져야 합니다.

실제 사례를 하나 소개하자면, avr gcc를 들 수 있습니다. 8비트 마이크로컨트롤러인 atmel사의 AVR 씨리즈를 위한 C 컴파일러죠.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html

typedef signed int int16_t
16-bit signed type.

typedef signed long int int32_t
32-bit signed type.

typedef signed long long int int64_t
64-bit signed type.

typedef signed char int8_t
8-bit signed type.

zzzzz의 이미지

참고로, AVR과 같은 환경에서는 그냥 char 또는 int보다는, stdint.h에 정의된 int8_t나 uint16_t 와 같이 크기가 명시된 타입을 쓰는 것이 더 권장되는 듯 합니다.

최종호의 이미지

C99 의

6.2.5 Types 의 5번째 문단에서는

Quote:

An object declared as type signed char occupies the same amount of storage as a
‘‘plain’’ char object. 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 ).

와 같이 기술하고 있습니다.
말씀하신 16 bit 이상의 값을 가진다는 내용은 안 보입니다.

Java의 경우는 각 타입이 가지는 정확한 값의 range가 정해지지만

(int의 경우는 -2^31 <= x < (2^31 - 1))

C에서는 그런값이 정해지지 않는 것으로 알고 있습니다.

다만 6.3.1.1 절은

Quote:

1 Every integer type has an integer conversion rank defined as follows:
? No two signed integer types shall have the same rank, even if they hav e 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.
? The rank of any standard integer type shall be greater than the rank of any extended
integer type with the same width.
? The rank of char shall equal the rank of signed char and unsigned char.
? The rank of _Bool shall be less than the rank of all other standard integer types.
? The rank of any enumerated type shall equal the rank of the compatible integer type
(see 6.7.2.2).
? The rank of any extended signed integer type relative to another extended signed
integer type with the same precision is implementation-defined, but still subject to the
other rules for determining the integer conversion rank.
? For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has
greater rank than T3, then T1 has greater rank than T3.

처럼 각 integer type이 표현할 수 있는 값의 범위의 순위를 두고 있습니다.

느슨하게 해석하자면

bit#(signed char) <= bit#(short int) <= bit#(int) <= bit#(long int) <= bit#(long long int)

와 같은 관계에 있다는 것이 되겠죠.
최종호의 이미지

미확인님의 글을 보고 찾아봤더니만,

Quote:

5.2.4.2.1 Sizes of integer types
1 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 integer
promotions. Their implementation-defined 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 // -(2^7 - 1)
? maximum value for an object of type signed char
SCHAR_MAX +127 // 2^7 - 1
? maximum value for an object of type unsigned char
UCHAR_MAX 255 // 2^8 - 1
? 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 // -(2^15 - 1)
? maximum value for an object of type short int
SHRT_MAX +32767 // 2^15 - 1
? maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 2^16 - 1
? minimum value for an object of type int
INT_MIN -32767 // -(2^15 - 1)
? maximum value for an object of type int
INT_MAX +32767 // 2^15 - 1
? maximum value for an object of type unsigned int
UINT_MAX 65535 // 2^16 - 1
? minimum value for an object of type long int
LONG_MIN -2147483647 // -(2^31 - 1)
? maximum value for an object of type long int
LONG_MAX +2147483647 // 2^31 - 1
? maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2^32 - 1
? minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // -(2^63 - 1)
....


와 같은 내용이 있네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.