소개
이 실습에서는 C 프로그래밍에서 혼합형 산술 연산을 처리하는 방법을 배웁니다. 이 실습은 다음 단계를 포함합니다.
먼저 정수형과 부동소수점 변수를 선언하고 초기화하는 방법, 그리고 적절한 형식 지정자를 사용하여 그 값을 출력하는 방법을 배웁니다. 그런 다음, 서로 다른 숫자형을 결합하는 산술 연산을 수행하여 형 변환 및 혼합 산술을 보여줍니다. 마지막으로 이러한 혼합형 산술 연산의 결과를 출력하고 확인합니다.
이 실습을 마치면 C 에서 혼합형 데이터를 효과적으로 관리하고 조작하는 방법에 대한 확실한 이해를 얻게 될 것입니다. 이는 견고하고 다재다능한 프로그램을 구축하는 데 필수적인 기술입니다.
정수형 및 부동소수점 변수 선언
이 단계에서는 C 프로그래밍에서 서로 다른 숫자형 변수를 선언하고 초기화하는 방법을 배웁니다. 혼합형 산술 연산을 위해 정수형 및 부동소수점 변수를 생성하는 데 중점을 둡니다.
먼저 프로젝트 디렉토리에 새로운 C 소스 파일을 생성합니다.
cd ~/project
nano mixed_arithmetic.c
이제 변수를 선언하는 다음 코드를 추가합니다.
#include <stdio.h>
int main() {
// 정수형 변수 선언
int whole_number1 = 10;
int whole_number2 = 5;
// 부동소수점 변수 선언
float decimal_number1 = 7.5;
float decimal_number2 = 3.2;
// 선언된 변수 출력
printf("정수형 변수:\n");
printf("whole_number1 = %d\n", whole_number1);
printf("whole_number2 = %d\n", whole_number2);
printf("\n부동소수점 변수:\n");
printf("decimal_number1 = %f\n", decimal_number1);
printf("decimal_number2 = %f\n", decimal_number2);
return 0;
}
프로그램을 컴파일하고 실행합니다.
gcc mixed_arithmetic.c -o mixed_arithmetic
./mixed_arithmetic
예상 출력:
정수형 변수:
whole_number1 = 10
whole_number2 = 5
부동소수점 변수:
decimal_number1 = 7.500000
decimal_number2 = 3.200000
이 코드에서는 다음을 보여줍니다.
int형을 사용하여 정수형 변수를 선언하는 방법float형을 사용하여 부동소수점 변수를 선언하는 방법- 특정 값으로 변수를 초기화하는 방법
- 적절한 형식 지정자와 함께
printf()를 사용하여 변수 값을 출력하는 방법 - 정수형과 부동소수점 숫자 표현의 차이점
서로 다른 형의 산술 연산 수행
이 단계에서는 C 에서 서로 다른 숫자형을 결합하는 산술 연산을 수행하는 방법을 배우고, 형 변환 및 혼합 산술을 보여줍니다.
이전 파일을 열어 예제를 계속합니다.
cd ~/project
nano mixed_arithmetic.c
이전 main() 함수를 다음 코드로 바꿉니다.
#include <stdio.h>
int main() {
// 정수형 변수
int whole_number1 = 10;
int whole_number2 = 5;
// 부동소수점 변수
float decimal_number1 = 7.5;
float decimal_number2 = 3.2;
// 혼합형 산술 연산
int int_result = whole_number1 + whole_number2;
float float_result = decimal_number1 + decimal_number2;
// 혼합형 덧셈 (정수 + 부동소수점)
float mixed_addition = whole_number1 + decimal_number1;
// 혼합형 곱셈
float mixed_multiplication = whole_number2 * decimal_number2;
// 형 변환 보여주기
printf("정수 덧셈: %d + %d = %d\n", whole_number1, whole_number2, int_result);
printf("부동소수점 덧셈: %.1f + %.1f = %.1f\n", decimal_number1, decimal_number2, float_result);
printf("혼합 덧셈: %d + %.1f = %.1f\n", whole_number1, decimal_number1, mixed_addition);
printf("혼합 곱셈: %d * %.1f = %.1f\n", whole_number2, decimal_number2, mixed_multiplication);
return 0;
}
프로그램을 컴파일하고 실행합니다.
gcc mixed_arithmetic.c -o mixed_arithmetic
./mixed_arithmetic
예상 출력:
정수 덧셈: 10 + 5 = 15
부동소수점 덧셈: 7.5 + 3.2 = 10.7
혼합 덧셈: 10 + 7.5 = 17.5
혼합 곱셈: 5 * 3.2 = 16.0
이 예제의 주요 내용:
- 같은 형의 변수 (int-int, float-float) 로 산술 연산 수행
- 혼합형 연산에서 자동 형 변환 보여주기
- 필요에 따라 C 는 정수를 부동소수점 숫자로 자동 변환
- 소수점 자릿수 표시를 제어하기 위한 형식 지정자 사용
Print and Verify the Results
In this final step, you'll learn how to format and verify the results of mixed-type arithmetic operations in C, focusing on precise output and result validation.
Open the previous file to continue our example:
cd ~/project
nano mixed_arithmetic.c
Update the main() function with more detailed output and result verification:
#include <stdio.h>
#include <math.h>
int main() {
// Integer variables
int whole_number1 = 10;
int whole_number2 = 5;
// Floating-point variables
float decimal_number1 = 7.5;
float decimal_number2 = 3.2;
// Mixed-type arithmetic operations
float mixed_addition = whole_number1 + decimal_number1;
float mixed_multiplication = whole_number2 * decimal_number2;
float mixed_division = decimal_number1 / whole_number2;
// Detailed result printing with formatting
printf("Arithmetic Operation Results:\n");
printf("1. Mixed Addition: %d + %.1f = %.2f\n", whole_number1, decimal_number1, mixed_addition);
printf("2. Mixed Multiplication: %d * %.1f = %.2f\n", whole_number2, decimal_number2, mixed_multiplication);
printf("3. Mixed Division: %.1f / %d = %.2f\n", decimal_number1, whole_number2, mixed_division);
// Result verification
printf("\nResult Verification:\n");
printf("Mixed Addition Verification: %.2f == %.2f\n",
mixed_addition, (float)whole_number1 + decimal_number1);
printf("Mixed Multiplication Verification: %.2f == %.2f\n",
mixed_multiplication, (float)whole_number2 * decimal_number2);
printf("Mixed Division Verification: %.2f == %.2f\n",
mixed_division, decimal_number1 / whole_number2);
return 0;
}
Compile and run the program:
gcc mixed_arithmetic.c -o mixed_arithmetic
./mixed_arithmetic
Example output:
Arithmetic Operation Results:
1. Mixed Addition: 10 + 7.5 = 17.50
2. Mixed Multiplication: 5 * 3.2 = 16.00
3. Mixed Division: 7.5 / 5 = 1.50
Result Verification:
Mixed Addition Verification: 17.50 == 17.50
Mixed Multiplication Verification: 16.00 == 16.00
Mixed Division Verification: 1.50 == 1.50
Key points in this example:
- Using different format specifiers for precise output
- Demonstrating mixed-type arithmetic with addition, multiplication, and division
- Performing inline result verification
- Showing type conversion in arithmetic operations
요약
이 실습에서는 C 프로그래밍에서 정수형 및 부동소수점 변수를 포함한 다양한 숫자형 변수를 선언하고 초기화하는 방법을 배웠습니다. 그런 다음 이러한 서로 다른 숫자형을 결합하는 산술 연산을 수행하여 형 변환 및 혼합 산술을 보여주었습니다. 마지막으로 이러한 혼합형 산술 연산의 결과를 출력하고 검증했습니다.
이 실습에서 얻은 주요 학습 내용은 정수형과 부동소수점 숫자 표현의 차이점, printf() 문에서 적절한 형식 지정자 사용, 서로 다른 숫자형 변수를 사용할 때 C 의 형 변환 및 혼합 산술의 동작을 이해하는 것입니다.



