Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Common string handling pitfalls in C programming

Post

Common string handling pitfalls in C programming

+7
−0

This is a self-answered Q&A meant as a C string handling FAQ. It will ask several questions at once which isn't ideal, but they are all closely related and I'd rather not fragment the post into several._

Code written by beginners to C, or found on C programming forums, frequently contains a few specific string handling bugs. Even experienced programmers coming from a higher level language and picking up C may make these mistakes.

These bugs seem to result from expecting C to have a built-in string class (like most languages do) which would handle all string operations and memory allocation for them.

Here are some frequently occurring bugs with corresponding questions:

  1. char str = "hello";.

    This will luckily not even compile if the compiler is configured correctly, see What compiler options are recommended for beginners learning C?

    Question: Why doesn't this work? Does C have a string class?

  2. char str[5] = "hello";.

    Compiles just fine, yet when printing this there will be garbage printed or other strange behavior. This bug is related to character arrays and missing null termination.

    Question: What exactly does a string consist of in C?

  3. char* str; scanf("%s", str);

    Compiles just fine, though if lucky there can be warnings. This bug is related to memory allocation.

    Question: Who is responsible for allocating memory for the string?

  4. char* str = malloc(5+1); str = "hello";

    Compiles just fine, though there are memory leaks.

    Question: How can a string get assigned a new value?

  5. char str[5+1] = "hello"; ... if(str == "hello").

    Compiles just fine but gives the wrong results.

    Question: How do you properly compare strings?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Bug 1 isn't a possible bug (2 comments)
Bug 1 isn't a possible bug
EJP‭ wrote almost 3 years ago

char str = "Hello"; should not compile in any C compiler.

Lundin‭ wrote almost 3 years ago

EJP‭ No, but unfortunately some mainstream compilers like the gcc-like ones are not set to strictly conforming C by default. Hence the provided link below bug 1, which instructs how to configure the compiler correctly to prevent the compiler from generating an executable when given non-conforming C code.