Can you explain these c codes?

1. It seems pointer work is suspicious in the following code however it prints the result successfully. Please tell me why this program not gives error.
char *fun(){
char *temp = "Crazy Engineers";
return temp;
}
int main(){
puts(fun());
}
2. What is the meaning of the +i in the following code? What will be the output? The program don't give a compiler error.

main(){
int i=-1;
+i;
printf("%d %d",i,+i);
}
3. What function of x and n is computed by the code segment? And the answer is x^n.
Can you explain how to calculate it quickly?
int foo ( int x , int n){
int val=1;
if(n>0){
    if (n%2 == 1) val = val *x;
    val = val * foo(x*x , n/2);
}
return val;
}
4. The output of the following program is "Hell". Can you explain how?
main (){
unsigned i=3;
cirscr();
if(i>-1)
    printf("Hell');
else
    printf("Heaven");
}
5.The output of the following program is 256. Can you tell me how?
main(){
    unsigned char i=0x80;
    printf("\n%d",i<<1);
}
6.The output of the following code is "ligent". Can you explain why?
main(){
    printf(5+"Intelligent");
}
7. What will be the output of the following program? It is given as 2 5. Why a+1 don't give an error?
main(){
int a[5] = {1,2,3,4,5};
int *ptr = (int*)(&a+1);
printf("%d %d" , *(a+1),*(ptr-1));
}

Replies

  • simplycoder
    simplycoder
    TheCSGuy
    1. It seems pointer work is suspicious in the following code however it prints the result successfully. Please tell me why this program not gives error.
    char *fun(){
    char *temp = "Crazy Engineers";
    return temp;
    }
    int main(){
    puts(fun());
    }
    2. What is the meaning of the +i in the following code? What will be the output? The program don't give a compiler error.

    main(){
    int i=-1;
    +i;
    printf("%d %d",i,+i);
    }
    
    3. What function of x and n is computed by the code segment? And the answer is x^n.
    Can you explain how to calculate it quickly?
    int foo ( int x , int n){
    int val=1;
    if(n>0){
        if (n%2 == 1) val = val *x;
        val = val * foo(x*x , n/2);
    }
    return val;
    }
    4. The output of the following program is "Hell". Can you explain how?
    main (){
    unsigned i=3;
    cirscr();
    if(i>-1)
        printf("Hell');
    else
        printf("Heaven");
    }
    5.The output of the following program is 256. Can you tell me how?
    main(){
        unsigned char i=0x80;
        printf("\n%d",i<<1);
    }
    6.The output of the following code is "ligent". Can you explain why?
    main(){
        printf(5+"Intelligent");
    }
    7. What will be the output of the following program? It is given as 2 5. Why a+1 don't give an error?
    main(){
    int a[5] = {1,2,3,4,5};
    int *ptr = (int*)(&a+1);
    printf("%d %d" , *(a+1),*(ptr-1));
    }
    As of now I wouldnt give you the answers straight away, but would encourage you to find them on your own.
    Let us know what IDE/Compiler you are using.
    Also to debug the statements, you can use printf just after the value is changed, so that you know exactly what is happening inside the memory since the pointers deal with the content at a specific address in the memory.
    Google on the syntax, its your best friend.
    If you still dont get it even after trying, let us know.
  • TheCSGuy
    TheCSGuy
    simplycoder
    As of now I wouldnt give you the answers straight away, but would encourage you to find them on your own.
    Let us know what IDE/Compiler you are using.
    Also to debug the statements, you can use printf just after the value is changed, so that you know exactly what is happening inside the memory since the pointers deal with the content at a specific address in the memory.
    Google on the syntax, its your best friend.
    If you still dont get it even after trying, let us know.
    I am solving such kind of questions from Gate study material and these are the questions I couldn't understood from their solutions. I don't want whole answers. Just a way to the answer or point or idea is sufficient. Please tell me how to find such stuff. I am a novice.
  • simplycoder
    simplycoder
    Thanks for posting the resource. Also remember that there are many mistakes in such books.

    Let us go one by one...

    1)I dont see why you find the pointer suspicious, function would return pointer and so it does. It will print the same thing which is in the pointer.

    2)This one is tricky, Its unkown to many that unary + is just a pseudo operator, ignore this and treat +i as i things stay as they are.. so the answer is -1.

    3)This is recursive way to calculate the power of the function (While posting on forum I suggest you to use pow(x,n) instead of x^n as this causes confusion with the X-OR operator.

    Take any two number, I shall take x=4 and n as 3.
    From the first call, we can see that

    int val=1;
    if(n>0){
    if (n%2 == 1) val = val *x;//// (1)
    val = val * foo(x*x , n/2);//// (2)

    }
    return val;
    }
    when x=4 and n=3 condition 1 will satisfy,
    So val=1*4=4.
    call to 4*foo(4*4,3/2)==foo(16,1)
    after solving foo(16,1), this will return to 4*16 which will be 64 which is nothing but pow(4,3).

    4.)Its simple, i>-1 is true in above case hence it will give output "Hell".

    5.)0x80 is hexadecimal number which when converted into binary will give you
    1000 0000 now if you left shift this number by 1 bit it will give you
    0001 0000 0000 Convert this in to decimal, you would get 256

    If you know that left shifting is simply multiplying by 2 then you need not follow the above procedure. 0x80 is 128 when left shifted by 1, is equivalent to 128*2= 256.

    6.)You are just shifting the count ahead, by default printf() starts outputing from 0th character, here you are mentioning to consider the 5th character of the string to be as 0th character for printing.

    7.)For this you must know pointer arithmetics.

    Each int will take upto 4 bytes of memory.
    Let us consider that your array a starts with memory location of 1000
    Since it is an array of integers, each element in the array will occupy 4 bytes.
    So the memory address for first element is 1000, second would be 1004, third would be 1008 and so on. so array a will occupy memory locations from 1000 to 1016. also &a+1 means address of last element + 1*number of bytes occupied by a single element of a.
    This will result &a+1=1020.

    int *ptr = (int*)(&a+1); can be interpreted as value at ptr is value at 1020, which is unknown, garbage value.
    To simplify, we can see that ptr is &a+1 = 1020
    so when we do ptr-1, similar logic is used for subtracting as compared to addition and we get ptr-1=1016 and we know that 1016 is last location of array a which contains 5.
    So *(ptr-1) is saying value at (1020-1*4)==>value at (1016) which is 5
    Also, *a = a[0]. So *(a+1)=a[1]=2.

    This would print 2 5.


    I hope these clear you doubts.
  • TheCSGuy
    TheCSGuy
    Thank you buddy. It really helped me. Your style of explaining is nice. Short but Sweet.😀
  • Manveer Kaur
    Manveer Kaur
    output of 4th question is "Heaven" :/

You are reading an archived discussion.

Related Posts

I have forgotten my old username and my original email has been since deleted, therefore I could not recover myself. I decided to let the computer reengineer myself by taking...
I search information about accuracies robots​ ​
How much rank should i get to get a seat in hyderabad?? Pls help me friends. Thanks in advance.
I am a CSE 2012 graduate from a well reputed college in Noida. But i couldn't get placed in campus placements probably because of my English fluency and nervous behaviors...
Pretty awesome, I wish I was that good. lol