Tamanho do caractere ('a') em C / C ++
Qual é o tamanho do caractere em C e C ++? Tanto quanto eu sei, o tamanho do caractere é de 1 byte em C e C ++.
#include <stdio.h>
int main()
{
printf("Size of char : %d\n",sizeof(char));
return 0;
}
#include <iostream>
int main()
{
std::cout<<"Size of char : "<<sizeof(char)<<"\n";
return 0;
}
Sem surpresas, os dois fornecem a saída:Size of char : 1
Agora sabemos que os personagens são representados como'a'
,'b'
,'c'
,'|'
, ... Então, acabei de modificar os códigos acima para estes:
Em C:
#include <stdio.h>
int main()
{
char a = 'a';
printf("Size of char : %d\n",sizeof(a));
printf("Size of char : %d\n",sizeof('a'));
return 0;
}
Size of char : 1
Size of char : 4
Em C ++:
#include <iostream>
int main()
{
char a = 'a';
std::cout<<"Size of char : "<<sizeof(a)<<"\n";
std::cout<<"Size of char : "<<sizeof('a')<<"\n";
return 0;
}
Size of char : 1
Size of char : 1
Porque osizeof('a')
retorna valores diferentes em C e C ++?