Character Arrays (Aka “Cstrings”)

Total Page:16

File Type:pdf, Size:1020Kb

Character Arrays (Aka “Cstrings”) Character Arrays (aka “cstrings”) --------------------------------------------- Section #1: Character Arrays (p.455) --------------------------------------------- As you know already, an array is a contiguous block of elements, all of the same data type. Let’s review the syntax for creating an array: <data type> <identifier>[<dimension>] = {<comma-delimited list of initializers...>}; This allows us to create arrays of ints, doubles, longs, shorts, etc. One special and important use for arrays is to create arrays of chars. Think of it – with an array of chars, we would be able to store a sequence of chars, which is exactly what a string is. Let’s try it out! What does this program do? Take a few moments to study the code, perhaps even draw a picture of memory, and what will be written to stdout: // version #1 int main() % char charBuf[12] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ’, ‘t’, ‘h’, ‘e’, ‘r’, ‘e’, ‘!’}; int index; for (index = 0; index < 12; ++index) { cout << charBuf[index]; } return 0; ( :ot it? If you guessed you’ll see “Hello there!” on stdout, you’re correct! All that’s happening is that we have an array charBuf where each array element is of type char, and it has a dimension of 12. The initialization list is setting each array element to a character using a list of character constants. So naturally, when the loop is entered and a zero-based index is used to subscript into the array, you’ll see each character appear, one at a time. In memory it would look something like this: charBuf [0x210] 9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9 | ‘H’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ | ‘ ’ | ‘t’ | ‘h’ | ‘e’ | ‘r’ | ‘e’ | ‘!’ | 9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] In the picture above, the array charBuf landed at memory address 0x210 and occupies a contiguous block of chars, with each char initialized to what’s in the initialization list. You can see how a loop using a zero-based index would walk through that array and display each char, right? 7ow about this version: // version #2 int main() % char charBuf[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ’, ‘t’, ‘h’, ‘e’, ‘r’, ‘e’, ‘!’}; int index; for (index = 0; index < 12; ++index) { cout << charBuf[index]; } return 0; ( If you guessed that you’ll get exactly the same result, then you’d be exactly right! The only difference is that the dimension was left out of the array declaration, but as you know, that’s allowed if you provide a complete initialization list. So everything works the same. --------------------------------------------- Section #2: cstrings (p.455) --------------------------------------------- But you know, it’s a real hassle to write out that initialization list for charBuf, there are so many single quotes and commas, they almost overwhelm what we’re trying to do. There has to be a better way… And there is! Here’s another version: // version #3 int main() % char charBuf[] = “Hello there!”; int index; for (index = 0; index < 12; ++index) { cout << charBuf[index]; } return 0; ( What’s going on here? A string literal is being used to initialize the array? How can that work? It works because a string literal is a character array! Every time you’ve used a string literal to write to stdout, what you’re really writing out to the screen is a character array, because a string literal is a character array, so it’s an appropriate initializer for the charBuf character array. So let’s try putting the dimension back into the array declaration: // version #4 int main() % char charBuf[45] = “Hello there!”; int index; for (index = 0; index < 12; ++index) { cout << charBuf[index]; } return 0; ( And what do we see this time? Well, if you try to compile that code you’ll see a compiler error! But why? The charBuf array is declared with a dimension of 12, and if you count the characters inside the string literal, you’ll see that there are exactly 12 characters inside that string literal. However, there’s one character inside the string literal that you don’t see, it’s at the very end of the string, just after that exclamation point. It has an ASCII integer value of zero, and it’s used to mark the end of the character array. If we were to see what the string literal looks like in memory, it would be something like this: [0x480] 9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9 | ‘H’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ | ‘ ’ | ‘t’ | ‘h’ | ‘e’ | ‘r’ | ‘e’ | ‘!’ |‘\0’ | 9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Do you see that character at the very end? That’s what is called the null character. As I just mentioned, the null character has an ASCII integer value of zero, and if you’re working with a char array, the integer value zero has special meaning in that context, it’s purpose is to mark end of the string. Notice that it’s written as an escape sequence, because if you simply wrote ‘0’ the compiler would interpret that as the character zero which has an ASCII integer value of 48 (see for yourself on the ASCII table on p.1038 in your textbook). Why is there a null character at the end of the array above? Because, every time you write a string literal in your code (which is a sequence of chars enclosed in a pair of double quotes) what you’re really writing is a null-terminated character array. In other words, a string literal is a null-terminated character array! So can you now see why version #4 above won’t compile? The variable charBuf was declared with a dimension of 12, which would allocate twelve chars in a solid block. However, the string literal being used as an initialization list actually contains 13 chars – the 12 you can see, and the null that you cannot see (but it’s there!). So the code fails to compile because the storage requirements of the string literal exceed the declared dimension of charBuf. This also explains why it will compile if you don’t explicitly specifiy the dimension, because without an explicit dimension the compiler will simply allocate enough memory to store the string literal (take another look at version #3). It’s important that you remember that the character zero isn’t the same thing as the null character (which has a value of zero). The character zero is the displayable character that you would see on the screen, but the null character is not a displayable character, it’s just used to indicate the end of a string. C++ expects strings to be marked with the null character at the end, and this kind of string goes by a special name, it’s what we call a cstring. What’s a cstring? A cstring is a null-terminated character array. In other words, a cstring is just a character array that’s terminated with the integer value zero, because a null character has an integer value of zero. Since a character array is used to store human readable text as a string, the null character plays a special role in the representation of that string, it allows us to know where the end of the string is. This is true only of character arrays – an array of any other data type is not assumed to be terminated with a null character, only cstrings are. +kay, back to version #$ of the code above. That version works nicely because we’re now able to initialize charBuf by leaving out the dimension and simply using the string literal as the initialization list. However, look at the boolean expression controlling the loop: // version #3 int main() % char charBuf[] = “Hello there!”; int index; for (index = 0; index < 12; ++index) { cout << charBuf[index]; } return 0; ( Pgh! There’s still that pesky 12 in there. In other words, although the string literal saves us from declaring a dimension, we still have to count the number of characters inside the string literal to know how many times to loop. Or do we? The answer is “no!” Why? Because since the string literal is a cstring, that means that the initialization list contains all of the characters you see inside the double quotes, as well as that null character that you don’t see. So when charBuf is allocated and initialized by the compiler, it will look like this: charBuf [0x210] 9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9 | ‘H’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ | ‘ ’ | ‘t’ | ‘h’ | ‘e’ | ‘r’ | ‘e’ | ‘!’ |‘\0’ | 9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9&&&&&9 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] *hat means that charBuf contains a cstring, and since a cstring is null-terminated, that means that charBuf is also null-terminated. Now, we can exploit that and simplify our loop: // version #5 int main() % char charBuf[] = “Hello there!”; int index; for (index = 0; charBuf[index] != ‘\0’; ++index) { cout << charBuf[index]; } return 0; ( /ith this version, we no longer have to count the characters inside the string literal, instead we just rely on the fact that cstrings are null-terminated. So the loop keeps running as long as the character at charBuf[index] is not a null character. Very cool! Okay, I can tell you’re getting excited about this stuff, but there’s more… How about another way to display the string to stdout: // version #6 int main() % char charBuf[] = “Hello there!”; int index; cout << charBuf return 0; ( Huh? How can that possibly work? Well, recall that an array identifier with no subscript operator evaluates to its base address.
Recommended publications
  • Preview Objective-C Tutorial (PDF Version)
    Objective-C Objective-C About the Tutorial Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. This is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. This reference will take you through simple and practical approach while learning Objective-C Programming language. Audience This reference has been prepared for the beginners to help them understand basic to advanced concepts related to Objective-C Programming languages. Prerequisites Before you start doing practice with various types of examples given in this reference, I'm making an assumption that you are already aware about what is a computer program, and what is a computer programming language? Copyright & Disclaimer © Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book can retain a copy for future reference but commercial use of this data is not allowed. Distribution or republishing any content or a part of the content of this e-book in any manner is also not allowed without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] ii Objective-C Table of Contents About the Tutorial ..................................................................................................................................
    [Show full text]
  • C Strings and Pointers
    Software Design Lecture Notes Prof. Stewart Weiss C Strings and Pointers C Strings and Pointers Motivation The C++ string class makes it easy to create and manipulate string data, and is a good thing to learn when rst starting to program in C++ because it allows you to work with string data without understanding much about why it works or what goes on behind the scenes. You can declare and initialize strings, read data into them, append to them, get their size, and do other kinds of useful things with them. However, it is at least as important to know how to work with another type of string, the C string. The C string has its detractors, some of whom have well-founded criticism of it. But much of the negative image of the maligned C string comes from its abuse by lazy programmers and hackers. Because C strings are found in so much legacy code, you cannot call yourself a C++ programmer unless you understand them. Even more important, C++'s input/output library is harder to use when it comes to input validation, whereas the C input/output library, which works entirely with C strings, is easy to use, robust, and powerful. In addition, the C++ main() function has, in addition to the prototype int main() the more important prototype int main ( int argc, char* argv[] ) and this latter form is in fact, a prototype whose second argument is an array of C strings. If you ever want to write a program that obtains the command line arguments entered by its user, you need to know how to use C strings.
    [Show full text]
  • Strings in C++
    Programming Abstractions C S 1 0 6 X Cynthia Lee Today’s Topics Introducing C++ from the Java Programmer’s Perspective . Absolute value example, continued › C++ strings and streams ADTs: Abstract Data Types . Introduction: What are ADTs? . Queen safety example › Grid data structure › Passing objects by reference • const reference parameters › Loop over “neighbors” in a grid Strings in C++ STRING LITERAL VS STRING CLASS CONCATENATION STRING CLASS METHODS 4 Using cout and strings int main(){ int n = absoluteValue(-5); string s = "|-5|"; s += " = "; • This prints |-5| = 5 cout << s << n << endl; • The + operator return 0; concatenates strings, } and += works in the way int absoluteValue(int n) { you’d expect. if (n<0){ n = -n; } return n; } 5 Using cout and strings int main(){ int n = absoluteValue(-5); But SURPRISE!…this one string s = "|-5|" + " = "; doesn’t work. cout << s << n << endl; return 0; } int absoluteValue(int n) { if (n<0){ n = -n; } return n; } C++ string objects and string literals . In this class, we will interact with two types of strings: › String literals are just hard-coded string values: • "hello!" "1234" "#nailedit" • They have no methods that do things for us • Think of them like integer literals: you can’t do "4.add(5);" //no › String objects are objects with lots of helpful methods and operators: • string s; • string piece = s.substr(0,3); • s.append(t); //or, equivalently: s+= t; String object member functions (3.2) Member function name Description s.append(str) add text to the end of a string s.compare(str) return
    [Show full text]
  • Technical Study Desktop Internationalization
    Technical Study Desktop Internationalization NIC CH A E L T S T U D Y [This page intentionally left blank] X/Open Technical Study Desktop Internationalisation X/Open Company Ltd. December 1995, X/Open Company Limited 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, photocopying, recording or otherwise, without the prior permission of the copyright owners. X/Open Technical Study Desktop Internationalisation X/Open Document Number: E501 Published by X/Open Company Ltd., U.K. Any comments relating to the material contained in this document may be submitted to X/Open at: X/Open Company Limited Apex Plaza Forbury Road Reading Berkshire, RG1 1AX United Kingdom or by Electronic Mail to: [email protected] ii X/Open Technical Study (1995) Contents Chapter 1 Internationalisation.............................................................................. 1 1.1 Introduction ................................................................................................. 1 1.2 Character Sets and Encodings.................................................................. 2 1.3 The C Programming Language................................................................ 5 1.4 Internationalisation Support in POSIX .................................................. 6 1.5 Internationalisation Support in the X/Open CAE............................... 7 1.5.1 XPG4 Facilities.........................................................................................
    [Show full text]
  • Variable Feature Usage Patterns in PHP
    Variable Feature Usage Patterns in PHP Mark Hills East Carolina University, Greenville, NC, USA [email protected] Abstract—PHP allows the names of variables, classes, func- and classes at runtime. Below, we refer to these generally as tions, methods, and properties to be given dynamically, as variable features, and specifically as, e.g., variable functions expressions that, when evaluated, return an identifier as a string. or variable methods. While this provides greater flexibility for programmers, it also makes PHP programs harder to precisely analyze and understand. The main contributions presented in this paper are as follows. In this paper we present a number of patterns designed to First, taking advantage of our prior work, we have developed recognize idiomatic uses of these features that can be statically a number of patterns for detecting idiomatic uses of variable resolved to a precise set of possible names. We then evaluate these features in PHP programs and for resolving these uses to a patterns across a corpus of 20 open-source systems totaling more set of possible names. Written using the Rascal programming than 3.7 million lines of PHP, showing how often these patterns occur in actual PHP code, demonstrating their effectiveness at language [2], [3], these patterns work over the ASTs of the statically determining the names that can be used at runtime, PHP scripts, with more advanced patterns also taking advantage and exploring anti-patterns that indicate when the identifier of control flow graphs and lightweight analysis algorithms for computation is truly dynamic. detecting value flow and reachability. Each of these patterns works generally across all variable features, instead of being I.
    [Show full text]
  • Strings String Literals String Variables Warning!
    Strings String literals • Strings are not a built-in data type. char *name = "csc209h"; printf("This is a string literal\n"); • C provides almost no special means of defining or working with strings. • String literals are stored as character arrays, but • A string is an array of characters you can't change them. terminated with a “null character” ('\0') name[1] = 'c'; /* Error */ • The compiler reserves space for the number of characters in the string plus one to store the null character. 1 2 String Variables Warning! • arrays are used to store strings • Big difference between a string's length • strings are terminated by the null character ('\0') and size! (That's how we know a string's length.) – length is the number of non-null characters • Initializing strings: currently present in the string – char course[8] = "csc209h"; – size if the amount of memory allocated for – char course[8] = {'c','s','c',… – course is an array of characters storing the string – char *s = "csc209h"; • Eg., char s[10] = "abc"; – s is a pointer to a string literal – length of s = 3, size of s = 10 – ensure length+1 ≤ size! 3 4 String functions Copying a string • The library provides a bunch of string char *strncpy(char *dest, functions which you should use (most of the char *src, int size) time). – copy up to size bytes of the string pointed to by src in to dest. Returns a pointer to dest. $ man string – Do not use strcpy (buffer overflow problem!) • int strlen(char *str) – returns the length of the string. Remember that char str1[3]; the storage needed for a string is one plus its char str2[5] = "abcd"; length /*common error*/ strncpy(str1, str2, strlen(str2));/*wrong*/ 5 6 Concatenating strings Comparing strings char *strncat(char *s1, const char *s2, size_t n); int strcmp(const char *s1, const char *s2) – appends the contents of string s2 to the end of s1, and returns s1.
    [Show full text]
  • VSI Openvms C Language Reference Manual
    VSI OpenVMS C Language Reference Manual Document Number: DO-VIBHAA-008 Publication Date: May 2020 This document is the language reference manual for the VSI C language. Revision Update Information: This is a new manual. Operating System and Version: VSI OpenVMS I64 Version 8.4-1H1 VSI OpenVMS Alpha Version 8.4-2L1 Software Version: VSI C Version 7.4-1 for OpenVMS VMS Software, Inc., (VSI) Bolton, Massachusetts, USA C Language Reference Manual Copyright © 2020 VMS Software, Inc. (VSI), Bolton, Massachusetts, USA Legal Notice Confidential computer software. Valid license from VSI required for possession, use or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor's standard commercial license. The information contained herein is subject to change without notice. The only warranties for VSI products and services are set forth in the express warranty statements accompanying such products and services. Nothing herein should be construed as constituting an additional warranty. VSI shall not be liable for technical or editorial errors or omissions contained herein. HPE, HPE Integrity, HPE Alpha, and HPE Proliant are trademarks or registered trademarks of Hewlett Packard Enterprise. Intel, Itanium and IA64 are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries. Java, the coffee cup logo, and all Java based marks are trademarks or registered trademarks of Oracle Corporation in the United States or other countries. Kerberos is a trademark of the Massachusetts Institute of Technology.
    [Show full text]
  • Strings String Literals Continuing a String Literal Operations on String
    3/4/14 String Literals • A string literal is a sequence of characters enclosed within double quotes: "When you come to a fork in the road, take it." Strings • String literals may contain escape sequences. • Character escapes often appear in printf and scanf format strings. Based on slides from K. N. King and Dianna Xu • For example, each \n character in the string "Candy\nIs dandy\nBut liquor\nIs quicker.\n --Ogden Nash\n" causes the cursor to advance to the next line: Bryn Mawr College Candy CS246 Programming Paradigm Is dandy But liquor Is quicker. --Ogden Nash Continuing a String Literal How String Literals Are Stored • The backslash character (\) • The string literal "abc" is stored as an array of printf("When you come to a fork in the road, take it. \ four characters: --Yogi Berra"); Null o In general, the \ character can be used to join two or character more lines of a program into a single line. • When two or more string literals are adjacent, the compiler will join them into a single string. • The string "" is stored as a single null character: printf("When you come to a fork in the road, take it. " "--Yogi Berra"); This rule allows us to split a string literal over two or more lines How String Literals Are Stored Operations on String Literals • Since a string literal is stored as an array, the • We can use a string literal wherever C allows a compiler treats it as a pointer of type char *. char * pointer: • Both printf and scanf expect a value of type char *p; char * as their first argument.
    [Show full text]
  • Data Types in C
    Princeton University Computer Science 217: Introduction to Programming Systems Data Types in C 1 Goals of C Designers wanted C to: But also: Support system programming Support application programming Be low-level Be portable Be easy for people to handle Be easy for computers to handle • Conflicting goals on multiple dimensions! • Result: different design decisions than Java 2 Primitive Data Types • integer data types • floating-point data types • pointer data types • no character data type (use small integer types instead) • no character string data type (use arrays of small ints instead) • no logical or boolean data types (use integers instead) For “under the hood” details, look back at the “number systems” lecture from last week 3 Integer Data Types Integer types of various sizes: signed char, short, int, long • char is 1 byte • Number of bits per byte is unspecified! (but in the 21st century, pretty safe to assume it’s 8) • Sizes of other integer types not fully specified but constrained: • int was intended to be “natural word size” • 2 ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) On ArmLab: • Natural word size: 8 bytes (“64-bit machine”) • char: 1 byte • short: 2 bytes • int: 4 bytes (compatibility with widespread 32-bit code) • long: 8 bytes What decisions did the designers of Java make? 4 Integer Literals • Decimal: 123 • Octal: 0173 = 123 • Hexadecimal: 0x7B = 123 • Use "L" suffix to indicate long literal • No suffix to indicate short literal; instead must use cast Examples • int: 123, 0173, 0x7B • long: 123L, 0173L, 0x7BL • short:
    [Show full text]
  • Wording Improvements for Encodings and Character Sets
    Wording improvements for encodings and character sets Document #: P2297R0 Date: 2021-02-19 Project: Programming Language C++ Audience: SG-16 Reply-to: Corentin Jabot <[email protected]> Abstract Summary of behavior changes Alert & backspace The wording mandated that the executions encoding be able to encode ”alert, backspace, and carriage return”. This requirement is not used in the core wording (Tweaks of [5.13.3.3.1] may be needed), nor in the library wording, and therefore does not seem useful, so it was not added in the new wording. This will not have any impact on existing implementations. Unicode in raw string delimiters Falls out of the wording change. should we? New terminology Basic character set Formerly basic source character set. Represent the set of abstract (non-coded) characters in the graphic subset of the ASCII character set. The term ”source” has been dropped because the source code encoding is not observable nor relevant past phase 1. The basic character set is used: • As a subset of other encodings • To restric accepted characters in grammar elements • To restrict values in library literal character set, literal character encoding, wide literal character set, wide lit- eral character encoding Encodings and associated character sets of narrow and wide character and string literals. implementation-defined, and locale agnostic. 1 execution character set, execution character encoding, wide execution character set, wide execution character encoding Encodings and associated character sets of the encoding used by the library. isomorphic or supersets of their literal counterparts. Separating literal encodings from libraries encoding allows: • To make a distinction that exists in practice and which was not previously admitted by the standard previous.
    [Show full text]
  • The ASCII Character Set Strings and Characters
    Characters and Strings 57:017, Computers in Introduction Engineering—Quick Review Fundamentals of Strings and Characters of C Character Strings Character Handling Library String Conversion Functions Standard Input/Output Library Functions String Manipulation Functions of the String Handling Library Comparison Functions of the String Handling Library Search Functions of the String Handling Library Other Functions of the String Handling Library Fundamentals of Strings Introduction and Characters z Introduce some standard library functions z Characters z Easy string and character processing z Character constant z Programs can process characters, strings, lines of z represented as a character in single quotes text, and blocks of memory z 'z' represents the integer value of z z stored in ASCII format ((yone byte/character) z These techniques used to make: z Strings z Word processors z Series of characters treated as a single unit z Can include letters, digits and special characters (*, /, $) z Page layout software z String literal (string constant) - written in double quotes z Typesetting programs z "Hello" z etc. z Strings are arrays of characters z The type of a string is char * i.e. “pointer to char” z Value of string is the address of first character The ASCII Character Set Strings and Characters z String declarations z Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; z Remember that strings represented as character arrays end with '\0' z color has 5 eltlements z Inputting strings z Use scanf char word[10]; scanf("%s", word); z Copies input into word[] z Do not need & (because word is a pointer) z Remember to leave room in the array for '\0' 1 Chars as Ints Inputting Strings using scanf char word[10]; z Since characters are stored in one-byte ASCII representation, char *wptr; they can be considered as one-byte integers.
    [Show full text]
  • A[0] • A[1] • A[2] • A[39] • A[40] Quiz on Ch.9 Int A[40]; Rewrite the Following Using Pointer Notation Instead of Array Notation
    Quiz on Ch.9 int a[40]; Rewrite the following using pointer notation instead of array notation: • a[0] • a[1] • a[2] • a[39] • a[40] Quiz on Ch.9 int a[40]; Rewrite the following using pointer notation instead of array notation: • a[0]++; • ++a[0] • a[2]--; • a[39] = 42; Chapter 10: Characters and Strings Character codes • Characters such as letters, digits and punctuation are stored as integers according to a certain numerical code • One such code is the ASCII code; the numeric values are from 0 to 127, and it requires 7 bits. • Extended ASCII requires 8 bits: see table here and App.A of our text. • Note well: – While the 7-bit ASCII is a standard, the 8-bit Extended ASCII is not. – There exist several versions of Extended ASCII! The version shown in our App.A is called “IBM code page 437”. QUIZ ASCII Use the ASCII table to decode the following string: 67 79 83 67 32 49 51 49 48 Use App. A of text, or search ASCII table online. Character codes • The C data type char stores Extended ASCII codes as 8-bit integers. • char variables can be initialized as either integers, or quote-delimited characters: Characters Use the ASCII table to figure out a simple relationship between the codes for uppercase and lowercase letters! Characters How to test if a character variable holds a lowercase letter: if (c >= 'a' && c <= 'z'); Your turn: test if c is • An uppercase letter • A decimal digit 12.2 Character Input and Output The following functions have prototypes in <stdio.h> 1) int getchar(void); Reads the next character from the standard input.
    [Show full text]