몽상실현개발주의

[따배씨] 7.7 논리 연산자 본문

Language/C

[따배씨] 7.7 논리 연산자

migrationArc 2021. 5. 24. 07:04

[따배씨] 7.7 논리 연산자

따배씨 - 따라하며 배우는 C언어

7강 분기

7.7 논리 연산자 Logical operators

#include <stdio.h>
#include <stdbool.h>

int main(){
    
    bool test1 = 3 > 2 || 5 > 6;    // true
    bool test2 = 3 > 2 && 5 > 6;    // false
    bool test3 = !(5 > 6);           // treu, equivalent to 5 <= 6
    
    printf("%d %d %d\n", test1, test2, test3);
  	// 1 0 1  
    return 0;
}

 

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

#define PERIOD '.'

int main(){
    char ch;
    int count = 0;
    
    while ((ch = getchar()) != PERIOD)
    {
        if (ch != '\n' && ch != ' ')
        {
            count++;
        }
    }
    
    printf("%d\n", count);
    return 0;
}

 

#include <stdio.h>
#include <stdbool.h>
#include <iso646.h>


int main(){
    bool test1 = 3 > 2 or 5 < 6;	//true
  	bool test2 = 3 > 2 and 5 > 6;	// false
  	bool test3 = not(5 > 6);
  
    printf("%d %d %d\n", test1, test2, test3);
  	// 1 0 1  
    return 0;
}
  • ios646.h 헤더를 사용하면 or, and, not 으로 사용가능
    • || == or
    • && == and
    • != == not

 

 

#include <stdio.h>

int main(){
    /*
        Short-circuit Evaluation
     
        - Logical expressions are evaluated from left to right
        - && and || are sequence points
     */
    
    int temp = (1 + 2) * (3 + 4);
    
    printf("Before : %d\n", temp);
    
    if (temp == 0 && (++temp == 1024))
    {
        //do nothing
    };
    
    printf("After : %d\n", temp);
    
    return 0;
}
  • Short-circuit Evaluation
    • && 와 || 는 Sequence point
    • temp == 0 && (++temp == 1024)
      • && 왼쪽의 연산이 참일 경우에만, && 오른쪽의 연산 수행
    • || 의 경우
      • 왼쪽이 참이면, || 오른쪽 확인 X
      • 왼쪽이 거짓일 경우에만, || 오른쪽 확인

 

 


이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.

http://blog.naver.com/atelierjpro

 

실리콘 밸리의 프로그래머 : 네이버 블로그

안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.

blog.naver.com

http://www.inflearn.com/course/following-c

 

홍정모의 따라하며 배우는 C언어 - 인프런 | 강의

'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원

www.inflearn.com

Comments