Your line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)
Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).
Note: do not confuse your declaration with the statement in line 3 here
char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */
which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.
Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)
Answer from Jens on Stack OverflowYour line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)
Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).
Note: do not confuse your declaration with the statement in line 3 here
char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */
which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.
Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)
No, in this case you're pointing to a real (non-null) string with a length of 0. You can simply do the following to set it to actual null:
char* str = NULL;
I learned in C that a string ends with a null value, "\0". How do I print out this null value in C?
I tried doing this by scanning the string "paint". However, it doesn't seem to work -
```
#include <stdio.h>
int main() {
char name[100];
scanf("%s", name);
printf("The name is %c", name[5]);
}
```
This is my output -
```
paint
The name is some weird symbol looking like 0
Process finished with exit code 0
```
c - How can I handle string if null character in the middle of string? - Stack Overflow
programming practices - Are C strings always null terminated, or does it depend on the platform? - Software Engineering Stack Exchange
C - why can't we store the null character at the beginning of a string?
Null character '\0' & null terminated strings
You can't have a null character in the middle of a C string, because a null character, by definition, ends the string.
You can use arrays of chars where some of them are null characters, but you have to treat them as arrays, not strings. So you have to keep track of the length yourself.
string is ends with null character(\0), how can "e" be output?
The string literal "App\0le" is stored in memory as an unnamed character array having the following elements
char unnamed_string_literal[7] = { 'A', 'p', 'p', '\0', 'l', 'e', '\0' };
This declaration
char *str = "App\0le";
may be rewritten taking into account the above assumption the following way
char *str = unnamed_string_literal;
So using the pointer arithmetic and knowing a priori the number of elements in the string literal (including its embedded zero character) you can output any elements of the character array that represents the string literal.
For example
#included <stdio.h>
int main( void )
{
char *str = "App\0le";
for (size_t i = 0; i < 7; i++)
{
if (str[i] == '\0')
{
putchar( '\\' ), putchar( '0' );
}
else
{
putchar( str[i] );
}
}
putchar( '\n' );
}
The program output is
App\0le\0
That is the expression str[i] is an expression of accessing i-th element of an array. It is totally unimportant what the type of the array and what it stores.
If you will write
char *str2 = str;
then the pointer str2 will point to the first character of the same string literal pointed to by the pointer str.
If you need to get a string then you need to declare a character array as for example
char str2[6];
and copy to it characters of the string literal pointed to by the pointer str excluding the embedded zero character but including the terminating zero character. You may not change the string literal itself because any attempt to change a string literal results in undefined behavior.
For example (without using standard C string functions)
#include <stdio.h>
int main( void )
{
char *str = "App\0le";
char str2[6];
size_t i = 0;
while (( str2[i] = str[i] ) != '\0') i++;
while (( str2[i] = str[i + 1] ) != '\0') i++;
puts( str2 );
}
The program output is
Apple
The things that are called "C strings" will be null-terminated on any platform. That's how the standard C library functions determine the end of a string.
Within the C language, there's nothing stopping you from having an array of characters that doesn't end in a null. However you will have to use some other method to avoid running off the end of a string.
Determination of the terminating character is up to the compiler for literals and the implementation of the standard library for strings in general. It isn't determined by the operating system.
The convention of NUL termination goes back to pre-standard C, and in 30+ years, I can't say I've run into an environment that does anything else. This behavior was codified in C89 and continues to be part of the C language standard (link is to a draft of C99):
- Section 6.4.5 sets the stage for
NUL-terminated strings by requiring that aNULbe appended to string literals. - Section 7.1.1 brings that to the functions in the standard library by defining a string as "a contiguous sequence of characters terminated by and including the first null character."
There's no reason why someone couldn't write functions that handle strings terminated by some other character, but there's also no reason to buck the established standard in most cases unless your goal is giving programmers fits. :-)
Hello everyone!
In C, strings (character arrays) are terminated by null character '\0' - character with value zero.
In ASCII, the NUL control code has value 0 (0x00). Now, if we were working in different character set (say the machine's character set wouldn't be ASCII but different one), should the strings be terminated by NUL in that character set, or by a character whose value is zero?
For example, if the machine's character set would be UTF-16, the in C, byte would be 16bits and strings would be terminated by \0 character with value 0x00 00, which is also NUL in UTF-16.
But, what if the machine's character set would be modified UTF-8 (or UTF-7, ...). Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. How would be strings terminated in that case? By the byte with value 0 or by the null character.
I guess my question could be rephrased as: Are null terminated strings terminated by the NUL character (which in that character set might be represented by a nonzero value) or by a character whose value is zero (which in that character set might not represent the NUL character).
Thank you all very much and I'm sorry for all mistakes and errors as english is not my first language.
Thanks again.
String literals like "Hello World!" are null-terminated, but char arrays are not automatically null terminated.
The general principle I've always taken is to be extra cautious and assign '\0' to the the end of the string unless that causes a performance problem. In those cases, I'm extra careful about which library functions I use.
Always be careful to allocate enough memory with strings, compare the effects of the following lines of code:
char s1[3] = "abc";
char s2[4] = "abc";
char s3[] = "abc";
All three are considered legal lines of code (http://c-faq.com/ansi/nonstrings.htmlhttp://c-faq.com/ansi/nonstrings.html), but in the first case, there isn't enough memory for the fourth null-terminated character. s1 will not behave like a normal string, but s2 and s3 will. The compiler automatically count for s3, and you get four bytes of allocated memory. If you try to write
s1[3] = '\0';
that's undefined behavior and you're writing to memory that doesn't belong to s1, and would have weird effects, maybe even disrupting malloc's backend information, making it hard to free memory.