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.
Post History
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 in...
#3: Post edited
Common string handling pitfalls in C programming
_Preface: 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._---When reading C programming forums or code written by beginners, there is a number of frequently recurring bugs related to string handling. These are not only written by complete beginners, but as often by experienced programmers coming from a higher level language and picking up C.The common bugs originates from them assuming that C like most languages has a built-in string class which will handle all string handling and memory allocation for them. Here follows some frequently occurring bugs and their related questions:---- Bug 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?](https://software.codidact.com/posts/282565)**Question: Why doesn't this work? Does C have a string class?**---- Bug 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?**---- Bug 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?**---- Bug 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?**---- Bug 5) `char str[5+1] = "hello";` ... `if(str == "hello")`.Compiles just fine but gives the wrong results.**Question: How do you properly compare strings?**
- <section class="notice">
- 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._
- </section>
- 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?](https://software.codidact.com/posts/282565)
- **Question: Why doesn't this work? Does C have a string class?**
- 1. `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?**
- 1. `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?**
- 1. `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?**
- 1. `char str[5+1] = "hello";` ... `if(str == "hello")`.
- Compiles just fine but gives the wrong results.
- **Question: How do you properly compare strings?**
#2: Post edited
_Preface: This is a self-answered Q&A meant as a C string handling FAQ. It will ask several question at once which isn't ideal, but they are all closely related and I'd rather not fragment the post into several._- ---
- When reading C programming forums or code written by beginners, there is a number of frequently recurring bugs related to string handling. These are not only written by complete beginners, but as often by experienced programmers coming from a higher level language and picking up C.
- The common bugs originates from them assuming that C like most languages has a built-in string class which will handle all string handling and memory allocation for them. Here follows some frequently occurring bugs and their related questions:
- ---
- - Bug 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?](https://software.codidact.com/posts/282565)
- **Question: Why doesn't this work? Does C have a string class?**
- ---
- - Bug 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?**
- ---
- - Bug 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?**
- ---
- - Bug 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?**
- ---
- - Bug 5) `char str[5+1] = "hello";` ... `if(str == "hello")`.
- Compiles just fine but gives the wrong results.
- **Question: How do you properly compare strings?**
- _Preface: 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._
- ---
- When reading C programming forums or code written by beginners, there is a number of frequently recurring bugs related to string handling. These are not only written by complete beginners, but as often by experienced programmers coming from a higher level language and picking up C.
- The common bugs originates from them assuming that C like most languages has a built-in string class which will handle all string handling and memory allocation for them. Here follows some frequently occurring bugs and their related questions:
- ---
- - Bug 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?](https://software.codidact.com/posts/282565)
- **Question: Why doesn't this work? Does C have a string class?**
- ---
- - Bug 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?**
- ---
- - Bug 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?**
- ---
- - Bug 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?**
- ---
- - Bug 5) `char str[5+1] = "hello";` ... `if(str == "hello")`.
- Compiles just fine but gives the wrong results.
- **Question: How do you properly compare strings?**
#1: Initial revision
Common string handling pitfalls in C programming
_Preface: This is a self-answered Q&A meant as a C string handling FAQ. It will ask several question at once which isn't ideal, but they are all closely related and I'd rather not fragment the post into several._ --- When reading C programming forums or code written by beginners, there is a number of frequently recurring bugs related to string handling. These are not only written by complete beginners, but as often by experienced programmers coming from a higher level language and picking up C. The common bugs originates from them assuming that C like most languages has a built-in string class which will handle all string handling and memory allocation for them. Here follows some frequently occurring bugs and their related questions: --- - Bug 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?](https://software.codidact.com/posts/282565) **Question: Why doesn't this work? Does C have a string class?** --- - Bug 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?** --- - Bug 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?** --- - Bug 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?** --- - Bug 5) `char str[5+1] = "hello";` ... `if(str == "hello")`. Compiles just fine but gives the wrong results. **Question: How do you properly compare strings?**