0

I still don't know, when my string in c program contain null-terminated and when it doesn't contain.

Sample of my code

...
float t0 = 2.43, t1 = 3.42, t2 = 1, t3 = 10.9;
...
printf("%.2f %.2f %.2f %.2f", t0, t1, t2, t3); 
...

If i use printf like in my code, does c program will automatically add null-terminated at the end of string that printed or no?

6
  • 2
    Maybe some documentation for printf() will help. The printf function sends the resulting output to stdout. No terminator is emitted. Or were you thinking of sprintf() ? Commented Mar 10, 2015 at 8:54
  • Sorry, but i don't see any string here... Commented Mar 10, 2015 at 8:59
  • @SouravGhosh The OP appears to be referring to the output content of printf, and whether it finishes with a null-terminator (which it doesn't). So far, no answer below seem to have picked up on that. My cheap-glass, out-of-round, poor excuse for a crystal ball tells me the question is eventually related to stdout redirection and whether that output adds a terminator to the target device. Commented Mar 10, 2015 at 9:05
  • @WhozCraig Yes sir, now i got the point. Till time, answers are confusing, though. Commented Mar 10, 2015 at 9:11
  • i am confuse, which is true? people give different answer below Commented Mar 10, 2015 at 16:15

4 Answers 4

2

Q: If i use printf like in my code, does c program will automatically add null-terminated at the end of string that printed or no?
A: No. printf() does not typical print the terminating null character '\0'. Instead "%.2f %.2f %.2f %.2f" causes output like "1.12 2.23 3.34 4.45" with the last character printed as '5'.

[Edit]
The format "%.2f %.2f %.2f %.2f" is a string which ends with a null terminator '\0'. The printed output of printf() did not print a '\0'. The null terminator '\0' in the format signals to printf() to stop. The null terminator '\0' itself is not printed.

Note: In C, a C string always has a terminating null character '\0'. If an array of char does not contain one, it is not a string. So the output of printf() in the above example is not a string, but simply a series of characters.

Sign up to request clarification or add additional context in comments.

Comments

1

The 1st argument to

printf("%.2f %.2f %.2f %.2f", ...

is a string literal ("%.2f %.2f %.2f %.2f") and yes, also string literals are 0-terminated.

Comments

0

Effectively yes, had you declared char format[]="%f %f\n"; Using gdb or adding some research code you'd be able to see the nul byte terminating the string.

Comments

0

The answer to If i use printf like in my code, does the string that printed contain null character at the end? is yes it contains but you will not see it in printf.

The null terminator indicates the end of a string when represented as an array of characters. When using the printf function you pass a pointer to the beginning of the string as a parameter and it will print the string until the NULL terminator.

1 Comment

NULL != null; null== NUL == 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.