2

I dont understand why I am getting an output like this: StackOver↨< as snprintf should take care of null termination as expected output is StackOver. I am using devcpp IDE.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(void)
{
    char buffer[10];
    printf("%d\n", sizeof(buffer));
    snprintf(buffer, sizeof(buffer), "%s", "StackOverflow");
    printf("%s", buffer);
    return 0;
}
12
  • 1
    Have you thought about checking the return value from snprintf Commented Jan 8, 2017 at 16:30
  • 1
    @EdHeal: you're not helping... Commented Jan 8, 2017 at 16:35
  • "StackOverflow" is 13 chars length, but your buffer only 10 chars . This is the reason why you have output like this. Commented Jan 8, 2017 at 16:35
  • 1
    Small remark, for completeness, you might want to change printf("%d\n", sizeof(buffer)); into printf("%zu\n", sizeof(buffer)); since the return type for sizeof is size_t. Commented Jan 8, 2017 at 16:46
  • 1
    FWIW, on Linux/Debian/Sid/x86-64 with GCC 6.2, libc 2.24 your code behave as it should, and shows StackOver without a new line. I strongly suspect your C standard library is buggy. Commented Jan 8, 2017 at 17:09

2 Answers 2

3

The C Standard states that the copied string shall be nul-terminated:

7.21.6.5 The snprintf function

...

Description

The snprintf function is equivalent to fprintf , except that the output is written into an array (specified by argument s ) rather than to a stream. If n is zero, nothing is written, and s may be a null pointer. Otherwise, output characters beyond the n-1st are discarded rather than being written to the array, and a null character is written at the end of the characters actually written into the array. If copying takes place between objects that overlap, the behavior is undefined.

It appears you are running with an outdated and/or buggy C runtime library as the snprintf() implementation doesn't seem to properly implement the required behavior.

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

4 Comments

MSVC's man page comments about the function suggest it previously worked like strncpy but with C99 no longer does.
@WeatherVane Nice. C99 is nearly two decades old.
Yep, that's where MSVC is. OP's compiler may be that old too.
Thanks for this. I've wasted a lot of time checking snprintf the same way as you need to check strncpy. Good to hear it has moved on!
-1

This code is working fine for me. The buffer only has space for 10 characters so sprintf is only able to write the first 9 characters that you tell it to write ("StackOver"). At the tenth character it stores a terminating null character, since every C string must be null-terminated.

The only suggestion I would make is adding a newline when printing the string at the end:

printf("%s\n", buffer);

The lack of a newline at the end might be the reason why your IDE is showing you that character.

If you want the buffer to fit "StackOverflow" you need to allocate it to something larger.

12 Comments

It didn't help. I dont know how come I am getting the similar output all the time?
You are supposed to get that output every time. There is no randomness involved. Maybe I didn't understand what you were actually trying to do?
@hugomg I tested it, and adding the newline removes a strange symbol for me in OSX terminal.
Is there any problem with my IDE?
@CareyGregory: snprintf is supposed to always include a null terminator: stackoverflow.com/questions/7706936
|

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.