CrazyEngineers
  • Output of below code

    radha gogia

    radha gogia

    @radha-BTDzli
    Updated: Oct 22, 2024
    Views: 1.4K
    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
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • [Prototype]

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
  • ManojKiran Eda

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 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
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 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..
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 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
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register