g++ 과 vc++ 컴파일러 차이점 질문

hyunuck의 이미지

제가 C++ 을 공부하는데요, 컴파일러는 g++ 을 쓰고 있습니다.
근데 아래와 같은 코드를 g++ 로 컴파일하면

#include <iostream.h>

void main()
{
	int number;
	number = 0x3FF;

	cout << "Dec: " << number << '\n';

	cout.setf(ios::hex);
	cout << "Hex: " << number << '\n';
	cout.setf(ios::dec);
}


Dec: 1023
Hex: 1023 

이렇게 나오구요.
VC++ 로 컴파일하면 의도한데로,

Dec: 1023
Hex: 3ff

이렇게 나옵니다.
왜 이렇게 나오는 건가요?
g++ 에서도 정상적으로 볼수 있는 방법은 없을까요?

그리고 추가로 이러한 컴파일러간의 차이점들에는 무엇무엇이 있으며,
혹시 이런곳을 정리해놓은곳은 없나요?

고수님들 한 수 부탁드립니다.
즐거운 설 연휴 되세요~!! :D

cinsk의 이미지

고칠 부분이 많군요.

<iostream.h> -> 예전 방식입니다.

표준 C++에서는

#include <iostream>

로 써야 합니다.

그리고

void main()도 잘못된 방식입니다.

int main()로 써야 합니다.

그리고 뭐 틀렸다고는 할 수 없지만,

hex, dec과 같은 format flag들은 ios_base에 속해 있습니다.

ios_base::dec, ios_base::hex이라고 쓰는 게 났겠죠.

또 setf는

fmtflags ios_base::setf(fmtflags flags);

이렇게 선언되어 있고. 단순히 주어진 flags을 setting하는 함수입니다.
C++ 표준에 보면 단순히 다음과 같이 나와있습니다:

fmtflags setf(fmtflags fmtfl);
4. Effects: Sets fmtflin flags(). 
5. Returns: The previous value of flags().

dec, hex, oct와 같은 flag은 함께 쓸 수 없는 것이므로,

dec이 on되어 있는데, setf(ios_base::hex)를 호출하면,
dec과 hex가 같이 setting되어 버립니다.

이때의 결과는? 알 수 없죠.
출력하는 부분에 dec을 먼저 검사한다면 10진수로 출력될 것이고,
hex를 먼저 검사한다면 16진수를 출력하겠죠.

그리고 '\n'을 쓰셨는데, 의도적으로 쓰신게 아니라면
endl을 쓰시는게 좋습니다. endl은 flushing 기능이 있으니까요.
어쨌거나 완전한 코드는 다음과 같습니다:

#include <iostream>

using namespace std;

int
main(void)
{
  int number;
  number = 0x3FF;

  cout << "Dec: " << number << endl;

  cout.setf(ios_base::hex, ios_base::basefield);
  cout << "Hex: " << number << endl;
  cout.setf(ios_base::dec, ios_base::basefield);
}

근데 위와 같이 한다면 좀 귀찮죠. 타이핑하기가..
그래서 저라면 다음과 같이 할 것 같습니다 (나머지 코드는 위와 같고,
위의 코드에서 cout하는 부분만):

  cout << "Dec: " << number << endl;
  cout << "Hex: " << hex << number << dec << endl;

마지막으로, 컴파일러간의 차이점을 물으셨는데,
글쎄요. VC 같은 경우 아직 표준 C++을 제대로 지원하지 않는 것으로
알고 있습니다. 특히 template 부분에서..

표준 C++ 문서를 구입해서 읽어보는 것도 좋습니다.
PDF 버전은 비싸지도 않으니까요. 아래 URL 참고.

http://www.techstreet.com/cgi-bin/detail?product_id=49964

그럼 이만.

musiphil의 이미지

굳이 줄마다 flushing이 필요한 것이 아니면 endl 대신 '\n'을 써도 나쁠 것은 없겠지요. ;-)

댓글 달기

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