shivi
shivi

Reputation: 65

C: sizeof() related doubts?

#include <stdio.h>
#include <string.h>

main()
{    
   printf("%d \n ",sizeof(' '));
   printf("%d ",sizeof(""));
}

output:

4
1

Why o/p is coming 4 for 1st printf and moreover if i am giving it as '' it is showing error as error: empty character constant but for double quote blank i.e. without any space is fine no error?

Upvotes: 6

Views: 249

Answers (7)

hk_043
hk_043

Reputation: 15

because of in 1st case there is a character that's why sizeof operator is take the SACII value of character and it's take as an integer so in 1st case it will give you 4. in 2nd case sizeof operator take as a string and in string there is no data means it's understood NULL string , so NULL string size is 1, that's why it will give you answer as a 1.

Upvotes: 0

A.s. Bhullar
A.s. Bhullar

Reputation: 2788

From C Traps and Pitfalls

Single and double quotes mean very different things in C.

A Character enclosed in single quotes is just a another way of writing the integer that corresponds to the given character in ASCII implementation. Thus ' ' means exactly same thing as 32.

On the other hand, A string enclosed in double quotes is a short-hand way of writing a pointer to the initial character of a nameless array that has been initialized with the characters between the quotes and an extra character whose binary value is zero. Thus writing "" that is empty string still has '\0' character whose size is one.

Upvotes: 0

nishantbhardwaj2002
nishantbhardwaj2002

Reputation: 767

The 'space', or 'any single character', is actually of type integer, equal to the ASCII value of that character. So it's size will be 4 bytes. If you create a character variable and store a character in it, then only it is stored in 1 byte memory.

char ch;
ch=' ';
printf("%d",sizeof(ch));
//outputs 1

For anything to be a string, it must be terminated with a null character represented as '\0'. If we write a string "hello", it is actually stored as 'h' 'e' 'l' 'l' 'o' '\0', so that the system knows string ends after the 'o' in "hello" and it stops reading when null character comes. The length of this string is still 5 if you use strlen() function but actually the sizeof(string) is 6 bytes. When we create an empty string, like "", it's length is 0 but size is 1 byte as it must terminate where it starts, i.e. at 0th character. Hence an empty string consists of only one character, that is null character, giving size 1 byte.

Upvotes: 0

vinay hunachyal
vinay hunachyal

Reputation: 3901

Here in below check the difference

 #include<stdio.h>
  int main()
  {
      char a= 'b';
      printf("%d %d %d", sizeof(a),sizeof('b'), sizeof("a"));
     return 0;

   }

here a is defined as character whose data type size is 1 byte.

But 'b' is character constant. A character constant is an integer,The value of a character constant is the numeric value of the character in the machine's character set. sizeof char constant is nothing but int which is 4 byte

this is string literals "a" ---> array character whose size is number of character + \0 (NULL). Here its 2

Upvotes: 5

TRKemp
TRKemp

Reputation: 518

This is answered in Size of character ('a') in C/C++

In C, the type of a character constant like 'a' is actually an int, with size of 4 (or some other implementation-dependent value). In C++, the type is char, with size of 1. This is one of many small differences between the two languages.

Upvotes: 3

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37954

The ' ' is example of integer character constant, which has type int (it's not converted, it has such type). Second is "" character literal, which contains only one character i.e. null character and since sizeof(char) is guaranteed to be 1, the size of whole array is 1 as well.

Upvotes: 8

Aniket Inge
Aniket Inge

Reputation: 25723

' ' is converted to an integer character constant(hence 4 bytes on your machine), "" is empty character array, which is still 1 byte('\0') terminated.

Upvotes: 6

Related Questions