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);
}
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 1 of 3.
Rahul said:
1 decade ago
initally **p contains address of argv[0] element ie "ab".it is then incremented by 2 (size of int given as 2 bytes).now it point to "ef".It is then decremented by -1.so finally points to "cd".so it print value of t as "cd".
Kushnal said:
1 decade ago
thanks bamarthi
Youssef said:
1 decade ago
Rahul said i don't think it decrement it
p[-1]=p-1
but not
p[-1] = (p=p-1) it dose not affect the result to the var, it just takes the value and decrement it but dose not save it to the variable
p[-1]=p-1
but not
p[-1] = (p=p-1) it dose not affect the result to the var, it just takes the value and decrement it but dose not save it to the variable
Pra said:
1 decade ago
I think
P[-1] = -p
So, it decrements the value am I correct ?
P[-1] = -p
So, it decrements the value am I correct ?
Nagaraj said:
1 decade ago
Can anyone explain how the below statement gets executed
t = (p+= sizeof(int))[-1];
t = (p+= sizeof(int))[-1];
Anamika said:
1 decade ago
Can anyone explain t = (p+= sizeof(int))[-1]; ?
Mouneesha said:
1 decade ago
In general size of(int)=4 so in linux it gives p+=4-1=3 i.e index 3 gh
but they have given int is 2 bytes so it gives in windows index 1 i.e cd.
but they have given int is 2 bytes so it gives in windows index 1 i.e cd.
Kishan said:
1 decade ago
Here char *argv[]={"ab", "cd", "ef", "gh"};.
Than call fun (argv) ;.
In char **p there is "ab".
Then sizeof (int) means 2 and then 2-1=1 store in p.
So in t=1 so ab=0 cd=1 ef=2 gh=3 in array start with 0.
So print cd.
Than call fun (argv) ;.
In char **p there is "ab".
Then sizeof (int) means 2 and then 2-1=1 store in p.
So in t=1 so ab=0 cd=1 ef=2 gh=3 in array start with 0.
So print cd.
Suhas said:
1 decade ago
Explanation:
Firstly, t = (p+= sizeof(int))[-1]; is treated as
t=(p=p+(size of int) (-1));
So p="ab" and ab+2-1 becomes cd so it prints "cd" finally.
Thank you..
Firstly, t = (p+= sizeof(int))[-1]; is treated as
t=(p=p+(size of int) (-1));
So p="ab" and ab+2-1 becomes cd so it prints "cd" finally.
Thank you..
Rajesh.T.K. said:
1 decade ago
This is the statement given in the program:
(p+= sizeof(int))[-1];
If you look at the above statement, the parenthesis separates the increment part and (-1). Does this performs decrements operation?
(p+= sizeof(int))[-1];
If you look at the above statement, the parenthesis separates the increment part and (-1). Does this performs decrements operation?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers