Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

string oops c language

Rating
-
Sold
-
Pages
20
Uploaded on
17-06-2022
Written in
2021/2022

Lecture notes of 20 pages for the course Problem Solving And Object Oriented Programming at Vellore Institute Of Technology (strings)

Institution
Course

Content preview

Strings
In C programming, a string is a sequence of characters terminated with a
null character \0 . For example:

char c[] = "c string";




When the compiler encounters a sequence of characters enclosed in the
double quotation marks, it appends a null character \0 at the end by
default.




Memory Diagram


How to declare a string?

Here's how you can declare strings:

char s[5];




String Declaration in C


Here, we have declared a string of 5 characters.

,How to initialize strings?

You can initialize strings in a number of ways.

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};




String Initialization in C


Let's take another example:

char c[5] = "abcde";


Here, we are trying to assign 6 characters (the last character is '\0' ) to
a char array having 5 characters. This is bad and you should never do this.



Assigning Values to Strings

Arrays and strings do not support the assignment operator once it is
declared. For example,

char c[100];
c = "C programming"; // Error! array type is not assignable.



Note: Use the strcpy() function to copy the string instead.

, Read String from the user

You can use the scanf() function to read a string.
The scanf() function reads the sequence of characters until it
encounters whitespace (space, newline, tab, etc.).




Example 1: scanf() to read a string

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}


Output

Enter name: Dennis Ritchie
Your name is Dennis.




Even though Dennis Ritchie was entered in the above program,
only "Dennis" was stored in the name string. It's because there was a space
after Dennis .

Also notice that we have used the code name instead of &name with scanf() .

scanf("%s", name);


This is because name is a char array, and we know that array names decay
to pointers in C.
Thus, the name in scanf() already points to the address of the first element in
the string, which is why we don't need to use & .

Written for

Institution
Course

Document information

Uploaded on
June 17, 2022
Number of pages
20
Written in
2021/2022
Type
Class notes
Professor(s)
Jaya
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
siddharthshehria

Get to know the seller

Seller avatar
siddharthshehria vellore institute of technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
3
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions