DEV Community

String Type In C

csm on November 24, 2026

If we go and search for "string type in C" on internet or ask AI, we get a line saying "C has no string type". I know, we get results like: Most...
Collapse
 
nyaomaru profile image
nyaomaru Necoz •

I love the King Nyaomaru series! πŸ˜ΉπŸ‘‘

I barely know C, so I found it really interesting that you can build your own string abstraction like this.

Seeing the whole flow from allocating memory on the heap to manually freeing it made the idea much easier to understand.

Thanks for the clear explanation, and congrats on your first Necoz post! πŸ˜ΈπŸ’š

Collapse
 
csm18 profile image
csm Necoz •

Thank you so much 😊

Collapse
 
effessdev profile image
EffessDev Necoz •

string_new("King Nyaomaru"); 🀣🀣🀣

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ Necoz • • Edited

You're welcome, bro! To be honest, I understood the concept too. 😊❀️

Collapse
 
gautamtalksdev profile image
Gautam K •

Nice walkthrough. Keeping len and cap next to the pointer is what makes appends cheap and avoids strlen on every call. Have you thought about a small-string optimisation next, storing short strings inline in the struct?

Collapse
 
csm18 profile image
csm Necoz •

Storing short strings inline is a cool idea!
I read about it in an article about C++ strings but, need to work on it.
Thank you so much for the idea! 😊

Collapse
 
gtref profile image
G-type Necoz •

I like this. Though I do prefer the shorthand const char *str[] = "Hello"; Good job on making this though. I really like it and how simple but functional it is. Idea that you could add next is add the fmt, ... to the function for making and reading strings so you can add variables to strings.

Collapse
 
csm18 profile image
csm Necoz •

Format function like sprintf for the string would be cool!
Thank you so much for the idea! 😊

Collapse
 
syedfahads profile image
Syed Fahad •

s->s = NULL inside string_free is a small habit that pays off later. free the same string twice by accident and it's just free(NULL), nothing explodes πŸ˜„

Collapse
 
csm18 profile image
csm Necoz •

True!

Collapse
 
csm18 profile image
csm Necoz •

@technogamerz and @hemapriya_kanagala
Sis, my first post that got many likes πŸ˜„
I want to say a special thanks to you two, because you were supporting me from the beginning!
πŸ™πŸ™ŒπŸ™Œ

Collapse
 
technogamerz profile image
π“π‘πž π‹πšπ³π² 𝐆𝐒𝐫π₯ Necoz •

Congratulations! Seeing your happiness made my day, I'm so happy for you πŸ₯°β€οΈ

Image descrbigption

Collapse
 
hemapriya_kanagala profile image
Hemapriya Kanagala Necoz •

so happy for you ra πŸ˜€ Here's to many more likes and lots more love coming your way! You deserve it πŸ₯³

Collapse
 
hemapriya_kanagala profile image
Hemapriya Kanagala Necoz •

You make even something like strings in C so interesting to read ra πŸ˜„ Looking forward to the next posts πŸ˜ƒ

Collapse
 
csm18 profile image
csm Necoz •

Thanks akka 😊
Memory management in C, takes a lot of time, one small thing breaks everything.
Its fun and frustrating πŸ˜„