실수형 (Float - Double)
Coddy C 여정의 Fundamentals 섹션에 포함된 레슨. 63개 중 7번째.
Float 및 double은 C 언어에서 소수점 숫자를 저장하는 데 사용되는 데이터 타입입니다.
float 변수를 선언합니다:
float price = 19.99f;C 언어에 이 값이 float 값임을 알려주는 'f' 접미사에 유의하세요.
`double` 변수를 선언합니다:
double pi = 3.14159265359;float과 double의 주요 차이점:
- 정밀도: Double은 float보다 정밀도가 높습니다.
- Float: 약 7자리 십진수
- Double: 약 15자리 십진수
- Size:
- Float: 4 bytes
- Double: 8 bytes
- 범위:
- Float: 1.2E-38 ~ 3.4E+38
- Double: 2.3E-308 ~ 1.7E+308
float 값을 출력합니다:
float temperature = 98.6f;
printf("Temperature is %f degrees\n", temperature);특정 소수점 자리수로 출력하기:
printf("Temperature is %.1f degrees\n", temperature);다음과 같이 출력됩니다: “Temperature is 98.6 degrees”
챌린지
쉬움다음 조건을 만족하는 프로그램을 작성하세요:
celsius라는 이름의 float 변수를 선언합니다.fahrenheit라는 이름의 double 변수를 선언합니다.celsius에 값 25.0을 할당합니다.- 다음 공식을 사용하여 celsius를 fahrenheit로 변환합니다:
fahrenheit = (celsius * 9.0/5.0) + 32.0 - 결과를 다음과 같이 출력합니다.
25.0 degrees Celsius is equal to 77.0 degrees Fahrenheit
출력 시 정확히 소수점 이하 한 자리까지 표시해야 합니다. Celsius와 Fahrenheit의 철자는 정확히 일치해야 합니다.
단일 printf에서 두 변수를 모두 출력하려면 각 숫자의 자리 표시자로 %.1f를 사용하세요. celsius 및 fahrenheit를 실제 변수 이름으로 대체합니다:
printf("%.1f degrees Celsius is equal to %.1f degrees Fahrenheit\n", celsius, fahrenheit);여기서 첫 번째 %.1f는 celsius의 값으로 대체되고, 두 번째 %.1f는 fahrenheit의 값으로 대체됩니다. 끝에 있는 \n은 커서를 새 줄로 이동합니다.
직접 해보기
#include <stdio.h>
int main() {
// 여기에 코드를 작성하세요
return 0;
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
Fundamentals의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러