C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 17)
17.
If int is 2 bytes wide.What will be the output of the program?
#include <stdio.h>
void fun(char**);

int main()
{
    char *argv[] = {"ab", "cd", "ef", "gh"};
    fun(argv);
    return 0;
}
void fun(char **p)
{
    char *t;
    t = (p+= sizeof(int))[-1];
    printf("%s\n", t);
}
ab
cd
ef
gh
Answer: Option
Explanation:

Since C is a machine dependent language sizeof(int) may return different values.

The output for the above program will be cd in Windows (Turbo C) and gh in Linux (GCC).

To understand it better, compile and execute the above program in Windows (with Turbo C compiler) and in Linux (GCC compiler).

Discussion:
26 comments Page 3 of 3.

Anamika said:   1 decade ago
Can anyone explain t = (p+= sizeof(int))[-1]; ?

Sahil said:   9 years ago
Please, explain t = (p+sizeof(int))[-1].
(1)

Zdd said:   7 years ago
Thank you @Ashish Bhardwaj.

VIVEK said:   6 years ago
Thanks @Ashish Bhardwaj.
(1)

Mohit said:   9 years ago
sizeof(int) = 4 not 2.

Kushnal said:   1 decade ago
thanks bamarthi


Post your comments here:

Your comments will be displayed after verification.