Solve the Programming Bits

Sahithi Pallavi

Sahithi Pallavi

@sahithi-oJZaYj Oct 19, 2024
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

Welcome, guest

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

CrazyEngineers powered by Jatra Community Platform

  • Manashree Thokal

    Manashree Thokal

    @manashree-7mZH3V Aug 8, 2011

    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

    @sahithi-oJZaYj Aug 8, 2011

    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

    @manashree-7mZH3V Aug 8, 2011

    The numbers wont get accepted as the syntax is incorrect.

    It should have been- scanf("%d%d",&a,&b);
  • Sahithi Pallavi

    Sahithi Pallavi

    @sahithi-oJZaYj Aug 8, 2011

    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

    @sahithi-oJZaYj Aug 9, 2011

    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

    @pensu-8tNeGU Aug 9, 2011

    @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

    @sahithi-oJZaYj Aug 9, 2011

    @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

    @pensu-8tNeGU Aug 9, 2011

    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

    @sahithi-oJZaYj Aug 9, 2011

    @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

    @pensu-8tNeGU Aug 9, 2011

    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

    @mcxfever-A6V9P4 Aug 10, 2011

    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

    @sahithi-oJZaYj Aug 18, 2011

    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

    @silverscorpion-iJKtdQ Aug 19, 2011

    8. I think it will print

    17 21


    9. 2 2 2 2 2 2 3 4 6 5
  • Reya

    Reya

    @reya-SMihdC Aug 20, 2011

    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

    @reya-SMihdC Aug 20, 2011

    9. 2,2,2,2,2,2,3,4,6,5.
  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Aug 22, 2011

    @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

    @reya-SMihdC Aug 22, 2011

    @Simplycoder: Oops! You are right 😀
  • Rupam Das

    Rupam Das

    @rupam-das-wW5CvN Sep 1, 2011

    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

    @simplycoder-NsBEdD Sep 3, 2011

    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.