Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null termination of char array

Tags:

c

null

char

Consider following case:

#include<stdio.h> int main() {     char A[5];     scanf("%s",A);     printf("%s",A); } 

My question is if char A[5] contains only two characters. Say "ab", then A[0]='a', A[1]='b' and A[2]='\0'. But if the input is say, "abcde" then where is '\0' in that case. Will A[5] contain '\0'? If yes, why? sizeof(A) will always return 5 as answer. Then when the array is full, is there an extra byte reserved for '\0' which sizeof() doesn't count?

like image 248
g4ur4v Avatar asked Aug 18 '12 15:08

g4ur4v


People also ask

What is a null terminated character array?

In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article).

Does char array have a null terminated?

A C-style string is a null (denoted by \0 ) terminated char array. The null occurs after the last character of the string. For an initialization using double quotes, "...", the compiler will insert the null .

How do you null terminate a char array in Java?

Null-terminated strings are a C thing, and only that. Other languages are free to work with strings as they see fit (e.g. preceded by a length field, $ terminated, etc.) An array in Java is just that - a collection of same-typed objects. There is no special treatment given for char arrays.

What is null termination character?

The null character indicates the end of the string. Such strings are called null-terminated strings. The null terminator of a multibyte string consists of one byte whose value is 0. The null terminator of a wide-character string consists of one gl_wchar_t character whose value is 0.


2 Answers

If you type more than four characters then the extra characters and the null terminator will be written outside the end of the array, overwriting memory not belonging to the array. This is a buffer overflow.

C does not prevent you from clobbering memory you don't own. This results in undefined behavior. Your program could do anything—it could crash, it could silently trash other variables and cause confusing behavior, it could be harmless, or anything else. Notice that there's no guarantee that your program will either work reliably or crash reliably. You can't even depend on it crashing immediately.

This is a great example of why scanf("%s") is dangerous and should never be used. It doesn't know about the size of your array which means there is no way to use it safely. Instead, avoid scanf and use something safer, like fgets():

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

Example:

if (fgets(A, sizeof A, stdin) == NULL) {     /* error reading input */ } 

Annoyingly, fgets() will leave a trailing newline character ('\n') at the end of the array. So you may also want code to remove it.

size_t length = strlen(A); if (A[length - 1] == '\n') {     A[length - 1] = '\0'; } 

Ugh. A simple (but broken) scanf("%s") has turned into a 7 line monstrosity. And that's the second lesson of the day: C is not good at I/O and string handling. It can be done, and it can be done safely, but C will kick and scream the whole time.

like image 137
John Kugelman Avatar answered Sep 30 '22 09:09

John Kugelman


As already pointed out - you have to define/allocate an array of length N + 1 in order to store N chars correctly. It is possible to limit the amount of characters read by scanf. In your example it would be:

scanf("%4s", A); 

in order to read max. 4 chars from stdin.

like image 20
harpun Avatar answered Sep 30 '22 08:09

harpun