What is a Null-Terminated String?
💡 Concept Name
Null-Terminated String – A sequence of characters in memory that ends with a special null character ('\0'
), used by C and C++ to mark where a string finishes.
📘 Quick Intro
In C, text is stored as an array of characters, but the language needs a way to know when the text stops. That’s why every C string ends with the '\0'
(null) character. This null-terminator signals the end of the string so functions don’t read past the real data.
🧠 Analogy / Short Story
Imagine a row of parked cars in a garage, but there’s no line to mark the end. Now, add a big “STOP” sign after the last car—the null character acts as this stop sign, telling everyone: “this is where the row ends.”
🔧 Technical Explanation
- 💡 The last element of a C string array is always
'\0'
(ASCII code 0). - 🔍 String functions like
strlen
,printf("%s")
, andstrcpy
scan characters until they hit'\0'
. - ⚠️ If you forget the null terminator, programs may read garbage data or even crash due to buffer overruns.
- 🖥️ This approach is specific to C/C++. Other languages like .NET or Java store the string’s length instead.
- 🌐 .NET strings are not null-terminated—they use a length prefix and Unicode encoding for safety and flexibility.
🎯 Purpose & Use Case
- ✅ Used in C programming for text processing and system-level code.
- ✅ Fundamental for many operating system APIs and embedded firmware.
- ✅ Still relevant when interoping between C/C++ and higher-level languages.
💻 Real Code Example
// Null-terminated string in C
#include <stdio.h>
int main() {
char language[] = "C Programming"; // Automatically null-terminated
printf("%s\n", language); // Output: C Programming
return 0;
}

❓ Interview Q&A
Q1: How do you know a string ends in C?
A: It ends with a null character ('\0'
).
Q2: What’s the risk if a string isn’t null-terminated?
A: String functions may keep reading memory until a random zero is found, leading to bugs or security issues.
Q3: Do .NET strings use null-termination?
A: No, .NET strings store length internally and support Unicode.
Q4: Why does C use null-terminated strings instead of storing the length?
A: To save memory and keep things simple for low-level operations (at the cost of safety and efficiency).
Q5: How do you print a null-terminated string in C?
A: Use printf("%s", stringVariable)
and C will print up to the null character.
Q6: Can null-terminated strings cause security vulnerabilities?
A: Yes, missing null terminators can lead to buffer overflows.
Q7: How does .NET avoid buffer overflows with strings?
A: By storing string length separately, it avoids reading beyond the string.
Q8: What is a downside of null-terminated strings?
A: Functions must scan for the null terminator, which can be inefficient.
Q9: Are C strings Unicode by default?
A: No, C strings are arrays of bytes, typically ASCII or platform dependent.
Q10: How do you safely handle strings in C to avoid errors?
A: Always ensure the string is properly null-terminated and buffer sizes are checked.
📝 MCQs
Q1. Which character ends a C string?
- \0 (null)
- \n (newline)
- EOF
- Space
Q2. Which language depends on null-terminated strings?
- Python
- C
- C#
- Java
Q3. How does .NET store string length?
- Null-terminated
- Fixed size
- Length-prefixed
- Double nulls
Q4. What can happen if null-terminator is missing?
- Faster code
- Buffer overflow or garbage read
- Shorter string
- No effect
Q5. Is null-termination safe for Unicode?
- Yes
- No, can cause errors
- Only in Linux
- Only with ASCII
Q6. What is the risk of not null-terminating a C string?
- Performance gain
- Security vulnerabilities
- No risk
- Compilation error
Q7. How do .NET strings represent characters?
- ASCII
- UTF-8
- UTF-16 encoding
- ISO-8859-1
Q8. Why does C use null-terminated strings?
- Speed
- Memory saving and simplicity
- Unicode support
- Thread safety
Q9. How do you print a C string safely?
- Use puts() only
- Ensure null termination before using printf
- Use cout instead
- Use strcpy()
Q10. What is a downside of null-terminated strings?
- Memory overhead
- Potential buffer overflows
- Fixed string length
- Unicode limitation
💡 Bonus Insight
Many bugs in C come from mishandled null-terminated strings—always double-check that you have a '\0'
at the end before passing strings to low-level APIs!
📄 PDF Download
Need a handy summary for your notes? Download this topic as a PDF!