Solve the Programming Bits

What is the output of the following programming bits? You have to give Explanation to your answer.


1. int i=10;
printf("%d%d%d",i,i++,++i);


2. printf("Enter two values a and b:"):
scanf("%d%d",a,b);
printf("%d+%d=%d",a,b,a+b);

Replies

  • Manashree Thokal
    Manashree Thokal
    Giving a try.

    1. 10 10 12

    Explanation:
    -Initial value of i will be printed i.e. 10
    -Value of i will be printed i.e. 10 and then incremented i.e. 11
    -Value of i will be incremented i.e. 12 and then printed.

    2. scanf syntax is incorrect. So it will give an error.
  • Sahithi Pallavi
    Sahithi Pallavi
    manashree
    Giving a try.

    1. 10 10 12

    Explanation:
    -Initial value of i will be printed i.e. 10
    -Value of i will be printed i.e. 10 and then incremented i.e. 11
    -Value of i will be incremented i.e. 12 and then printed.
    Yeah even I thought the same. If this is the answer, why the companies ask this kind of simple questions? This is the question asked by IBM. Try executing it once, It shows some other output.

    2. scanf syntax is incorrect. So it will give an error.
    Error? What kinda error you will get? You won't get any output?
  • Manashree Thokal
    Manashree Thokal
    The numbers wont get accepted as the syntax is incorrect.

    It should have been- scanf("%d%d",&a,&b);
  • Sahithi Pallavi
    Sahithi Pallavi
    manashree
    The numbers wont get accepted as the syntax is incorrect.

    It should have been- scanf("%d%d",&a,&b);
    Yeah you are right, the given input numbers wont get accepted. But could you please tell me what specific Error/ Warning you will gete if you execute this?
  • Sahithi Pallavi
    Sahithi Pallavi
    why I am asking that many times because I executed that statement - the input values that I have given was not accepting, it is taking some other values instead.

    I executed this in turbo c++3.0 and I got this output :

    Enter two values a and b : 2 3
    1224 + 1230 = 2454
  • Pensu
    Pensu
    @sahithi: scanf functions is used to accept the values. If you wont give it any it will assume it by itself. So, the second program doesnt show errror, it jus give you some garbage output. The basic thing happening here is it scans the value of "a" here and as you havent initialize it, so basically its a garbage value. Hence the output.

    For the first question, i have a very convincing explaination. In printf function what actually happens is it starts executing the statements from right to left. Now in this question we have i=10. First expression from right is ++i, so it first increments the i and makes the value 11. Now next is i++, it prints 11 and makes i=12. Now next is i which is simple 12.

    P.S: These type of questions are compiler dependent. Different compiler shows different results. I am using Turbo C to explain the outputs.
  • Sahithi Pallavi
    Sahithi Pallavi
    @nicepeeyush : You are perfectly right.


    Here are the another two questions.

    3. main()
    {
    printf("hello");
    main();
    }
    how many times it will print? Give the output with explanation.


    4. what will be the output?
    int main()
    {
    char a,b;
    printf(“%d%d%d%d”,sizeof(‘A’), sizeof(‘NULL’), sizeof(‘a’), sizeof(main));
    }
  • Pensu
    Pensu
    Both are compiler dependent. In the first one: Yes, you can call main function recursively, no problem in that, provided your compiler allows you to do it. So, the answer is infinite. But as i said above depending on your compiler you can even get an error.

    For the second question, first three are easy. All the values inside ' ' are considered as integers. So it will print the size of integer. If you will use " ", then it will print the size of character. Now the fourth term, here compiler comes. Some compiler wont allow this operation. But as you have to answer it so what other compilers do is that they give every function a random size. No matter what you write here, main, scanf, printf, user defined function or anything the result would be same. Although if you write main() it will give you the size of the returning value i.e. if you are going to write int main() and then evaluate sizeof(main()), it will print 2 or 4(depending on 32 or 64 bit compiler).
  • Sahithi Pallavi
    Sahithi Pallavi
    @nicepeeyush : Hats off to you.


    Here are some more questions..



    5.what is the output?
    #DEFINE xyz(exr)printf(“exr=%d”,exr)
    main()
    {
    int x=3;y=2;
    xyz(x/y);
    }


    6.#define hello(x,y) printf(#expr"%d",expr);
    main(){
    float x=1,y=2;
    expr=x/y;
    hello(expr);
    }



    7. main(){
    main();
    }
  • Pensu
    Pensu
    5. Using pre processor directive, it will just replace the expression xyz(x/y) with defined value and exr with x/y and the important thing is that all of this happens before compilation(the reason why they are called pre processor directives). So basically when program goes to compiler its a correct program where i am printing division of two integer values.

    6. Syntax error, you can't define a micro inside another one. Nesting of micros is not allowed.

    7.Infinite loop, as i said above recursive calling of main is possible.
  • mcxfever
    mcxfever
    10, 10, 12

    Here are some question in C++


    class Test
    {
    public:
    void DoSomething(StatusInfo& rStatus);

    StatusInfo& rStatus();
    const StatusInfo& Status() const;

    private:
    StatusInfo& mrStatus;
    }


  • Sahithi Pallavi
    Sahithi Pallavi
    8.main()
    {
    int i=0;
    for(i=0;i<20;i++)
    {
    switch(i)
    case 0:i+=5;
    case 1:i+=2;
    case 5:i+=5;
    default : i+=4;
    break;
    }
    printf("%d,",i);
    }



    9. main()
    {
    int c[ ]={2.8,3.4,4,6.7,5};
    int j,*p=c,*q=c;
    for(j=0;j<5;j++)
    {
    printf(" %d ",*c);
    ++q;
    }
    for(j=0;j<5;j++)
    {
    printf(" %d ",*p);
    ++p;
    }
    }
  • silverscorpion
    silverscorpion
    8. I think it will print

    17 21


    9. 2 2 2 2 2 2 3 4 6 5
  • Reya
    Reya
    8. It will throw an error.None of the case labels are within the switch statement.

    Output will be 16,21 after debugging.
  • Reya
    Reya
    9. 2,2,2,2,2,2,3,4,6,5.
  • simplycoder
    simplycoder
    @Reya: for question number 8, I agree with the first part that the compiler will spitout an error, however, the output is dependent on how you debug the particular statement. Its just the matter of parantheses.
    int i=0;
    for(i=0;i<20;i++)
    {
      switch(i)
        {
           case 0:i+=5;
           case 1:i+=2;
           case 5:i+=5;
           default : i+=4;
           break;
    
        }
    }
    printf("%d,",i);
    
    The code might be adjusted like this aswell, to which I think the output is 22,
    Am I missing something?
  • Reya
    Reya
    @Simplycoder: Oops! You are right 😀
  • Rupam Das
    Rupam Das
    what if a and b are declared as pointers?????
    int * a,*b??? Then scanf syntax is perfect as the bit does not show a and bs definition

    Edited: Oops Threaded reply not working or what? wanted to answer for 1st question)
  • simplycoder
    simplycoder
    Hello everyone..
    I dont know whther this falls under programing bits category, but if you want some practice for programing in C or for that matter any language, you can try out the programs from #-Link-Snipped-#.

    I have personally solved every one of them and after solving them, I got a good grasp of control structures.

You are reading an archived discussion.

Related Posts

Digg Newswire is a tool to shape the breaking news/stories on Digg. We all know that stories arrive on Top News every ten minutes. Whereas, News appears in the Newswire...
Hi friends i have collected some text completion problems and wanted to share it with you, so try to answer this Text completion problems and check your answers soon: TEXT...
On Monday, the National Center for Supercomputing Applications (NCSA), University of Illinois’ and IBM has given reasons for abandoning plans to create a petaflop-speed supercomputer. A petaflop is a measure...
DRDO aka Defence Research and Development Organisation internships, key-points, eligibility criteria and career opportunities after the internship.
hi my name si Rohit. I am a student of enginerring in IT. I live in India.My hobbis are reading, computer stuff, games on computer ,travelling