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;
}
user7375520
  • 273
  • 2
  • 15
  • 1
    Have you thought about checking the return value from `snprintf` – Ed Heal Jan 08 '17 at 16:30
  • 1
    @EdHeal: you're not helping... – Karoly Horvath Jan 08 '17 at 16:35
  • "StackOverflow" is 13 chars length, but your buffer only 10 chars . This is the reason why you have output like this. – Ignat Jan 08 '17 at 16:35
  • @Ignat that is why the function `snprintf` is being used, which prevents buffer overflow. – Weather Vane Jan 08 '17 at 16:36
  • @KarolyHorvath - Why is that not helping? Checking the return value would help – Ed Heal Jan 08 '17 at 16:40
  • It will return a negative value. – user7375520 Jan 08 '17 at 16:40
  • @user7375520 No it won't. It returns the number of characters successfully printed, which is less than `strlen("StackOverflow")`, which tells you it did not succeed. – Carey Gregory Jan 08 '17 at 16:43
  • In some versions of MSVC this function does not include a nul terminator if the formatted string exactly fits or overflows the size limit. It looks as though this is the case with your compiler too. It also returns a negative function value in overflow situations. – Weather Vane Jan 08 '17 at 16:44
  • 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`. – fedepad Jan 08 '17 at 16:46
  • @WeatherVane Thanks a lot! – user7375520 Jan 08 '17 at 16:58
  • *devcpp* is probably an IDE, not a compiler (or a C standard library). You need to look into the documentation of your C standard library & compiler. You might report a bug, if you are sure that you have found one. – Basile Starynkevitch Jan 08 '17 at 17:02
  • 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. – Basile Starynkevitch Jan 08 '17 at 17:09

2 Answers2

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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
-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.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • It didn't help. I dont know how come I am getting the similar output all the time? – user7375520 Jan 08 '17 at 16:39
  • 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 Jan 08 '17 at 16:40
  • @hugomg I tested it, and adding the newline removes a strange symbol for me in OSX terminal. – fedepad Jan 08 '17 at 16:41
  • Is there any problem with my IDE? – user7375520 Jan 08 '17 at 16:43
  • @CareyGregory It tried with ur suggestion by adding a new line. Still getiing the same output. But if I run the program on some online C complier I am getting the expected output. I dont want to allocate a larger buffer just want to see how snprintf is not giving the expected result in this case. Thanks! – user7375520 Jan 08 '17 at 16:49
  • Expected result is "StackOver". It should print only 9 characters. – user7375520 Jan 08 '17 at 16:51
  • It is printing only 9 characters when I tested it. Are you taking about the "↨" bit? – hugomg Jan 08 '17 at 16:52
  • That is my question: Why I'm getting output as "StackOver↨<"? – user7375520 Jan 08 '17 at 16:53
  • @user7375520 Your expectations are incorrect. As @WeatherVane commented on your question, not all implementations of `snprintf` will insert a null terminator in this case, which means you end up printing garbage. – Carey Gregory Jan 08 '17 at 16:53
  • 2
    @CareyGregory: snprintf is supposed to always include a null terminator: http://stackoverflow.com/questions/7706936/ – hugomg Jan 08 '17 at 16:59
  • Maybe so, but it appears not to be happening here. Could be the IDE, I don't know, but clearly something is being printed that shouldn't be. – Carey Gregory Jan 08 '17 at 17:06
  • The problem is not with the IDE or the compiler. It is with the C standard library implementation of the original poster. OP should make a bug report (or switch to a better implementation, e.g. by using some recent Linux distribution) – Basile Starynkevitch Jan 08 '17 at 17:11