C_week8 - Lecture 8 Page 1 BIL 104E INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING Compiled by Ergin TARI Lecture 8 Page 2 INFORMATION TO

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 8, Page 1 Compiled by Ergin TARI BIL 104E INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 8, Page 2 Compiled by Ergin TARI THE NOTES IN THE FOLLOWING SLIDES ARE COMPILED FROM FREE VERSIONS OF THE BOOKS Sams Teach Yourself C in 21 Days (Sams Teach Yourself) By Peter Aitken and Sams Teach Yourself C in 24 Hours ( Published by Sams ) By Tony Zhang. The electronic versions of these and other books can be found in the web pages including http://server11.hypermart.net/davidbook901/data/c/c1f8c0d9.htm http://www.informit.com/itlibrary http://www.free-book.co.uk/computers-internet/programming/c/ http://aelinik.free.fr/c/ INFORMATION TO USERS
Background image of page 2
Lecture 8, Page 3 Compiled by Ergin TARI BIL104E: Introduction to Scientific and Engineering Computing, Spring 2005. Lecture 8 Outline Declaring Strings Initializing Strings String Constants Versus Character Constants How Long Is a String? Copying Strings with strcpy() Reading and Writing Strings
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 8, Page 4 Compiled by Ergin TARI Declaring Strings What Is a String? A string is a character array terminated by a null character (\0). For instance, a character array, array_ch, declared in the following statement, is considered a character string: char array_ch[7] = {`H', `e', `l', `l', `o', `!', `\0'}; In C, the null character can be used to mark the end of a string, or to return logical FALSE. C treats \0 as one character. Each character in a string takes only 1 byte. A series of characters enclosed in double quotes ("") is called a string constant . The C compiler can automatically add a null character (\0) at the end of a string constant to indicate the end of the string. For example, the character string "A character string." is considered a string constant; so is Hello!"
Background image of page 4
Lecture 8, Page 5 Compiled by Ergin TARI Initializing Strings A character array can be declared and initialized like this: char arr_str[6] = {`H', `e', `l', `l', `o', `!'}; Here the array arr_str is treated as a character array. However, if you add a null character (\0) into the array, you can have the following statement: char arr_str[7] = {`H', `e', `l', `l', `o', `!', `\0'}; Here the array arr_str is expanded to hold seven elements; the last element contains a null character. Now, the character array arr_str is considered a character string because of the null character that is appended to the array. You can also initialize a character array with a string constant. For example, the following statement initializes a character array, str, with a string constant, "Hello!": char str[7] = "Hello!";
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 8, Page 6 Compiled by Ergin TARI Initializing Strings (cont.) The compiler can automatically append a null character (\0) to the end of the array, and treat the character array as a character string. Note that the size of the array is specified to hold up to seven elements, although the string constant has only six characters enclosed in double quotes. The extra space is reserved for the null
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/22/2010 for the course CSE 104 taught by Professor E.t. during the Spring '09 term at Istanbul Technical University.

Page1 / 42

C_week8 - Lecture 8 Page 1 BIL 104E INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING Compiled by Ergin TARI Lecture 8 Page 2 INFORMATION TO

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online