Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:56:45+00:00 2026-06-01T14:56:45+00:00

In the following: printf(Example%s\n,NULL); printf(%s\n,NULL); I get the output as: Example(null) Segmentation Fault When

  • 0

In the following:

printf("Example%s\n",NULL);
printf("%s\n",NULL);

I get the output as:

Example(null)
Segmentation Fault

When I tried backtrace in GDB it shows printf() is converted to puts(). But I can’t seem to understand why this happens.

BTW I found this article but still can’t seem to make sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:56:46+00:00Added an answer on June 1, 2026 at 2:56 pm

    The standard says that passing a NULL pointer as argument to a printf with %s specifier is undefined behavior1 (i.e. anything can happen), so both behaviors are licit.

    In the first case, the standard library (in particular, the printf code) is doing you a favor by printing (null).

    In the second case, instead, the optimizer understands that your printf can be replaced by a puts (which is more efficient) without any change to the “observable behavior” of the program, and so it replaces it. But, puts does not happen to contain the NULL-checking code of the printf, and thus you get a segmentation fault.


    1. C99, §7.19.6.1, ¶8:

      the argument shall be a pointer to the initial element of an array of character type.

      ¶9:

      If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

      You fall in this last case, because NULL is not “a pointer to the initial element of an array of character type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following C code as an example: char buffer1[5]; int* ret; printf(Buffer1 is:
I have the following code: printf(num: %d\n, strcasecmp(buf, h\n)); And I get the following
I'm just starting with The Well-Grounded Rubyist, and they gave the following example: print
Take for example the following regex match. preg_match('!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-[0-9]+)?$!', 'publisher/news/1/2010-march:03-23/test_title/1/page-1', $matches); print_r($matches); It produces the
How do you make the following code work? example = %%(test)% % {'test':'name',} print
I wanted to ask whether the following statement where printf() has been provided with
I have the following preprocessor divective: #ifndef NDEBUG #define TRACE printf #else #define TRACE(...)
The following works as expected. awk -F'^' '{printf set %s:%s %s\n,$1,$2, $7}' todel.txt |
Is the following C++ code well-formed? void consumer(char const* p) { std::printf("%s", p); }
Consider following example: #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <hiredis/hiredis.h> int main(int argc,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.