6
#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?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
shivi
  • 65
  • 3
  • type of `' '` is `int`. `""` is like `char array[] = { '\0' };` sizeof(array) is 1. – BLUEPIXY Jul 17 '14 at 12:17
  • 2
    Tip: Always compile with full warnings enabled: `-Wall -Wextra -pedantic` or whatever is appropriate on your machine. If you do so, your compiler should warn about implicit `int` return type for `main`, so correct that. – Deduplicator Jul 17 '14 at 12:22
  • 2
    And if your compiler's nice, it may also warn you about the wrong format specifier: The result of `sizeof` has type `size_t` and the `printf` format specifier for `size_t` is `zu`, not `d`. – mafso Jul 17 '14 at 12:26
  • Closely related to [Logic behind sizeof() for character constants and function names](http://stackoverflow.com/q/19906802/1708801). – Shafik Yaghmour Jul 17 '14 at 13:39

7 Answers7

8

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.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
6

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

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
5

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

vinay hunachyal
  • 3,781
  • 2
  • 20
  • 31
  • Simply In `C` single quotes such as `'a'` indicate character constants whereas `"a"` is an array of characters, always terminated with the 0 character. – Jayesh Bhoi Jul 17 '14 at 12:26
3

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.

Community
  • 1
  • 1
TRKemp
  • 518
  • 2
  • 6
0

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.

nishantbhardwaj2002
  • 757
  • 2
  • 6
  • 18
0

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.

A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32
0

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.

hk_043
  • 15
  • 4