`C
`
`PROGRAMMING
`LANGUAGE
`
`Second Edition
`
`Brian W. Kernighan • Dennis M. Ritchie
`
`AT&T Bell Laboratories
`Murray Hill, New Jersey
`
`PRENTICE HALL, Englewood Cliffs, New Jersey 07632
`
`EXPERIAN EXHIBIT 1014
`IPR2023-01406
`
`
`
`Library of Congress Cataloging-in-Publication Data
`
`Kernighan, Brian W.
`The C programming language.
`Includes index.
`1. C (Computer program language)
`Dennis M.
`II. Title.
`QA76.73.C15K47 1988
`005.13'3
`ISBN 0-13-110370-9
`ISBN 0-13-110362-8 (pbk.)
`
`I. Ritchie,
`
`88-5934
`
`Copyright O 1988, 1978 by Bell Telephone Laboratories, Incorporated.
`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, mechanical, photocopy•
`ing, recording, or otherwise, without the prior written permission of the publisher.
`Printed in the United States of America. Published simultaneously in Canada.
`
`UNIX is a registered trademark of AT&T.
`
`This book was typeset (pie l tbl I eqn l troff -ms) in Times Roman and Courier by
`the authors, using an Autologic APS-5 phototypesetter and a DEC VAX 8550 running
`the 9th Edition of the UNIX• operating system.
`
`Prentice Hall Software Series
`Brian Kernighan, Advisor
`
`Printed in the United States of America
`
`IO 9 8 7
`
`ISBN
`ISBN
`
`□ -13-110362-8
`□ -l3-11 □ 37 □ -9
`
`{PBK}
`
`Prentice-Hall International (UK) Limited, London
`Prentice-Hall of Australia Pty. Limited, Sydney
`Prentice-Hall Canada Inc. , Toronto
`Prentice-Hall Hispanoamericana, S.A., Mexico
`Prentice-Hall of India Private Limited, New Delhi
`Prentice-Hall of Japan, Inc., Tokyo
`Simon & Schuster Asia Pte. Ltd., Singapore
`Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
`
`
`
`38
`
`TYPFS, OPERATORS AND EXPRESSIONS
`
`CHAPTER 2
`
`#define VTAB, xb'
`#define BELL' x7'
`
`/* ASCII vertical tab*/
`/* ASCII bell character*/
`The complete set of escape sequences is
`\a
`\b
`\f
`\n
`\r
`\t
`\v
`
`alert (bell) character
`backspace
`formfeed
`newline
`carriage return
`horizontal tab
`vertical tab
`
`\\
`\?
`\'
`\"
`\ooo
`\xhh
`
`backslash
`question mark
`single quote
`double -quote
`octal number
`hexadecimal number
`
`The character constant ' \ 0 ' represents the character with value zero the
`null character.
`'\0' is often written instead of O to emphasize the char;cter
`nature of some expression, but the numeric value is just O.
`A constant expression is an expre&sion that involves only constants. Such
`expres~ions may be evaluated during compilation rather than run-time, and
`accordingly may be used in any place that a constant can occur, as in
`#define MAXLINE 1000
`char line[MAXLINE+1];
`
`or
`
`/* in leap years*/
`#define LEAP 1
`int days[31+28+LEAP+31+30+31+30+31+31+30+31+30+31];
`
`A string constant, or string literal, is a sequence of zero or more characters
`surrounded by double quotes, as in
`
`"I am a string"
`
`or
`
`/* the empty string*/
`
`""
`The quotes are not part of the string, but serve only to delimit it. The same
`escape sequences used in character const~nts apply in strings; \" represents the
`double-quote character. String constants can be concatenated at compile time:
`
`"hello," " world"
`
`is equivalent to
`
`"hello, world"
`
`This is useful for splitting long strings across several source lines.
`Technically, a string constant is an array of characters. The internal
`representation of a string has a null character '\0' at the end, so the physical
`storage required is one more than the number of characters written between the
`quotes. This representation means that there is no limit to how long a string
`can be, but programs must scan a string ~ompletely to determine its length.
`The standard library function strlen ( s) returns the length of its character
`
`
`
`SECTION 2.8
`
`INCREMENT AND DECREMENT OPERATORS
`
`47
`
`In a context where no value is wanted, just the incrementing effect, as in
`
`' n')
`if ( C ==
`nl++;
`
`prefix and postfix are the same. But there are situations where one or the other
`is specifically called for. For instance, consider the function squeeze ( s, c ),
`which removes all occurrences of the character c from the string s.
`
`/• squeeze: delete all c from s •/
`void squeeze(char s[], int c)
`{
`
`inti, j;
`
`for ( i = j = O; s [ i] I= '\0'; i++)
`if ( s [ i] I = c)
`s[j++] = s[i];
`s(j] = ''\O';
`
`}
`
`Each time a non-c occurs, it is copied into the current j position, and only then
`is j incremented to be ready for the next character. This is exactly equivalent
`to
`
`if ( s [ i] l = c) {
`s[j] = s[i];
`j++;
`
`}
`
`Another example of a similar construction comes from the getline func(cid:173)
`tion that we wrote in Chapter I, where we can replace
`
`if (c -- '\n')
`s(i] = c;
`++i;
`
`{
`
`}
`
`by the more compact
`
`if ( C == '\n' )
`s[i++] = c;
`
`As a third example, consider the standard function strcat ( s, t), which
`concatenates the string t to the end of the string s. strcat assumes that
`there is enough space in s to hold the combination. As we have written it,
`strcat returns no value; the standard library version returns a pointer to the
`resulting string.
`
`
`
`48
`
`TYPES, OPERATORS AND EXPRESSIONS
`
`CHAPTER 2
`
`/* strcat: concatenate t to end of s; s must be big enough*/
`void strcat(char s[], chart[])
`{
`
`inti, j;
`
`i = j = O;
`whi 1 e
`( s [ i ]
`i++;
`while ( (s[i++] = t[j++]) I= '\O')
`
`/* find end of s */
`
`f =
`
`' \ 0 ' )
`
`}
`
`As each character is copied from t to s, the postfix ++ is applied to both i and
`j to make sure that they are in position for the next pass through the loop.
`
`Exercise 2-4. Write an alternate version of squeeze ( s 1, s2) that deletes
`each character in s 1 that matches any character in the string s2. D
`
`Exercise 2-5. Write the function any{ s 1, s2 ), which returns the first location
`in the string s 1 where any character from the string s2 occurs, or -1 if s 1
`contains no characters from s2. (The standard library function strpbrk does
`the same job but returns a pointer to the location.) D
`
`2.9 Bitwise Operators
`
`C provides six operators for bit manipulation; these may only be applied to
`integral operands, that is, char, short, int, and long, whether signed or
`unsigned.
`
`&.
`
`A
`
`<<
`>>
`
`bitwise AND
`bitwise inclusive OR
`bitwise exclusive OR
`left shift
`right shift
`one's complement (unary)
`
`The bitwise AND operator & is often used to mask off some set of bits; for
`example,
`n = n &. 0177;
`
`sets to zero all but the low-order 7 bits of n.
`The bitwise OR operator I is used to turn bits on:
`X =XI SET ON·
`-
`,
`'
`sets to one in x the bits that are set to one in SET ON.
`The bitwise exclusive OR operator " sets a one-in each bit position where its
`operands have different bits, and zero where they are the same.
`
`