retagged by
1,129 views
1 votes
1 votes
#include<stdio.h>
int main()
{
    char *s="\123456789\n";
    printf("%d", sizeof(s));
    return 0;
}

why this piece of code gives output $8$

please explain. thanx

retagged by

2 Answers

Best answer
5 votes
5 votes

char *s is read as s is a pointer to char

size of char is different when compare to int or float or double

but with respective of pointers, size of character pointer or integer pointer or float pointer or double pointer all are same.. but size is compiler dependent...

32 bit compiler... size of pointer is 4

64 bit compiler... size of pointer is 8 

selected by
1 votes
1 votes

Because S is char pointer which hold the starting  address of string array which is of char type.simple s is a pointer which can hold 8 byte address.

For example-

A 1 2 3 4 5 6 7 8 9 \0
Address     100 101 102 103 104 105 106 107 108 109

   S contain starting address of A

S
100

500
So size of s is 8 byte
 

Related questions

1 votes
1 votes
1 answer
1
radha gogia asked Jul 10, 2018
1,661 views
If I have an array :int a [3][4] ; How to evaluate below Outputs ? 1. sizeof(*a) 2.sizeof( a) 3. sizeof(a) Please explain precisely .
1 votes
1 votes
1 answer
2
Shiva Sagar Rao asked Feb 16, 2019
642 views
#include <stdio.h int main() { int a = 1; char d[] = "ab"; printf("%d", sizeof(a+d)); return 0; }Explain the Output
1 votes
1 votes
1 answer
3
saumya mishra asked May 15, 2018
1,603 views
#include<stdio.h int main(void) { printf("bytes occupied by '7' = %d\n", sizeof('7')); printf("bytes occupied by 7 = %d\n", sizeof(7)); printf("bytes occupied by 7.0 = %d...
3 votes
3 votes
1 answer
4
Lakshman Bhaiya asked Apr 23, 2018
952 views
Q)What is the output of the following C program fragment?Assume size of an integer is 4 Bytes#include<stdio.h>int main(){int i = 5;int var = sizeof( i++);printf("%d %d",i...