Sitemap
Pseudonotes

Pseudonotes

Making docs, ctrl+c/v debugging easier daily through pseudocode.

Press enter or click to view image in full size

Ctrings

1 min read6 days ago

--

The c language doesn’t have a built in type string. But doesn’t it ?

A string is known to store a stream of characters, C provides this very well.

char name[150];

Declares an array of upto 150 characters, that can be called a string. So how’d we go about reading it as input, from an input stream.

An input stream could be stdin, user input or a file. But focus is just via stdin.

Get Pseudonotes’s stories in your inbox

Join Medium for free to get updates from this writer.

Usually to get user input in C we use the scanf function.

scanf(format, destination);

This being the format. But out of the box C using scanf terminates with the occurence of a whitespace. So the above example works till we land a white space and everything afterwards is ignored.

scanf("%s", name); // input : Yellow Brown | output: Yellow

To counter this behavior, C provides an extra library, stdlib.h.

Inside stdlib, we have various functions, but interested in reading a string completeness use the gets function.

int res = gets(name);// input : Yellow Brown | output: Yellow Brown

Kudos.

--

--

Pseudonotes
Pseudonotes

Written by Pseudonotes

Making docs, ctrl+c/v debugging easier daily through pseudocode.