9 Mart 2020 Pazartesi

char tipi

Giriş
C silinde CHAR_MAX ve CHAR_MIN değerleri alt ve üst sınıfı belirtir.

signed veya unsigned
char signed veya unsigned olabilir. Açıklaması şöyle.
According to the C11 standard (read n1570), char can be signed or unsigned (so you actually have two flavors of C). What exactly it is is implementation specific.
Açıklaması şöyle.
The C and C++ standards allows the character type char to be signed or unsigneddepending on the platform and compiler.
Most systems, including x86 GNU/Linux and Microsoft Windows, use signed char,
but those based on PowerPC and ARM processors typically use unsigned char.(29)
This can lead to unexpected results when porting programs between platforms which have different defaults for the type of char.
Örnek
Eğer char signed tip ise aşağıdaki kod false döner. Çünkü signed 0xfb -5'tir. unsigned 0xfb ise 251'dir.
char a = 0xfb; 
unsigned char b = 0xfb;
bool f;
f = (a == b); 
cout << f;
Örnek - signed integer overflow
Elimizde şöyle bir kod olsun. Eğer char signed integer ise bu kod "undefined behavior" verir.
#include <stdio.h>
#include <limits.h>

int main()
{
    char c = CHAR_MAX;
    c += 1;
    printf("CHAR_MIN=%d CHAR_MAX=%d c=%d (%c)\n", CHAR_MIN, CHAR_MAX, c, c);
}

Hiç yorum yok:

Yorum Gönder