515 views
3 votes
3 votes
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
	char *p="good";
	char a[]="good";
	printf("%d%d%d",sizeof(p),sizeof(*p),strlen(p));
	printf("%d%d",sizeof(a),strlen(a));
	getch();
}



What will be output? Pls Explain

2 Answers

1 votes
1 votes
sizeof(p) will print 8 as it returns the size of the char pointer which can be 4 or 8 on 32bit and 64 bit machine respectively.

sizeof(*p) will return 1 since the size of 1 char("g" in this case) is 1 byte.

strlen(p) will return 4, which is quite obvious.

sizeof(a) will return 5 since it is a char array trailing with the terminator symbol

And finally, strlen(a) will return 4.
0 votes
0 votes
Considering the system to be 32 bit, means every instruction will be of 32 bit.

Now,

Char *p = 'good'; here p will be a pointer variable having the base address of string 'good'.

Char a[] =' good'; here also a is just a mnemonic having the base address of string 'good'.

Eg- lets assume base address is 'x'. So a[2] = (a+2) = (base address 'a' + 2*sizeof(char)).

Now, sizeof(p) = sizeof(a) = 4(32 bits)

         Sizeof(*p) = 4,  since *p will be pointing to string's first character and its length is 4.

        Strlen(a) = strlen(p) = 32, since length of a or p will be equal to 32 bits.

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked Apr 16
79 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
122 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
256 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how