1. in the prg bellow what vars are stored on stack, heap, data segment?
The concepts of "heap", "stack", and "data segment" are implementation
details independent of the C language (i.e., the language does not
mandate that some things be stored on a stack or a heap). All the
language cares about is the extent (the lifetime of the variable) and
the visibility (where the variable can be used). For example, since
it's declared at file scope, i has *static* extent (storage is
allocated at the beginning of program execution and held until the
program exits), whereas j and k have *local* extent (storage is
allocated when each variable is defined, and deallocated when main
exits). The memory allocated with malloc() (which is not the variable
k, but what k points to) has *dynamic* extent, meaning it's held until
explicitly released.
Again, whether this storage is on a stack or a heap is not defined by
the language, but up to the individual implementation.
main() should return int, not void. The following are standard
definitions for main():
int main(void)
int main(int argc, char **argv) /* or char *argv[] */
Typing main() as void will invoke undefined behavior.
{
int j;
int *k = (void *)malloc(1);
}
(I think j and k are on stack, but where is i?)
2. how to find sizeof variable w/o using sizeof command?
(no clue)
There's a sizeof operator; it's not a command. I can think of one
trick, but I don't know how reliable it would be, so I'm not going to
relay it here.
3. what is the o/p of this prg?
void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}
(I think it shoulld be printing 5 but it prints 6!)
The statement "x = x++;" invokes undefined behavior; technically
speaking, *any* answer is acceptable. It could print 5, it could
print 6, it could print 42, it could launch Rogue.
Replace that line with one of the following:
x++;
x += 1;
x = x + 1;