Menu
CoddyTech

정수 나눗셈과 범위

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

C#과 C는 모두 정수 나눗셈의 결과를 0 방향으로 버립니다. C#의 checked 및 unchecked 컨텍스트는 오버플로를 서로 다르게 처리하며, C의 부호 있는 정수 오버플로는 래핑된다는 보장이 없습니다.

정수 나눗셈의 결과는 0 방향으로 버려지며, 나머지는 피제수의 부호를 따릅니다. 부호 있는 정수 오버플로는 정의되지 않은 동작입니다.

C# 예제

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
class Program {

static void Main(){
Console.WriteLine((-7/3)+" "+(-7%3));
}
}

C# 출력:

-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실력 점검

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

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

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