논리 연산자
Coddy의 C++ 개발자를 위한 C 코스 레슨. 48개 중 8번째.
C++의 내장 논리 연산자는 bool을 반환하고, C의 논리 연산자는 0 또는 1인 int를 반환합니다. 둘 다 단락 평가를 합니다. C++의 오버로드된 연산자는 C의 연산자 오버로딩에는 없는 가능성을 더해 줍니다.
논리 연산자는 단락 평가를 하고, 임의의 피연산자 값을 선택하는 대신 0 또는 1을 생성합니다.
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 << std::boolalpha << (5 && 9) << "\n";
}
C++ 출력:
trueC 예제
#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\n",5 && 9);
return 0;
}
C 출력:
1챌린지
쉬움입력은 분자 a와 제수 b입니다. b가 0이 아니고 a가 b로 나누어떨어질 때만 1을 반환하고, 그렇지 않으면 0을 반환하세요. b가 0일 때는 a%b를 절대 계산하지 마세요.
잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 표시된 잠금 상태의 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=0,b=0;if(scanf("%d%d",&a,&b)!=2)return 1;printf("%d\n",divides(a,b));return 0;}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
C++ 개발자를 위한 C의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러