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 8256441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:51:53+00:00 2026-06-08T01:51:53+00:00

Came across an interesting interview question: test 1: printf("test %s\n", NULL); printf("test %s\n", NULL);

  • 0

Came across an interesting interview question:

test 1:
printf("test %s\n", NULL);
printf("test %s\n", NULL);

prints:
test (null)
test (null)

test 2:
printf("%s\n", NULL);
printf("%s\n", NULL);
prints
Segmentation fault (core dumped)

Though this might run fine on some systems, at least mine is throwing a segmentation fault.
What would be the best explanation of this behavior? Above code is in C.

Following is my gcc info:

deep@deep:~$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
  • 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-08T01:51:54+00:00Added an answer on June 8, 2026 at 1:51 am

    First things first: printf is expecting a valid (i.e. non-NULL)
    pointer for its %s argument so passing it a NULL is officially
    undefined. It may print "(null)" or it may delete all files on your
    hard drive–either is correct behavior as far as ANSI is concerned
    (at least, that’s what Harbison and Steele tells me.)

    That being said, yeah, this is really wierd behavior. It turns out
    that what’s happening is that when you do a simple printf like this:

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

    gcc is (ahem) smart enough to deconstruct this into a call to
    puts. The first printf, this:

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

    is complicated enough that gcc will instead emit a call to real
    printf.

    (Notice that gcc emits warnings about your invalid printf argument
    when you compile. That’s because it long ago developed the ability to
    parse *printf format strings.)

    You can see this yourself by compiling with the -save-temps option
    and then looking through the resulting .s file.

    When I compiled the first example, I got:

    movl    $.LC0, %eax
    movl    $0, %esi
    movq    %rax, %rdi
    movl    $0, %eax
    call    printf      ; <-- Actually calls printf!
    

    (Comments were added by me.)

    But the second one produced this code:

    movl    $0, %edi    ; Stores NULL in the puts argument list
    call    puts        ; Calls puts
    

    Note that this optimization is correct, i.e. it produces the same result for valid strings; notably puts prints a newline character after the string.

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

Sidebar

Related Questions

I came across an interesting algorithm question in an interview. I gave my answer
I came across this post , which reports the following interview question: Given two
I came across an interesting question on java regex Is there a regular expression
I came across an interesting question today where I have two methods that, at
While answering this question I came across an interesting difference in the overload resolution
Recently I came across one interesting question on linked list. Sorted singly linked list
I have a question I came across an interesting piece of code. In MySQL
I came across an interesting question in my textbook, but not further answer or
I just came across an interesting LogCat entry: 06-28 08:43:25.616 I Web Console 5075
I just came across the interesting problem of trying to trim the leading zeroes

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.