Andrew koenig c traps and pitfalls

160 249 1
Andrew koenig   c traps and pitfalls

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Đây là quyển sách tiếng anh về lĩnh vực công nghệ thông tin cho sinh viên và những ai có đam mê. Quyển sách này trình về lý thuyết ,phương pháp lập trình cho ngôn ngữ C và C++.

Ira s and Pitfalls ~ ANDREW KOENIG AT&T Bell Laboratories ADDISON"WESLEY PUBLISHING COMPANY Reading, Massachusetts • Menlo Park, California • Sydney Don Mills, Ontario • Madrid • San Juan • New York • Singapore Amsterdam • Wokingham, England • Tokyo • Bonn Library of Congress Cataloging-in-Publication Data Koenig, Andrew. C traps and pitfalls. Incluqes index. 1. C (Computer program QA76.73.C15K67 1989 ISBN 0-201-17928-~ - - - - - - - - AT.T language) 005.26 I. Title. 88-16616 Copyright @ 1989 by AT&T Bell Laboratories, All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechan- ical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Published simultane- ously in Canada. This book was typeset in Palatino and Courier by the author, using an Autologic APS-5 phototypesetter and a DEC Micro VAX II c()mputer running the 9th Edition of the UNIX operating system. UNIX is a registered trademark of AT&T. DEC, PDP, and VAX are trademarks of Digital Equipment Corporation. ABCDEFGHIJ-HA-898 To Barbara~ who for too long has had fa endure a house full of drafts. > [...]... looks like a comment Such older compilers also treat compound Such a compiler will handle a » = assignments as two tokens 1; with no problem but a strict ANSI C compiler will reject it 1.4 Integer constants If the first character of an integer constant is the digit 0, that constant is taken to be in octal Thus 10 and 010 mean very different things Moreover, many C compilers accept 8 and 9 as "&ctal" digits... machine Such a program might contain a switch statement to handle each of the various operation codes On such a machine, it is often true that a subtract operation is identical to an add operation after the sign of the second operand has been inverted Thus, it is nice to be able to write something like this: 24 SYNTACTIC PITFALLS CHAPTER 2 case SUBTRACT: opnd2 -opnd2; 1* no break *1 case ADD: Of course,... these syntactic details Chapter 3 deals with misconceptions of meaning: ways a programmer who intended to say one thing can actually be saying something else We assume here that the lexical and syntactic details of the language are well understood and concentrate on semantic details INTRODUCTION CHAPTER 0 3 Chapter 4 recognizes that a C program is often made out of several parts that ate compiled separately... of a compiler diagnostic Section 4.4 (page 57) discusses other cases in detail Because an integer is usually large enough to hold several characters, some C compilers permit multiple characters in a character constant as well as a string constant This means that writing 'yes' instead of "yes" may well go undetected The latter means "the address of the first of four consecutive memory locations containing... because so many other languages use = for comparison It is also easy to interchange & and &&, or : and ::, especially because the & and : operators in Care different from their counterparts in some other languages Section 3.8 (page 48) will discuss the precise meanings of these operators 1.3 Greedy lexical analysis Some C tokens, such as /, *, and =, are only one character long Other C tokens, such... groups and understanding the motivation for the relative precedence of the groups The things that bind the most tightly are the ones that aren't really operators: subscripting, function calls, and structure selection These all associate to the left: a b c means the same as ( a b) c and not a (b .c) Next come the unary operators These have the highest precedence of any of the true operators Because function... }; = 1.5 Strings and characters Single and double quotes mean very different things in C, and confusing them in some contexts will result in surprises rather than error messages A character enclosed in single quotes is just another way of writing the integer that corresponds to the given character in the 10 LEXICAL PITFALLS CHAPTER 1 implementation's collating sequence Thus, in an ASCII implementation,... without complaint The meaning of this strange construct follows from the definition of octal numbers For instance, 0195 means lxg2+9xg1+5xgO, which is equivalent to 141 (decimal) or 0215 (octal) Obviously we recommend against such usage ANSI C prohibits it Watch out for inadvertent octal values in contexts like this: struct int part_number; char *description; parttab[] "left-handed widget" 046, "right-handed... e, s, and a null character, respectively." The meaning of 'yes' is not precisely defined, but many C implementations take it to mean "an integer that is composed somehow of the values of the characters y, e, and s." Any similarity between these two quantities is purely coincidental Exercise 1-1 Some C compilers allow nested comments Write a C program that finds out if it is being run on such a compiler... programs While these combinations are usually well-defined, the definitions are sometimes counter-intuitive or confusing This chapter looks at some syntactic constructions that are less than obvious 2.1 Understanding function declarations I once talked to someone who was writing a C program to run standalone in a microprocessor When this machine was switched on, the hardware would call the subroutine . Wokingham, England • Tokyo • Bonn Library of Congress Cataloging-in-Publication Data Koenig, Andrew. C traps and pitfalls. Incluqes index. 1. C (Computer program QA76.73 .C1 5K67. index. 1. C (Computer program QA76.73 .C1 5K67 1989 ISBN 0-2 0 1-1 792 8-~ - - - - - - - - AT.T language) 005.26 I. Title. 8 8-1 6616 Copyright @ 1989 by AT&T Bell

Ngày đăng: 19/03/2014, 14:05

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan