У меня есть назначение для написания кода, который находит максимальное значение, которое может содержать определенный тип данных.Я делаю это, используя тест 1, используя сдвиг влево.Он работает нормально для int, char, short int и long int, однако я не могу заставить его работать для unsigned in и unsigned char.Код выполняется, однако, когда он доходит до этой точки, он просто застревает и ничего не выводит.Я приложу весь мой код ниже.Как мне заставить это работать, что я делаю не так.Я использовал% u вместо% d, я не уверен, что еще мне придется изменить.Спасибо !!
//
// File name: ewh9320_proj2_data_types_rotate.c
// Author: Ethan Hall
// Created for RIT CMPR271 on 9/25/19
// Purpose: To find the maximum value of bits used for each data type
//
#include <stdio.h>
#include <math.h>
int main(void)
{
//int
int overflow0, previous0;
while(1)
{
previous0 = overflow0;
overflow0 = 1 + (previous0<<1);
if(previous0 > overflow0)
{
printf("int:\n");
printf("The max value is %d\n\n", previous0);
break;
}
}
//char
char overflow1, previous1;
while(1)
{
previous1 = overflow1;
overflow1 = 1 + (previous1<<1);
if(previous1 > overflow1)
{
printf("char:\n");
printf("The max value is %d\n\n", previous1);
break;
}
}
//short int
short int overflow2, previous2;
while(1)
{
previous2 = overflow2;
overflow2 = 1 + (previous2<<1);
if(previous2 > overflow2)
{
printf("short int:\n");
printf("The max value is %d\n\n", previous2);
break;
}
}
//unsigned int
unsigned int overflow3, previous3;
while(1)
{
previous3 = overflow3;
overflow3 = 1 + (previous3<<1);
if(previous3 > overflow3)
{
printf("unsigned int:\n");
printf("The max value is %u\n\n", previous3);
break;
}
}
//unsigned char
unsigned char overflow4, previous4;
while(1)
{
previous4 = overflow4;
overflow4 = 1 + (previous4<<1);
if(previous4 > overflow4)
{
printf("unsigned char:\n");
printf("The max value is %d\n\n", previous4);
break;
}
}
//long int
long int overflow5, previous5;
while(1)
{
previous5 = overflow5;
overflow5 = 1 + (previous5<<1);
if(previous5 > overflow5)
{
printf("long int:\n");
printf("The max value is %ld\n\n", previous5);
break;
}
}
}
вторая программа, которая не запустится после int64_t:
int main(void)
{
//int8_t
int8_t overflow0, previous0;
while(1)
{
previous0 = overflow0;
overflow0 = 1 + (previous0<<1);
if(previous0 > overflow0)
{
printf("int8_t:\n");
printf("The max value is %d\n\n", previous0);
break;
}
}
//uint8_t
uint8_t overflow1, previous1;
while(1)
{
previous1 = overflow1;
overflow1 = 1 + (previous1<<1);
if(previous1 > overflow1)
{
printf("uint8_t:\n");
printf("The max value is %u\n\n", previous1);
break;
}
}
//int16_t
int16_t overflow2, previous2;
while(1)
{
previous2 = overflow2;
overflow2 = 1 + (previous2<<1);
if(previous2 > overflow2)
{
printf("int16_t:\n");
printf("The max value is %d\n\n", previous2);
break;
}
}
//uint16_t
uint16_t overflow3, previous3;
while(1)
{
previous3 = overflow3;
overflow3 = 1 + (previous3<<1);
if(previous3 > overflow3)
{
printf("uint16_t:\n");
printf("The max value is %u\n\n", previous3);
break;
}
}
//int32_t
int32_t overflow4, previous4;
while(1)
{
previous4 = overflow4;
overflow4 = 1 + (previous4<<1);
if(previous4 > overflow4)
{
printf("int32_t:\n");
printf("The max value is %d\n\n", previous4);
break;
}
}
//uint32_t
uint32_t overflow5, previous5;
while(1)
{
previous5 = overflow5;
overflow5 = 1 + (previous5<<1);
if(previous5 > overflow5)
{
printf("uint32_t:\n");
printf("The max value is %u\n\n", previous5);
break;
}
}
//int64_t
int64_t overflow6, previous6;
while(1)
{
previous6 = overflow6;
overflow6 = 1 + (previous6<<1);
if(previous6 >= overflow6)
{
printf("int64_t:\n");
printf("The max value is %lld\n\n", previous6);
break;
}
}
//uint64_t
uint64_t overflow7, previous7;
while(1)
{
previous7 = overflow7;
overflow7 = 1 + (previous7<<1);
if(previous7 > overflow7)
{
printf("uint64_t:\n");
printf("The max value is %llu\n\n", previous7);
break;
}
}
//uintptr_t
uintptr_t overflow8, previous8;
while(1)
{
previous8 = overflow8;
overflow8 = 1 + (previous8<<1);
if(previous8 > overflow8)
{
printf("uintptr_t:\n");
printf("The max value is %lu\n\n", previous8);
break;
}
}
//intmax_t
intmax_t overflow9, previous9;
while(1)
{
previous9 = overflow9;
overflow9 = 1 + (previous9<<1);
if(previous9 > overflow9)
{
printf("intmax_t:\n");
printf("The max value is %ld\n\n", previous9);
break;
}
}
}