






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 10
This page cannot be seen from the preview
Don't miss anything!
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
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
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
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
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