Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Strings

(1-2h) Study Chapter 8 including exercise 8.4 (p. 480). The following section/s is/are optional:

Learning goals

8.1 Introduction

8.2 Fundamentals

sizeof_character_constant.c
#include <stdio.h>
int main() { printf("sizeof('a') = %zu\n", sizeof('a')); }
sizeof_character_constant.transcript.txt
sizeof('a') = 4

But we store character literals mostly as chars[1].

(char) 'b'

string: series of characters. Uses double-quotes "

(const char[6]) "hello"

Same as:

(const char[6]) "hello"
(const char[6]) "hello"
(const char *) "hello"

Modifiable:

(char[6]) "mello"

Reading a string:

We cannot conveniently integrate BUFFER_SIZE into the format string[2]. fgets is better if we just read a string:

But it does not have formatting functions integrated, so we end up scanf anyway.

fgets covered in the section File Processing

8.3 Character-handling library <ctype.h>

8.4 String conversion functions in <stdlib.h>

8.5 Standard IO Library <stdio.h>

(char[16]) "Hi 42"

And back:

(int) 42

8.6 String manipulation functions of <string.h>

(char[16]) "Hello world!"

8.7 Comparison functions of <string.h>

strcmp, strncmp

(int) -1
(int) 0
(int) 1
(int) -1
(int) 1

8.8 Search functions of <string.h>

8.9 Memory functions of <string.h>

8.10 Other functions

8.11 Secure programming

Formatted Input/Output

(1-2h)

Study Chapter 9 including exercise 9.4 (p. 531). The following section/s is/are optional:

Learning goals

9.1 Introduction

9.2 Streams

stream

a sequence of potentially unlimited data elements made available over time, e.g., from a USB drive to computer’s main memory. Stream (computing)

The three standard streams. Source

The three standard streams. Source

9.3 Formatting output with printf

9.4 Printing integers

9.5 Printing floating point numbers

9.6 Printing strings and characters

9.7 Other conversion specifiers

9.8 Field width and precision

Field width

With field width
  1
 12
123
1234
Without
1
12
123
1234

Precision

001
012
123
1234
1.000
1.200
1.230
1.234
1.234

Field width and precisions as arguments

1.000
1.0000
     1.000
    1.0000

9.9 printf format flags

9.10 Printing literals and escape sequences

9.11 Formatted input with scanf

Integer

Floating point:

Characters and strings

Scan set

(char[16]) "aei"
(char[16]) "cc"

Others

Skipping characters

(int) 2

Suppression character *

(int) 2

9.12 Secure Programming

Footnotes
  1. Why it is defined as one int is due to historical and backwards-compatibility reasons. C++ is newer than C and, a character literal is indeed 1 byte (char). In the earlier days of C, functions with specific datatypes did not exist, so arguments were promoted to int. So at some point the standard body decided to make character literals directly int.

    Due to this, we can write things like multi-character constants:

    (int) 1633837924
    dcba
  2. We could use a chain of macros, or build the string with sprintf.

  3. Why must the question mark be escaped\?: https://stackoverflow.com/a/19374879