Output of below code

What's the output?
main()
{
printf("%d",scanf("%d%d",printf,scanf));
}

I just wanted to ask that why how this code proceeds,I mean how actually we can supply printf and scanf in the sanf functions ,when we have the the format specifiers to be %d which is for int ,so how can we actually pass such pre-defined functions here

Replies

  • [Prototype]
    [Prototype]
    What is the output of this code?
    This should give Access Violation (application crash).

    The scanf expects a pointer to a memory location where you want to store the user input. I am not very sure but I guess there's no compilation error because printf is already declared and being a function, when used in scan passes the address of the function(which is what scanf expects). However, when the scanf will try to put a value into the printf's memory space, it'll not be allowed to do it as its a protected space which is already allocated and currently in use. Hence program will crash unless handled for error.

    Let me know if that makes sense.
  • ManojKiran Eda
    ManojKiran Eda
    Thats a nice doubt #-Link-Snipped-#

    C is having a property that the function name without () , the name evaluates to the address of the function.

    Which is pretty similar to an array.....As array name without[] acts as the base pointer which holds the address of the first location...and function without () acts as a pointer which holds the address of function.
  • radha gogia
    radha gogia
    [Prototype]
    What is the output of this code?
    This should give Access Violation (application crash).

    The scanf expects a pointer to a memory location where you want to store the user input. I am not very sure but I guess there's no compilation error because printf is already declared and being a function, when used in scan passes the address of the function(which is what scanf expects). However, when the scanf will try to put a value into the printf's memory space, it'll not be allowed to do it as its a protected space which is already allocated and currently in use. Hence program will crash unless handled for error.

    Let me know if that makes sense.
    Thankuu i understood,good explanation
  • radha gogia
    radha gogia
    [Prototype]
    What is the output of this code?
    This should give Access Violation (application crash).

    The scanf expects a pointer to a memory location where you want to store the user input. I am not very sure but I guess there's no compilation error because printf is already declared and being a function, when used in scan passes the address of the function(which is what scanf expects). However, when the scanf will try to put a value into the printf's memory space, it'll not be allowed to do it as its a protected space which is already allocated and currently in use. Hence program will crash unless handled for error.

    Let me know if that makes sense.
    I just wanted to know that actually why the code is not actually waiting for the user to input also some data,it directly prints null,as you explained in ur explanation that sanf expects the pointer to memory address where it is going to store the user input,so till i have not yet entered any input,then how come it directly interprets the output,becoz yet no input is enterd and hence no violation of the memory space of the printf function..
  • [Prototype]
    [Prototype]
    radha gogia
    I just wanted to know that actually why the code is not actually waiting for the user to input also some data,it directly prints null,as you explained in ur explanation that sanf expects the pointer to memory address where it is going to store the user input,so till i have not yet entered any input,then how come it directly interprets the output,becoz yet no input is enterd and hence no violation of the memory space of the printf function..
    Which compiler are you using? I've compiled using GCC and it crashes as soon as I write some input and press enter.
  • radha gogia
    radha gogia
    [Prototype]
    Which compiler are you using? I've compiled using GCC and it crashes as soon as I write some input and press enter.
    I had used online compiler and it just gave the output -1
  • [Prototype]
    [Prototype]
    radha gogia
    I had used online compiler and it just gave the output -1
    I don't know how online compilers are taking the input but it's better if you download gcc and check it there. Online compilers are not meant for 'trick' questions.

You are reading an archived discussion.

Related Posts

Xiaomi is not just a phone maker. It's evolving into an electronics company and already beat Samsung in the mobile game on home ground - China. Xiaomi is now bringing...
Quote: 3-D printing is much more than a new way of making; it's an opportunity to rethink everything we know about design and product engineering. "Many people look at end...
Hello, I'm working on the design of a retaining wall, on which a steel hall will be mounted. The retaining wall is not beneath the whole perimeter of the building,...
I just wanted to know the difference between %u and %#u .....
Food & Bev Tech unveils ; plea to create Brand India & up food exports https://www.fnbnews.com/article/detnew.asp?articleid=35972§ionid=1 Govt plans smaller clusters for food processing industry https://www.business-standard.com/co...-food-processing-industry-114081100312_1.html Indian Food Industry https://www.ibef.org/industry/indian-food-industry.aspx