Jump to content

C - Maior número inteiro


skcratch

Recommended Posts

Sim, essas informações estão no ficheiro limits.h.

Aqui fica um exemplo:

int main(int argc, char *argv[])
{
printf("TYPE \t\t BYTES \t SMALLEST \t\t LARGEST\n\n");

printf("signed char \t %zd \t %hhd \t\t\t %hhd\n",
	sizeof(signed char), CHAR_MIN, CHAR_MAX);

printf("unsigned char \t %zd \t 0 \t\t\t %hhu\n\n",
	sizeof(unsigned char), UCHAR_MAX);

printf("signed int \t %zd \t %d \t\t %d\n",
	sizeof(signed int), INT_MIN, INT_MAX);

printf("unsigned int \t %zd \t 0 \t\t\t %u\n\n",
	sizeof(unsigned int), UINT_MAX);

printf("signed short \t %zd \t %hd \t\t %hd\n",
	sizeof(signed short), SHRT_MIN, SHRT_MAX);

printf("unsigned short \t %zd \t 0 \t\t\t %hu\n\n",
	sizeof(unsigned short), USHRT_MAX);

printf("signed long \t %zd \t %ld \t %ld\n",
	sizeof(signed long), LONG_MIN, LONG_MAX);

printf("unsigned long \t %zd \t 0 \t\t\t %lu\n\n",
	sizeof(unsigned long), ULONG_MAX);

printf("float \t\t %zd \t %E \t\t %E\n",
	sizeof(float), FLT_MIN, FLT_MAX);

printf("double \t\t %zd \t %E \t\t %E\n",
	sizeof(double), DBL_MIN, DBL_MAX);

return 0;
}

<3 life

Link to comment
Share on other sites

podes sempre calcular 2 ^ (4 * sizeof(tipo)) - 1. até porque int é uma word, e varia consoante a largura do bus da máquina em que se encontra.

Assim à primeira vista essa fórmula está errada.

Para números com sinal deve ficar 2 ^ (8 * sizeof(tipo) - 1) - 1 e sem sinal 2 ^ (8 * sizeof(tipo)) - 1.

De qualquer forma é muito mais simples, seguro e eficiente usar as constantes do sistema.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.