inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

홍정모의 따라하며 배우는 C언어

6.6 _Bool 자료형

Boolean을 만들어서 사용하는 이유가 뭐에요?

817

Creed

작성한 질문수 22

0

그냥 변수명을 int true = 1, false = 0;

이라고 지어서

printf("True is %d\n",true);

printf("False is %d\n", false);

이렇게 출력하는거랑 똑같지 않나요?

왜 사람들이 bool타입을 만들어서 사용하는건가요?

c

답변 1

0

강민철

그렇게 사용하셔도 됩니다.

실제로 아래 소스 코드를 어셈블리어로 변환했을 때와

 

  1 #include <stdio.h>
  2 #include <stdbool.h>
  3
  4 int main()
  5 {
  6     int i_true = 1;
  7     int i_false = 0;
  8
  9     // bool b_true = true;
 10     // bool b_false = false;
 11
 12     printf("True is %d\n", i_true);
 13     printf("False is %d\n", i_false);
 14
 15     // printf("True is %d\n", b_true);
 16     // printf("False is %d\n", b_false);
 17
 18     return 0;
 19 }

 

아래와 소스 코드를 어셈블리어로 변환했을 때의 결과는 동일합니다.

  1 #include <stdio.h>
  2 #include <stdbool.h>
  3
  4 int main()
  5 {
  6     // int i_true = 1;
  7     // int i_false = 0;
  8
  9     bool b_true = true;
 10     bool b_false = false;
 11
 12     // printf("True is %d\n", i_true);
 13     // printf("False is %d\n", i_false);
 14
 15     printf("True is %d\n", b_true);
 16     printf("False is %d\n", b_false);
 17
 18     return 0;
 19 }

위 코드의 어셈블리어

  1         .section        __TEXT,__text,regular,pure_instructions
  2         .build_version macos, 12, 0     sdk_version 12, 3
  3         .globl  _main                           ; -- Begin function main
  4         .p2align        2
  5 _main:                                  ; @main
  6         .cfi_startproc
  7 ; %bb.0:
  8         sub     sp, sp, #32
  9         stp     x29, x30, [sp, #16]             ; 16-byte Folded Spill
 10         add     x29, sp, #16
 11         .cfi_def_cfa w29, 16
 12         .cfi_offset w30, -8
 13         .cfi_offset w29, -16
 14         mov     w8, #1
 15         str     x8, [sp]
 16 Lloh0:
 17         adrp    x0, l_.str@PAGE
 18 Lloh1:
 19         add     x0, x0, l_.str@PAGEOFF
 20         bl      _printf
 21         str     xzr, [sp]
 22 Lloh2:
 23         adrp    x0, l_.str.1@PAGE
 24 Lloh3:
 25         add     x0, x0, l_.str.1@PAGEOFF
 26         bl      _printf
 27         mov     w0, #0
 28         ldp     x29, x30, [sp, #16]             ; 16-byte Folded Reload
 29         add     sp, sp, #32
 30         ret
 31         .loh AdrpAdd    Lloh2, Lloh3
 32         .loh AdrpAdd    Lloh0, Lloh1
 33         .cfi_endproc
 34                                         ; -- End function
 35         .section        __TEXT,__cstring,cstring_literals
 36 l_.str:                                 ; @.str
 37         .asciz  "True is %d\n"
 38
 39 l_.str.1:                               ; @.str.1
 40         .asciz  "False is %d\n"
 41
 42 .subsections_via_symbols

 

아래 코드의 어셈블리어

  1         .section        __TEXT,__text,regular,pure_instructions
  2         .build_version macos, 12, 0     sdk_version 12, 3
  3         .globl  _main                           ; -- Begin function main
  4         .p2align        2
  5 _main:                                  ; @main
  6         .cfi_startproc
  7 ; %bb.0:
  8         sub     sp, sp, #32
  9         stp     x29, x30, [sp, #16]             ; 16-byte Folded Spill
 10         add     x29, sp, #16
 11         .cfi_def_cfa w29, 16
 12         .cfi_offset w30, -8
 13         .cfi_offset w29, -16
 14         mov     w8, #1
 15         str     x8, [sp]
 16 Lloh0:
 17         adrp    x0, l_.str@PAGE
 18 Lloh1:
 19         add     x0, x0, l_.str@PAGEOFF
 20         bl      _printf
 21         str     xzr, [sp]
 22 Lloh2:
 23         adrp    x0, l_.str.1@PAGE
 24 Lloh3:
 25         add     x0, x0, l_.str.1@PAGEOFF
 26         bl      _printf
 27         mov     w0, #0
 28         ldp     x29, x30, [sp, #16]             ; 16-byte Folded Reload
 29         add     sp, sp, #32
 30         ret
 31         .loh AdrpAdd    Lloh2, Lloh3
 32         .loh AdrpAdd    Lloh0, Lloh1
 33         .cfi_endproc
 34                                         ; -- End function
 35         .section        __TEXT,__cstring,cstring_literals
 36 l_.str:                                 ; @.str
 37         .asciz  "True is %d\n"
 38
 39 l_.str.1:                               ; @.str.1
 40         .asciz  "False is %d\n"
 41
 42 .subsections_via_symbols

 

요컨대 true, false를 나타내는 불리언과

정수형임을 나타내는 정수형을 구분하여 코드를 읽고 쓰기 위해

사용한다고 보여집니다.

Export template 안됨

1

65

2

완전히 똑같이 따라해도 exe파일이 안만들어져서 실행이 안됩니다.

1

96

3

main 함수에서 왜 int만 선언이 되는걸까요

1

84

2

8비트 2진수 변환시 왜 1을 더해야하나요?

1

81

2

혹시 강의를 빠르게 수강하려면 어디서부터 듣는게 좋을까요?

1

81

1

프로토타입과 함수간의 인자 불일치

1

87

2

12.12 헤더 관련 질문

1

74

2

Visual Studio Community 2026 사용 문의

1

171

2

Q. 15:30, 부호가 있는 8비트 정수 질문

1

73

2

getchar(), putchar()

1

112

3

강의자리ㅛ

1

93

2

비주얼스튜디오코드로 공부해도 상관없나요?

1

131

2

소스파일안에 여러 파일

1

88

2

F5와 F7의 차이

1

91

2

c = TWO * (a+b); 에서 a와 b는?

1

68

2

; 세미콜론을 붙이는 기준에 문의

1

78

1

Step over 기능 문의

1

66

2

2.6 강의 따옴표 출력 규칙 문의

1

87

2

int main 함수 관련 오류 문의

1

82

2

13.4 words[0]

0

73

2

11.7 함수를 구현해 봤습니다.

1

67

2

11.6 직접 strcmp와 strncmp를 구현해 보았습니다.

1

71

2

11.6 my_strcat과 my_strncat을 구현해봤습니다.

1

62

2

11.6 fit_str함수를 구현해 봤습니다.

1

60

2