smartbeginner said:
Hello,
My pgm is
main()
{
char *c;
printf("\n Size of c is %d",sizeof(c));
c="Im a beginner";
printf("\n Size of string c is %d",sizeof(c));
}
The o/p in both cases is 2
Can you answer me why?
Because sizeof(char *) is a compile-time constant, apparently with the
value 2 for your implementation.
Your comment in the second printf is wrong: c is a char*, not a string.
In fact, since you did not end the last output line with an end-of-line
character, your program is not portable. '\n' is the end-of-line
character, not a beginning-of-line character as your program suggests.
You have other problems.
You have no declaration for the variadic function printf(), and one is
required. #including <stdio.h> will fix this.
sizeof() gives a result as an unsigned integer of type size_t, but "%d"
is the specifier for a *signed* int of type signed int. You need to fix
this. If your library supports the specifier for size_t (unlikely), use
it. Otherwise cast the result of size_t and use the appropriate
specifier. It is usually safe to case to (unsigned long) and print with
'%lu'.
You need to find out what standard your compiler claims to conform to.
If it is C89 (or C90), then your failure to have a return or exit() at
the end of main is an error.
If it is C99, then the implicit declaration of main to return an int is
gone and you *must* supply the return type,
int main(void)
and you *should* always return values which claim to do so, but are not
required to in the special case of printf().
If you are going to post to technical newsgroups, drop the cute babytalk:
'program' is not spelled 'pgm'
'output' is not spelled 'o/p'