Output of below code

radha gogia

radha gogia

@radha-BTDzli Oct 22, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Oct 10, 2014

    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

    @manojkiran-Cse2se Oct 10, 2014

    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

    @radha-BTDzli Oct 11, 2014

    [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

    @radha-BTDzli Oct 12, 2014

    [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]

    @prototype-G9Gn5k Oct 12, 2014

    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

    @radha-BTDzli Oct 12, 2014

    [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]

    @prototype-G9Gn5k Oct 12, 2014

    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.