정수 나눗셈과 범위
Coddy의 C++ 개발자를 위한 C 코스 레슨. 48개 중 4번째.
C17과 C++17은 모두 부호 있는 정수 나눗셈에서 0 방향으로 버림하며, 부호 있는 정수 오버플로의 동작은 정의되지 않습니다. 문법이 같다고 해서 래핑에 의존해도 되는 것은 아닙니다. 두 언어 모두에서 정수의 범위는 여전히 계약의 일부입니다.
정수 나눗셈은 0 방향으로 버림하며, 나머지는 피제수의 부호를 따릅니다. 부호 있는 정수 오버플로는 정의되지 않은 동작입니다.
C++ 예제
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <map>
#include <memory>
#include <algorithm>
#include <functional>
#include <variant>
#include <sstream>
int main(){
std::cout << -7/3 << " " << -7%3 << "\n";
}
C++ 출력:
-2 -1C 예제
#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챌린지
쉬움입력은 정수 피제수 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;}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
C++ 개발자를 위한 C의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러