[완료]c공용체 질문입니다.

ikkart의 이미지

1 #include
2
3 typedef union _byte{
4 unsigned char byte;
5 struct _bit {
6 unsigned char B0 : 1;
7 unsigned char B1 : 1;
8 unsigned char B2 : 1;
9 unsigned char B3 : 1;
10 unsigned char B4 : 1;
11 unsigned char B5 : 1;
12 unsigned char B6 : 1;
13 unsigned char B7 : 1;
14 } bit;
15 } byte;
16
17 int main(void){
18 byte data;
19
20 printf("0-255 input \n");
21 printf("input 10jinsoo ===> 2jinsoo print\n");
22 printf("[10jin] :");
23 scanf("%d", &data.byte);
24 // data.byte = 1;
25 printf("2jinsoo => ");
26 printf("%d", data.bit.B7);
27 printf("%d", data.bit.B6);
28 printf("%d", data.bit.B5);
29 printf("%d", data.bit.B4);
30 printf("%d", data.bit.B3);
31 printf("%d", data.bit.B2);
32 printf("%d", data.bit.B1);
33 printf("%d", data.bit.B0);
34 printf("\n");
35 return 0;
36 }

위에 처럼 코딩을 했는데 리눅스환경에서 gcc로 컴파일 하면
값을 입력했을때 메모리 덤프가 일어나고
VC++로 하면 이상없이 잘되네요. 왜 VC++에서는 되는데 gcc에서는
메모리 덤프가 일어 날까요?
그래서 입력받지 않고 직접 data.byte로 값을 넣어주니 gcc에서는
이상한값이 나오고 VC++에서는 제대로 된값이 나오네요
왜 VC++와 gcc에서 결과값이 다를까요?

whitelazy의 이미지

우분투 6.06 LTS에
GCC 4.0.3 에서 이상없이 잘 되는군요..
255까지도 잘 나오는데요?

whitelazy@huro:~/program$ ./dec2bin
0-255 input
input 10jinsoo ===> 2jinsoo print
[10jin] :0
2jinsoo => 00000000
whitelazy@huro:~/program$ ./dec2bin
0-255 input
input 10jinsoo ===> 2jinsoo print
[10jin] :3
2jinsoo => 00000011
whitelazy@huro:~/program$ ./dec2bin
0-255 input
input 10jinsoo ===> 2jinsoo print
[10jin] :4
2jinsoo => 00000100
whitelazy@huro:~/program$ ./dec2bin
0-255 input
input 10jinsoo ===> 2jinsoo print
[10jin] :255
2jinsoo => 11111111
whitelazy@huro:~/program$

vc가 안깔려있어서 vc에서 테스트는 패스.. -_-a

ikkart의 이미지

이게 학교 서버에서 한거라;ㅎㅎ
아무래도 학교에서 gcc버젼이 옛날거라 그런것 같네요;
댓글 달아주셔셔 감사합니다^^;

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.