Subsections

Preface

Original Preface

At some point in 1979, I heard a lot of people talking about this relatively new language, C, and the book that had just come out about it. I bought a copy of K&R, otherwise known as The C Programming Language, by Brian Kernighan and Dennis Ritchie, but it sat on my shelf for a while because I didn't have an immediate need for it (besides which I was busy being a college freshman at the time.) It proved in the end to be an auspicious purchase, though, because when I finally did take it up, I never put it down: I've been programming in C ever since.

In 1983, I came across the Usenet newsgroup net.lang.c, which was (and its successor comp.lang.c still is) an excellent place to learn a lot more about C, to find out what questions everyone else is having about C, and to discover that you may not know all there is to know about C after all. It seems that C, despite its apparent simplicity, has a number of decidedly nonobvious aspects, and certain questions come up over and over again. This book is a collection of some of those questions, with answers, based on the Frequently Asked Questions (“FAQ”) list I began posting to comp.lang.c in May 1990.

I hasten to add, however, that this book is not intended as a critique or “hatched job” on the C language. It is all too easy to blame a language (or any tool) for the difficulties its users encounter with it or to claim that a properly designed tool “ought” to prevent its users from misusing it. It would therefore be easy to regard a book like this, with its long lists of misuses, as a litany of woes attempting to show that the language is hopelessly deficient. Nothing could be farther from the case.

I would never have learned enough about C to be able to write this book, and I would not be attempting to make C more pleasant for others to use by writing this book now, if I did not think that C is a great language or if I did not enjoy programming in it. I do like C, and one of the reasons I teach classes in it and spend time participating in discussion about in on the Internet is that I would like to discover which aspects of C (or of programming in general) are difficult to learn or keep people from being able to program efficiently and effectively. This book represents some of what I've learned: These questions are certainly some of the ones people have the most trouble with, and the answers have been refined over several years in an attempt to ensure that people don't have too much trouble with them.

A reader will certainly have trouble if there are any errors in these answers, and although the reviewers and I have worked hard to eliminate them, it can be as difficult to eradicate the last error from a large manuscript as it is to stamp out the last bug in a program. I will appreciate any corrections or suggestions sent to me in care of the publisher or at the e-mail address given, and I would like to offer the customary $1.00 reward to the first finder of any error. If you have access to the Internet, you can check for an errata list (and a scorecard of the finders) at the ftp and http addresses mentioned in question 20.40.

As I hope I've made clear, this book is not a critique of the C programming langauge, nor is it a critique of the book from which I first learned C or of that book's authors. I didn't just learn C from K&R; I also learned a lot of programming. As I contemplate my own contribution to the C programming literature, my only regret is that the present book does not live up to a nice observation made in the second edition of K&R, namely, that “C is not a big language, and it is not well served by a big book.” I hope that those who most deeply appreciate C's brevity and precision (and that of K&R) will not be too offended by the fact that this book says some things over and over and over or in three slightly different ways.

Although my name is on the cover, there are a lot of people behind this book, and it's difficult to know where to start handling out acknowledgements. In a sense, every one of comp.lang.c's readers (today estimated at 320,000) is a contributor: The FAQ list behind this book was written for comp.lang.c first, and this book retains the flavor of a good comp.lang.c discussion.

This book also retains, I hope, the philosophy of correct C programming that I began learning when I started reading net.lang.c. Therefore, I shall first acknowledge the posters who stand out in my mind as having most clearly and consistently articulated that philosophy: Doug Gwyn, Guy Harris, Karl Heuer, Henry Spencer, and Chris Torek. These gentlemen have displayed remarkable patience over the years, answering endless questions with generocity and wisdom. I was the one who stuct his neck out and started writing the Frequent questions down, but I would hate to give the impression that the answers are somehow mine. I was once the student (I believe it was Guy who answered my posting asking essentially the present volume's question 5.10), and I owe a real debt to the masters who went before me. This book is theirs as much as mine, though I retain title to any inadequacies or mistakes I've made in the presentation.

The former on-line FAQ list grew by a factor of three in the process of becoming this book, and its growth was a bit rapid and awkward at times. Mark Brader, Vinit Carpenter, Stephen Clamage, Jutta Degener, Doug Gwyn, Karl Heuer, Joseph Kent, and George Leach read proposals or complete drafts and helped to exert some control over the process; I thank them for their many careful suggestions and corrections. Their efforts grew out of a shared wish to improve the overall understanding of C in the programming community. I appreciate their dedication.

Three of those reviewers have also been long-time contributors to the on-line FAQ list. I thank Jutta Degener and Karl Heuer for their help over the years, and I especially thank Mark Brader, who has been my most persistent critic ever since I first began posting the comp.lang.c FAQ list five years ago. I don't know how he has had the stamina to make as many suggestions and corrections as he has and to overcome my continuing stubborn refusal to agree with some of them, even though (as I eventually understood) they really were improvements. You can thank Mark for the form of many of this book's explanations and blame me for mangling any of them.

Additional assorted thanks: to Susan Cyr for the cover art; to Bob Dinse and Eskimo North for providing the network access that is particularly vital to a project like this; to Bob Holland for providing the computer on which I've done most of the writing; to Pete Keleher for the Alpha text editor; to the University of Washington Mathematics Research and Engineering libraries for access to their collections; and to the University of Washington Oceanoraphy department for letting me borrow their tape drivers to access my dusty old archives of Usenet postings.

Thanks to Tanmoy Bhattacharya for the example in question 11.10, to Arjan Kenter for the code in question 13.7, to Tomohiko Sakamoto for the code in question 20.31, and to Roger Miller for the line in question 11.35.

Thanks to all these people, all over the world, who have contributed to the FAQ list in various ways by offering suggestions, corrections, constructive criticism, or other support: Jamshid Afshar, David Anderson, Tanner Andrews, Sudheer Apte, Joseph Arceneaux, Randall Atkinson, Rick Beem, Peter Bennett, Wayne Berke, Dan Bernstein, John Bickers, Gary Blaine, Yuan Bo, Dave Boutcher, Michael Bresnahan, Vincent Broman, Stan Brown, Joe Buehler, Kimberley Burchett, Gordon Burditt, Burkhard Burow, Conor P. Cahill, D'Arcy J.M. Cain, Christopher Calabrese, Ian Cargill, Paul Carter, Mike Chambers, Billy Chambless, Franklin Chen, Jonathan Chen, Raymond Chen, Richard Cheung, Ken Corbin, Ian Cottam, Russ Cox, Jonathan Coxhead, Lee Crawford, Steve Dahmer, Andrew Daviel, James Davies, John E. Davis, Ken Delong, Norm Diamond, Jeff Dunlop, Ray Dunn, Stephen M. Dunn, Michael J. Eager, Scott Ehrlich, Arno Eigenwillig, Dave Eisen, Bjorn Engsig, David Evans, Clive D.W. Feather, Dominic Feeley, Simao Ferraz, Chris Flatters, Rod Flores, Alexander Forst, Steve Fosdick, Jeff Francis, Tom Gambill, Dave Gillespie, Samuel Goldstein, Tim Goodwin, Alasdair Grant, Ron Guilmette, ... I have tried to keep track of everyone whose suggestions I have used, but I fear I've probably overlooked a few; my apologies to anyone whose name should be on this list but isn't.

Finally, I'd like to thank my editor at Addison-Wesley, Debbie Lafferty, for tapping me on the electronic shoulder one day and asking if I might be interested in writing this book. I was, and you now hold it, and I hope that it may help to make C programming as pleasant for you as it is for me.

Seattle, Washington
July, 1995

Steve Summit
summit@aw.com

이 문서는 Steve Summit씨에게 저작권(copyright)이 있습니다. 이 문서의 내용은 “C Programming FAQs: Frequently Asked Questions”의 저자와 출판사의 사회에 봉사하고자 하는 뜻에서 여러분에게 제공되는 것입니다.

이 문서는 원책인 “C Programming FAQs: Frequently Asked Questions”의 내용을 보충하고자 하는 뜻에서 만들어진 것이며, 국제법에 의해 보호됩니다. 이 문서는 개인적인 목적으로는 이 곳에서 자유롭게 열람할 수 있지만 허가없이 출판될 수는 없습니다.

특정 주제들에 대한 질문은 이 뉴스 그룹에 자주 올라옵니다. 이들은 매우 좋은 질문이며, 어떤 것들은 즉각 답변하기 매우 어려운 난해한 것들입니다. 하지만 이러한 질문들이 자주 올라온다면 네트워크에 과부하가 걸릴 수도 있고, 독자들은 매번 같은 답변을 읽느라 번거로울 것이며, 잘못된 답변에 대한 지겨운 정정 작업이 발생할지도 모릅니다.

이 문서는 이러한 일반적인 문제들에 대한 정확하고 간결한 답변을 제시하고자 매달 게시되며, 넷 토론(net discussion)이 이러한 기본적인 문제에 대한 지속적인 반복없이 좀 더 생산적인 질문에 전념하는데에 그 목적이 있습니다.

아무리 뉴스 그룹의 글이 좋다고 하더라도 언어 참고서(language reference manual)나 튜토리얼(tutorial)을 주의 깊게 읽는 것만 못합니다. C 언어에 관심이 있고, 이 뉴스 그룹에 참여하길 원한다면 당연히 이러한 책들을 반복하여 읽고 연구하는 것에도 관심을 가져야 할 것입니다. 어떤 C 책들과 컴파일러 매뉴얼들은 불행하게도 완전하지 않습니다. A few even perpetuate some of the myths which this article attempts to refute. 유명한 여러 책들에 대해서는 이 글의 `참고문헌란'에 나열되어 있습니다. 질문 18.9와 18.10을 참고하시기 바랍니다. 또한 많은 질문들과 답변들이 이러한 책들과 연결되어 있습니다. 관심있는 독자들은 참고하시기 바랍니다.

여러분이 이 문서에 나와 있지 않는, C 언어에 대한 질문이 있다면 넷 그룹에 게시하기 전에 이러한 참고 서적들에서 먼저 답을 찾아보거나, 주변의 동료들에게 도움을 청해보시기 바랍니다. 넷 그룹에는 답변하기를 즐기는 많은 사람들이 있지만, 증가되고 있는 질문 갯수와 같은 질문에 대한 반복적인 답변의 분량을 생각하면 그 사람들에게 너무나도 가혹한 일입니다. 또한 이 문서에 대한 질문이나 제안 사항이 있다면 전자 우편을 사용하여 알려주시기 바랍니다. 이 문서는 넷 트래픽을 감소시키기 위한 것이지, 증가시키기 위한 것이 아니기 때문입니다.

이 문서는 자주 물어보는 질문들만 나열한 것이 아니라 각 질문에 대한 답변들도 포함하고 있습니다. 여러분이 이들 답변에 대해 이미 알고 있더라도 한번 죽 훑어보는 것을 추천합니다. 그리고 이러한 질문이 뉴스 그룹에 게시되더라도 답변하기 위해 시간을 낭비할 필요는 없습니다. (그러나, 이 문서는 매우 방대하고, 무거운 주제를 다루기 때문에 뉴스그룹의 모든 사람들이 이 문서를 자세히 읽고 질문할 것이라고 기대하지는 마시기 바랍니다. 그리고 이러한 사람들에게 욕설을 한다거나 하는 행동은 자제해 주시기 바랍니다)

이 문서는 1999년 2월 7일에 마지막으로 수정되었습니다. 만약 여러분이 인쇄된 형식의 문서를 보거나, CD-ROM 또는 아카이브 사이트(archive site)에서 얻은 문서를 보신다면 먼저 최신의 문서를 다음 사이트에서 얻으시기 바랍니다.

  http://www.eskimo.com/~scs/C-faq/top.html
  http://www.faqs.org/faqs/

또는 질문 20.40에 언급된 FTP 사이트를 참고하시기 바랍니다. 그리고 이 목록은 자주 수정되기 때문에 질문 번호가 오래된 목록과 새 목록이 서로 다를 수 있습니다. 따라서 번호로 목록을 찾을 때 주의하시기 바랍니다.

이 문서는 자유로이 배포되는 것이므로 이 문서를 얻기 위해 어떠한 추가 비용을 지불할 필요는 없습니다.

이 문서의 다른 형식도 얻을 수 있습니다. 이전 문서와 새 문서의 차이점만을 명시한 짧은 형식도 준비되어 있으며, 하이퍼텍스트(hypertext) 버전도 위에 언급한 URL에 준비되어 있습니다. 마지막으로, 인쇄된 형식을 선호하는 사람들을 위해 책으로도 출판되어 있습니다. (책의 경우 좀 더 자세한 답변과 더 많은 질문과 답변이 수록되어 있습니다). 이 책은 Addison-Wesley사에서 출판되었으며 ISBN은 0-201-84519-9입니다.

이 문서는 계속 수정되고 있습니다. 여러분의 참여를 환영합니다. 여러분의 제안 사항을 scs@eskimo.com으로 보내주시기 바랍니다.

답변된 질문들은 다음 주제별로 나뉘어져 있습니다.

  1. 선언과 초기화
  2. 구조체, Unions, 열거형(enumerations)
  3. 식(Expressions)
  4. 포인터
  5. 널(null) 포인터
  6. 배열과 포인터
  7. 메모리 할당(allocation)
  8. 문자와 문자열
  9. Boolean 식과 변수
  10. C 전처리기(preprocessor)
  11. ANSI/ISO C 표준(standard)
  12. 표준 입출력(stdio)
  13. 라이브러리 함수
  14. 실수 (floating point)
  15. 가변 인자 리스트(Variable-Length Argument Lists)
  16. 이상한 문제들
  17. 스타일
  18. 툴과 기타 자료
  19. 시스템 의존성
  20. 기타(miscellaneous)
  21. 참고문헌
  22. Acknowledgements

각 단원의 질문 번호는 이 문서에서 연속적이지 않을 수 있습니다. 왜냐하면 이 질문들은 책에서 뽑은 것이기 때문에, 책과 같은 번호를 유지하고자 하기 때문입니다.)

About Translation

이 문서는 Steve Summit씨의 `C Programming FAQs'를 한국어로 번역한 것입니다. 이 한국어판 문서의 내용은 번역자에게 그 저작권이 있으며, 개인적인 목적으로는 자유로이 사용될 수 있지만 허가없이 출판되거나 홍보용, 상업용 등의 목적으로는 사용될 수 없습니다. 또한 (개인적인 목적을 포함) 어떠한 이유에서라도 이 문서의 일부만이 사용되는 것을 금합니다.

이 문서에 대한 조언, 비판, 정정은 언제든지 환영하며 이러한 사항은 아래 e-mail 주소로 보내시기 바랍니다:

  cinsky at gmail.com

위의 주소에 보낼 수 없다면 다음 주소0.1로 보내시기 바랍니다:

  tanarrian at yahoo dot co dot kr

물론 위에서 제공한 모든 e-mail 주소에서 `at'은 `@'로, `dot'은 `.'로 바꾸어야 합니다. (스팸 메일을 방지하기 위한 조치이므로 양해바랍니다.) 번역자의 홈 페이지를 방문하면 항상 최신의 연락처를 얻을 수 있습니다:

  http://www.cinsk.org/cfaqs/

Translation

이 문서는 Steve씨의 원문의 느낌을 그대로 전달하는 것을 목표로 하고 있습니다. 따라서 번역하기 곤란하거나, 우리말로 마땅한 단어가 없는 것은 영어를 그대로 쓰고 있습니다.

이 글에서 Unix, UNIX 등은 모두 같은 뜻이며 특정 UNIX 시스템이 아닌 일반적인(general) UNIX 시스템을 의미하는 단어입니다.

역자는 저자에게서 질문 번호를 바꾸지 않은 한, 새로운 질문/답변을 추가할 수 있다는 허락을 얻었습니다. 질문 번호가 숫자가 아닌, 알파벳으로 된 질문은 역자가 추가한 질문입니다.

Current Status - Very Important

오리지널 Steve씨의 원문은 현재, [C99] 표준이 완벽하게 제정되기 전의 상태에 쓰여졌습니다. 역자가 추가한 [C99] 표준에 관한 것은 현재 reference로 [C99]를 사용합니다. Steve씨가 쓴 원문은 현재 [C9X]를 씁니다. [C9X]는 이 글이 개정되면서 [C99]로 바뀔 예정입니다.

오리지널 Steve씨의 출판된 책은 [ANSI]에 대한 reference가 많이 나와 있습니다만, 온라인 버전은 [ANSI] reference가 제공되지 않습니다. 왜냐하면 [ANSI]는 더 이상 공식 C 표준이 아니기 때문입니다. (질문 [*]11.1을 참고하기 바랍니다.) 그러나 편의상 모든 질문에 책에서 얻은 [ANSI] reference도 추가합니다. 최신 정보를 얻으실 분들은 더 이상 [ANSI]나 [C89], [C9X], [ANSI Rationale], 그리고 [H&S]를 참고하면 안됩니다. (이들은 모두 구식의 C 표준을 따르고 있기 때문에, 최신 C 표준인 [C99]와 다른 부분이 있습니다.)

최신 C99에서 제공하는 여러가지 추가 기능에 대해서는 아직 이 글이 지원하지 않습니다. 여기에는 variable-length array, designator (structure) 등이 포함되며 곧 추가할 예정입니다. 또한 좀 더 자세한, 최신의 정보를 얻고 싶다면, 꼭 아래 site를 방문해서 최신의 정보를 얻기 바랍니다.

  http://www.cinsk.org/cfaqs/

Acknowledgements

이 글을 만드는 데 도움을 주신 모든 분들께 감사를 드립니다. Steve Summit씨와 마찬가지로, 저도 Usenet의 많은 분들에게 도움을 받았습니다. 특히 comp.lang.c, comp.unix.programmer 뉴스 그룹의 모든 분들께 감사를 드립니다. 마찬가지로, 여러가지 정보를 얻을 수 있었던, KLDP BBS0.2의 모든 분들께도 감사드립니다.

문서 작성에 쓰인 LATEX에 대한 도움을 주신 KTUG0.3의 모든 분께도 감사드립니다.

lovewar님과, Hyun-Kyung Lee님이 각각 여러 오타를 수정해 주셨습니다.

Yong-Uk Kim님, 김성훈(niky)님, 윤희수(cppig1995)님이 각각 본문에서 한 가지 문제점을 지적해 주셨습니다.

임성훈(wariua)님이 약 80여개의 오타를 지적해 주셨습니다.

아울러 혹시라도 제가 잊고 빠트린 분이 있다면 꼭 연락 주시기 바랍니다.

Seong-Kook Shin
2018-05-28