Chapter 09

  • Uploaded by: mohammad
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Chapter 09 as PDF for free.

More details

  • Words: 237
  • Pages: 7
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

CP 202 Chapter 9

Declaring String  

A string in C is implemented as an array of char. Declaring a string variable is the same as declaring an array of char: char string_var[30];

Declaring and Initializing String 

Initializing string variables: char str[10] = “Hello to”; It will look in the memory like: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

H





e

l

l

o

t

o \0

The str[8] contains the character ‘\0’, the null character, that marks the end of a string. This means that str can contain only 9 characters. If you want to work with an array of char as string, you need to add the null character at the end.

Arrays of String 

An array of strings is a two-dimensional array of characters in which each row is one string. char month[12][10] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};

Strings and I/O Functions 

You need to use the placeholder %s with the string variables.

char str[20] = “Hello to”;

char month[12][10] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; printf(“%s\n”, str); hello to

printf(“ %c%c \n”, str[4], month[1][3]); or printf(“%s\n”, month[10]);

// // // November

Strings and I/O Functions EXAMPLE

Strings and I/O Functions EXAMPLE (OUTPUT)

Related Documents

Chapter 09
November 2019 10
Chapter 09
November 2019 16
Chapter 09
November 2019 9
Chapter 09
May 2020 4
Chapter 09
June 2020 1
Chapter 09
November 2019 7

More Documents from ""

Chapter3-appendix2
November 2019 39
Arak
June 2020 24
Cpcs202_lab 1
November 2019 46
Chapter5-appendix1
November 2019 39
June 2020 22