0

I'm doing my first steps with C (pointers, pointer-pointer etc, I love it), so have mercy if this is a dumb question.

This fragment outputs nothing:

char buf[256];
snprintf(buf, sizeof buf, "output: %s%s%s");

puts("test");

And this fragment outputs "test" (as expected):

char buf[256];
snprintf(buf, sizeof buf, "output: %s%s");

puts("test");

=>test

Question: Which role does snprintf play here? Is there any relationship with the puts-statement or why has the puts no effect/output in the first code?

Aydin K.
  • 3,309
  • 36
  • 44
  • 2
    Your `snprintf` calls are missing parameters - your compiler should be warning you about this (assuming you have compiler warnings enabled ?). You might want to pick [one of these](http://stackoverflow.com/q/562303/253056) and do a little reading. – Paul R Nov 04 '16 at 17:10
  • 1
    Moreover sprintf and snprintf do not print anything on stdout, they print on a buffer (buf in your case) – Marco Nov 04 '16 at 17:11
  • am using eclipse cdt. there are no compiler warnings showing up. am checking right now if I missed a setting somewhere – Aydin K. Nov 04 '16 at 17:12
  • 1
    When you learn something, it's always going to be useful to read the manual. Check the manual of snprintf() to understand what it does. – P.P Nov 04 '16 at 17:13
  • I know what snprintf does (will use it for int+string concatenation in memory), but my question aims more about the different puts-result afterwards – Aydin K. Nov 04 '16 at 17:13
  • Small update: As Paul R found out, there should be compiler warnings, which are not shown in eclipse CDT (don't know why yet). The first code (non-working) causes a segmentatoin fault (also not visible in eclipse CDT but in console when gcc-ing by hand), while the second runs fine. – Aydin K. Nov 04 '16 at 17:24

1 Answers1

1

Because snprintf(buf, sizeof buf, "output: %s%s%s"); requires 3 parameters :

snprintf(buf, sizeof buf, "output: %s%s%s", str1, str2, str3);

and snprintf(buf, sizeof buf, "output: %s%s"); requires 2 parameters:

snprintf(buf, sizeof buf, "output: %s%s", str1, str2);

if you don't pass parameters to snprintf function doesn't mean snprintf wont try to access them. So, the result you see is a segmentation fault result caused by snprintf trying to access "str3" parameter that doesn't exist.

nopasara
  • 538
  • 3
  • 10
  • Thanks, your answer sounds plausible. But in both of my tests (succesful and unsuccesful), there are at least two parameters missing. In one of them there is no segmentation fault happening. This is not a predictable behaviour of the compiler or? Anyway, I will accept your answer because I don't think that there is a reasonable answer to such questions when functions are not used as they should be used. – Aydin K. Nov 04 '16 at 17:35
  • 2
    Unpredictable behavior is exactly what you should expect from such an error. It is called undefined behavior. – chqrlie Nov 04 '16 at 17:44
  • 1
    It's pretty simple: when snprintf tries to access first two parameters, those parameters reside in existing page (snprintf receives some junk, but from the memory that belongs to your process), but when it tries to access 3rd parameter snprintf tryes to access memory page that dont belong to your process or doesnt exist and you catch segmentation fault. Other words when you'll get segmentation fault depends on junk information in your allocated memory pages(because snprintf searches for /0 = EOL). – nopasara Nov 04 '16 at 17:47