Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming for Engineers: Lecture 16 - C Strings, Study notes of Engineering

The lecture notes for cse294a introduction to c programming for engineers, lecture #16. The topic of this lecture is c strings. The differences between c and c++ strings, how to declare and populate c strings as arrays and pointers, and the use of c string functions such as strcmp, strcat, strcpy, and strlen.

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-q8h
koofers-user-q8h 🇺🇸

4

(2)

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE294A
Introduction to C Programming for Engineers
Lecture #16
Jeffrey Miller, Ph.D.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C Programming for Engineers: Lecture 16 - C Strings and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

C Strings

C strings are different than C++ strings, which youwill learn next semester

C strings are nothing more than an array ofcharacters, though there are special functions thatcan be used on them that can not be used on othertypes of arrays - Since C strings are arrays, we can also usepointers with them

Populating C Strings

To give values to C strings, you can set individual characters char my_str[10]; my_str[0] = ‘h’; my_str[1] = ‘i’; my_str[2] = ‘\0’; - You can also read the data from the user char my_str[10]; scanf(“%s”, my_str); // no &…why? - NOTE: Since C strings are terminated by the null character \0, youactually have one character less than the value in the brackets allocatedto your string

C String Functions

int strcmp (const char, const char);

Returns negative value if first string is less thansecond one

Returns 0 if the strings are the same - Returns positive value if first string is greaterthan second one - NOTE: For the C String functions, it isnecessary to include the cstring library #include

C String Functions

char strcpy(char, const char*);

Copies the contents of the second string into thefirst string

Returns a pointer to the new string - When strcmp on the two strings is called, 0 isreturned

C String Functions

size_t strlen (const char*);

Returns the length of the string, minus the \

size_t is a non-negative int, so it can be treatedas an int

Homework

Homework #7 will be posted soon!