문자열을 double로 변환하는 데 문제가 있습니다. 제발 도와주세요. DIFF 파일을 실행하는 동안문자열을 C로 double 변환
char Price[100];
double newPrice;
printf("\nPlease enter the price:");
fgets(Price,100,stdin);
newPrice = atof (Price);
printf("\nPrice of item is %f",newPrice);
그것은 사랑하는 출력을 제공 :
여기 내 코드입니다.
FILE1 (방법 1)
char Price[100];
double newPrice;
int main()
{
myval();
return 0;
}
myval()
{
printf("\nPlease enter the price:");
fgets(Price, 100, stdin);
newPrice = atof(Price);
printf("\nPrice of item is %f", newPrice);
}
파일 2 (방법 2)
#define MAX 50
char oldPrice[MAX];
double newPrice;
int main()
{
UserInput();
}
int UserInput()
{
printf("\nPlease enter the price:");
fgets(oldPrice, MAX, stdin);
newPrice = atof(oldPrice);
printf("\nPrice of item is %f", newPrice);
return 0;
}
위의 두 가지 방법은 TCC (작은 컴파일러)을 사용하여 컴파일되었다. 두 가지 방법 모두 동일하지만, 나는이 2 출력 1의은 diff 출력을 얻을 :
D:\>new.exe
Please enter the price:12.3
Price of item is 12.300000
출력 2 :
D:\>t.exe
Please enter the price:12.3
Price of item is 7735248.000000
기술적으로 두 함수 사이에는 차이가 없습니다 (첫 번째 예제의 'myval' 함수에서 무언가를 반환하지 않음으로써 정의되지 않은 동작 제외). 시도하기 전에 표시된 코드로 두 번째 예제를 빌드 하시겠습니까? –
@ JoachimPileborg - 나는 그것을 이해하지 못한다. 그러나 [ideone에 대한 재발견] (http://ideone.com/PBG2K1) – Chowlett
아, [첫 번째 것도 실패한다] (http://ideone.com/U8JUnd). 나는 아직도 모르지만 _why_. – Chowlett