Menu
CoddyTech

정수 나눗셈과 범위

Coddy의 Rust 개발자를 위한 C 코스 레슨. 48개 중 4번째.

Rust와 C의 정수 나눗셈은 0을 향해 버림합니다. Rust의 오버플로 검사는 연산과 빌드 설정에 따라 달라지며, C의 부호 있는 정수 오버플로는 정의되지 않습니다. Rust의 wrapping_* 메서드를 일반적인 C 산술의 의미로 간주하지 마세요.

정수 나눗셈은 0을 향해 버림하며, 나머지는 피제수의 부호를 따릅니다. 부호 있는 정수 오버플로는 정의되지 않은 동작입니다.

Rust 예제


fn main(){
println!("{} {}",-7/3,-7%3);
}

Rust 출력:

-2 -1

C 예제

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>

int main(void) {
printf("%d %d\n",-7/3,-7%3);
return 0;
}

C 출력:

-2 -1
challenge icon

챌린지

쉬움

입력은 정수인 피제수 a와 양의 정수인 제수 b입니다. C의 정수 연산을 사용해 quotient와 integer_remainder를 구현하세요. quotient:remainder를 출력하세요.

잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 표시된 잠금 상태의 main.c에서 직접 호출하는 공개 API의 선언과 호환되도록 유지하세요. 또 다른 main을 추가하지 마세요. 작업에서 특정 범위를 확인하는 경우가 아니라면, 모든 숫자 작업 픽스처는 오버플로가 발생하지 않을 만큼 작습니다. 지정된 출력만 출력하세요. 입력 파싱과 출력이 표시된 경우 드라이버가 이를 처리합니다.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){int a,b;if(scanf("%d%d",&a,&b)!=2)return 1;printf("%d:%d\n",quotient(a,b),integer_remainder(a,b));return 0;}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Rust 개발자를 위한 C의 모든 레슨

직접 연습해 보세요: 온라인 C 컴파일러