0% found this document useful (0 votes)
14 views13 pages

KR 1

Uploaded by

gouthamrathod01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

KR 1

Uploaded by

gouthamrathod01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

K&R Chapter 1

A Tutorial Introduction
Dr. Charles R. Severance
www.cc4e.com
code.cc4e.com (sample code)
online.dr-chuck.com
Learning Path: online.dr-chuck.com
• Internet History, Technology, and Security – ihts.dr-chuck.com
• Python – www.py4e.com
• Django (Python, HTML, CSS, SQL, JavaScript) – www.dj4e.com
• Web Applications (PHP, HTML, CSS, SQL, JavaScript) – www.wa4e.com
• PostgreSQL (SQL) – www.pg4e.com
• C Programming – www.cc4e.com ← We are here 
• Computer Architecture
• Java Enterprise Application Development
Outline of the book
• Chapters 1-4 – Mostly syntax
• Just another programming language
• Arrays, strings and the weird fact that strings are character arrays
• Chapter 5 – Pointers and Arrays
• Chapter 6 – Structures
• Chapter 7 – 8 – Detailed C Features
• The detail we skipped

1 2 3 4 5 6 7 8
Chapter 1
• Sections 1.1 – 1.5 – “Just another programming language”
• Section 1.6 – Arrays
• Static allocation – cannot be resized until Chapter 5 
• Section 1.7 – 1.8 – Functions and Parameters
• Call by value is simple – call by reference is in Chapter 5 
• Section 1.9 – Character Arrays
• This is important because there is no string object in C
• Section 1.10 – Variable scoping between functions
Character Arrays
• We must carefully understand the “size” of the character array and
not exceed it – in C nothing is “auto-extended”
#include <stdio.h>
x = "" int main() {
for i in range(1000) : char x[10];
x += '*' int i;
print(x)
for(i=0; i<1000; i++ ) x[i] = '*';
$ python3 cc_01_01.py printf("%s\n",x);
*********…*********** }

$ a.out
Segmentation fault: 11

kr_01_01.c
String / Character Constants
• Languages like PHP, Python, and #include <stdio.h>
int main() {
JavaScript treat single and double char x[3] = "Hi";
char y[3] = { 'H', 'i' };
quotes nearly the same. Both create printf("x %s\n", x);
*string* constants. printf("y %s\n", y);
printf("%s\n", "Hi");
• In C single quotes are a character and }
printf("%c%c\n", 'H', 'i');

double quotes are a character array


$ a.out
(neither are a string) x Hi
• In C, a character is a byte – a short y Hi
Hi
(typically 8-bit) integer Hi

kr_01_02.c
Character Sets
• The C char type is just a number – character representations depend
on the character set.
• Modern characters including 😊 are represented in multi-byte
sequences using Unicode and UTF-8 – but in 1978 we used ASCII and
other character sets.
print('A', ord('A')) #include <stdio.h>
print('😊', ord('😊’)) int main()
{
$ python3 kr_01_03.py printf("%c %d\n", 'A', 'A');
A 65 }
😊 128522
$ a.out
A 65

kr_01_03.c
ASCII
• American Standard
Code for Information
Interchange

https://en.wikipedia.org/wiki/ASCII
http://www.catonmat.net/download/ascii-cheat-sheet.png
Terminating a String #include <stdio.h>
int main() {
char x[6];
x[0] = 'H';
• The size of a “string” stored in a C x[1] = 'e';
x[2] = 'l';
array is not the length of the x[3] = 'l';
x[4] = 'o';
array x[5] = '\0';
• C uses a special character ' \0 ' printf("%s\n",x);

that *marks* the string end by x[2] = 'L';


printf("%s\n",x);
convention
x[3] = '\0';
• Character arrays need to allocate printf("%s\n",x);
an extra byte to store the line- }

end character $ a.out


Hello
HeLlo
HeL
kr_01_04.c
String Length
#include <stdio.h>
• In C string “length” must be int main() {
char x[] = "Hello";
computed in a loop that scans int py_len();
for a zero character printf("%s %d\n",x, py_len(x));
}
• There the strlen() function in int py_len(self)
char self[];
string.h computes string length {
int i;
for(i=0; self[i]; i++);
x = 'Hello' return i;
print(x, len(x)) }

$ python3 kr_01_05.py $ a.out


Hello 5 Hello 5

kr_01_05.c
Reverse a String in C
• A beloved / terrible coding interview
question
• Exercise 1-17 in K&R
• Do *not* cheat, do not look for the
answer (1000’s are out there)
• Your struggle is very valuable for you
• The reversal must be done in-place
• Even length strings, odd length strings,
empty strings, single character strings
– think about them all
Summary
• Overview and approach for the book
• Representing ”strings” in C character arrays
• Actually doing your homework because it is good for you
Acknowledgements / Contributions
These slides are Copyright 2022- Charles R. Severance (online.dr-chuck.com) Continue new Contributors and Translators here
as part of www.cc4e.com and made available under a Creative Commons
Attribution 4.0 License. Please maintain this last slide in all copies of the
document to comply with the attribution requirements of the license. If you
make a change, feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan School of


Information

Insert new Contributors and Translators here including names and dates

You might also like