C언어 txt 읽어올 때 char, int 질문드립니다.

김승민@Google의 이미지

txt 파일에서 5 란 숫자가 저장되어있고 이것을 코드로 읽어와서 커맨드 창에 출력하려합니다.

두가지 방법으로 해봤습니다.

1.
int TC;

fscanf(fp, "%d\n", &TC);
printf("%d\n", TC);

2.
int temp;

fscanf(fp, "%c\n", &temp);
printf("%c\n", temp);

둘 다 출력 결과는 5가 나왔습니다.

궁금한점은 1번에서 만약 5가 char 형식으로 저장되어있다면

5에해당하는 아스키코드 53 값을 자동으로 int형 숫자 5로 변환해주어 저장해주는 것인지 궁금합니다.

세벌의 이미지

printf("%d\n", TC);


printf("(%d)(%c)\n", TC, TC);

이런 식으로 바꾸어 보면 힌트가 보일 거 같습니다.
김승민@Google의 이미지

감사합니다~!

shint의 이미지

http://codepad.org/YSeuqLTe

#include <stdio.h>
 
int main()
{
    int i = 0;
    int k = 0;
    int a = 5;
    char b = a;
    printf("변수 크기 : %d %d\n", sizeof(a), sizeof(b));
    printf("%d %c %x %d %c %x\n", a, a, a, b, b, a);
 
    printf("\n");
    printf("비트 값 확인 방법\n");
    unsigned char c = 0;
    for(k=0; k<8; k++)
    {
        c = k;
        printf("%d ", c);
        //비트 순서를 오른쪽에서 보기 위해서.
        //for(i=0; i<4; i++) 를 아래 처럼 변경
        for(i=3; i>=0; i--)
        {
            printf("%x", c>>i & 1);
        }
        printf("\n");
    }
    return 0;
}
 
#if 0
작은 변수에 큰 변수의 데이터를 넣으면. 묵시적인 형변환이 일어납니다.
https://msdn.microsoft.com/ko-kr/library/s3f49ktz.aspx?f=255&MSPPError=-2147217396
 
signed char 의 범위는 -128 ~ 127
unsigned char 의 범위는 0 ~ 255
 
signed int      –2,147,483,648 ~ 2,147,483,647
unsigned int	0 ~ 4,294,967,295
 
1 바이트에 4바이트를 넣으면. 1바이트만 저장 됩니다.
#endif
 
변수 크기 : 4 1
5  5 5  5
 
비트 값 확인 방법
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111

//----------------------------------------------------------
//255 개이고. 8 비트인데... 0x01 이고...
//위에 코드를 잘못 적었네요. ㅇ_ㅇ;; 다시 올려봅니다.
//----------------------------------------------------------
http://codepad.org/TNbVwSsS

#include <stdio.h>
 
int main()
{
    int i = 0;
    int k = 0;
    int a = 5;
    char b = a;
    printf("변수 크기 : %d %d\n", sizeof(a), sizeof(b));
    printf("%d %c %x %d %c %x\n", a, a, a, b, b, a);
 
    printf("\n");
    printf("비트 값 확인 방법\n");
    unsigned char c = 0;
    for(k=0; k<=256; k++)
    {
        c = k;
        printf("%3d ", c);
        //비트 순서를 오른쪽에서 보기 위해서.
        //for(i=0; i<4; i++) 를 아래 처럼 변경
        for(i=8; i>=0; i--)
        {
            printf("%x", c>>i & 0x01);
        }
        printf("\n");
    }
    return 0;
}
 
#if 0
작은 변수에 큰 변수의 데이터를 넣으면. 묵시적인 형변환이 일어납니다.
https://msdn.microsoft.com/ko-kr/library/s3f49ktz.aspx?f=255&MSPPError=-2147217396
 
signed char 의 범위는 -128 ~ 127
unsigned char 의 범위는 0 ~ 255
 
signed int      –2,147,483,648 ~ 2,147,483,647
unsigned int	0 ~ 4,294,967,295
 
1 바이트에 4바이트를 넣으면. 1바이트만 저장 됩니다.
#endif
 
변수 크기 : 4 1
5  5 5  5
 
비트 값 확인 방법
  0 000000000
  1 000000001
  2 000000010
  3 000000011
  4 000000100
  5 000000101
  6 000000110
  7 000000111
  8 000001000
  9 000001001
 10 000001010
 11 000001011
 12 000001100
 13 000001101
 14 000001110
 15 000001111
 16 000010000
 17 000010001
 18 000010010
 19 000010011
 20 000010100
 21 000010101
 22 000010110
 23 000010111
 24 000011000
 25 000011001
 26 000011010
 27 000011011
 28 000011100
 29 000011101
 30 000011110
 31 000011111
 32 000100000
 33 000100001
 34 000100010
 35 000100011
 36 000100100
 37 000100101
 38 000100110
 39 000100111
 40 000101000
 41 000101001
 42 000101010
 43 000101011
 44 000101100
 45 000101101
 46 000101110
 47 000101111
 48 000110000
 49 000110001
 50 000110010
 51 000110011
 52 000110100
 53 000110101
 54 000110110
 55 000110111
 56 000111000
 57 000111001
 58 000111010
 59 000111011
 60 000111100
 61 000111101
 62 000111110
 63 000111111
 64 001000000
 65 001000001
 66 001000010
 67 001000011
 68 001000100
 69 001000101
 70 001000110
 71 001000111
 72 001001000
 73 001001001
 74 001001010
 75 001001011
 76 001001100
 77 001001101
 78 001001110
 79 001001111
 80 001010000
 81 001010001
 82 001010010
 83 001010011
 84 001010100
 85 001010101
 86 001010110
 87 001010111
 88 001011000
 89 001011001
 90 001011010
 91 001011011
 92 001011100
 93 001011101
 94 001011110
 95 001011111
 96 001100000
 97 001100001
 98 001100010
 99 001100011
100 001100100
101 001100101
102 001100110
103 001100111
104 001101000
105 001101001
106 001101010
107 001101011
108 001101100
109 001101101
110 001101110
111 001101111
112 001110000
113 001110001
114 001110010
115 001110011
116 001110100
117 001110101
118 001110110
119 001110111
120 001111000
121 001111001
122 001111010
123 001111011
124 001111100
125 001111101
126 001111110
127 001111111
128 010000000
129 010000001
130 010000010
131 010000011
132 010000100
133 010000101
134 010000110
135 010000111
136 010001000
137 010001001
138 010001010
139 010001011
140 010001100
141 010001101
142 010001110
143 010001111
144 010010000
145 010010001
146 010010010
147 010010011
148 010010100
149 010010101
150 010010110
151 010010111
152 010011000
153 010011001
154 010011010
155 010011011
156 010011100
157 010011101
158 010011110
159 010011111
160 010100000
161 010100001
162 010100010
163 010100011
164 010100100
165 010100101
166 010100110
167 010100111
168 010101000
169 010101001
170 010101010
171 010101011
172 010101100
173 010101101
174 010101110
175 010101111
176 010110000
177 010110001
178 010110010
179 010110011
180 010110100
181 010110101
182 010110110
183 010110111
184 010111000
185 010111001
186 010111010
187 010111011
188 010111100
189 010111101
190 010111110
191 010111111
192 011000000
193 011000001
194 011000010
195 011000011
196 011000100
197 011000101
198 011000110
199 011000111
200 011001000
201 011001001
202 011001010
203 011001011
204 011001100
205 011001101
206 011001110
207 011001111
208 011010000
209 011010001
210 011010010
211 011010011
212 011010100
213 011010101
214 011010110
215 011010111
216 011011000
217 011011001
218 011011010
219 011011011
220 011011100
221 011011101
222 011011110
223 011011111
224 011100000
225 011100001
226 011100010
227 011100011
228 011100100
229 011100101
230 011100110
231 011100111
232 011101000
233 011101001
234 011101010
235 011101011
236 011101100
237 011101101
238 011101110
239 011101111
240 011110000
241 011110001
242 011110010
243 011110011
244 011110100
245 011110101
246 011110110
247 011110111
248 011111000
249 011111001
250 011111010
251 011111011
252 011111100
253 011111101
254 011111110
255 011111111
  0 000000000

//1byte 8bit 로 구글 검색
1BYTE 가 반드시 8 BIT 는 아니라고 하네요. ㅇ_ㅇ;;

https://blog.perfectacle.com/2017/08/07/why-1byte-is-8bit/
http://zepeh.tistory.com/313
http://mindnet.tistory.com/entry/%EB%84%A4%ED%8A%B8%EC%9B%8C%ED%81%AC-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0-1%ED%8E%B8-Bit-%EC%99%80-Byte-%EC%B0%A8%EC%9D%B4%EC%A0%90
https://m.blog.naver.com/PostView.nhn?blogId=pmpkjh&logNo=70867749&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F
http://bitwuool.tistory.com/entry/1byte-%EB%8A%94-8%EB%B9%84%ED%8A%B8%EC%9E%96%EC%95%84%EC%9A%94%EA%B7%B8%EB%9F%BC-%EC%B5%9C%EB%8C%80%ED%91%9C%ED%98%84%EC%88%98%EB%8A%94-1111-1111-%EC%9E%85%EB%8B%88%EA%B9%8C
https://kbench.com/?q=node/2635
http://www.incodom.kr/%EB%8D%B0%EC%9D%B4%ED%84%B0_%EB%8B%A8%EC%9C%84
https://kldp.org/node/153459
http://feelpass.tistory.com/324
http://tip.daum.net/question/53804114

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

김승민@Google의 이미지

감사합니다~!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.