C: sizeof () related to doubts?

#include <stdio.h> #include <string.h> main() { printf("%d \n ",sizeof(' ')); printf("%d ",sizeof("")); } 

output:

 4 1 

Why does o / p come 4 for the 1st printf, and besides, if I give it like '', it shows the error as an error: empty character constant, but for double quote space, i.e. without any space is not a mistake?

+6
source share
7 answers

Check the difference below.

  #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 size is 1 byte .

But 'b' is character constant . A character constant is an integer , a character constant value is a numeric value of a character in a set of machine characters. sizeof char constant is just an int which is 4 byte

it is the string literal "a" ---> an array whose size is equal to the number of characters + \0 (NULL) . Here its 2

+5
source

' ' is an example of an integer character constant that is of type int (it is not converted, it has such a type). The second is a "" character literal, which contains only one character, i.e. A null character, and since sizeof(char) guaranteed to be 1 , the size of the entire array is 1 .

+8
source

' ' converted to an integer character constant (therefore, 4 bytes on your computer), "" is an empty array of characters that still remains 1 byte ('\ 0') is complete.

+6
source

The answer is Character Size ('a') in C / C ++

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

+3
source

"Space" or "any single character" actually has an integer type equal to the ASCII value of that character. Thus, the size will be 4 bytes. If you create a symbol variable and store a symbol in it, then only it is stored in 1 byte memory.

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

For everything to be a string, it must be terminated with a null character, represented as "\ 0". If we write the string "hello", it is actually stored as 'h' 'e' 'l' 'l' 'o' '\0' , so that the system knows that the line ends after "o" in "hello" and stops read when a null character arrives. The length of this string is still 5 if you use the strlen () function, but actually sizeof (string) is 6 bytes. When we create an empty string, for example, "", the length is 0 and the size is 1 byte, since it should end where it starts, that is, at the 0th character. Therefore, an empty string consists of only one character, which is a null character, specifying a size of 1 byte.

0
source

From C Traps and Traps

Single and double quotes mean very different things in C.

A single-quoted character is just another way of writing an integer corresponding to a given character in an ASCII implementation. Thus, ' ' means the same as 32.

A double-quoted string, on the other hand, is a short way to write a pointer to the starting character of an unnamed array that was initialized with the characters between the quotation marks and an extra character whose binary value is zero. Thus, the entry ", which is an empty string, still has the character" \ 0 ", whose size is one.

0
source

because in the first case there is a character, so the sizeof operator takes the value of the SACII character and takes it as an integer, so in the 1st case it will give you 4. in the second case, the sizeof operator takes as a string, but there is no data in the string, means that it understands the NULL string, so the size of the NULL string is 1, so it will give you the answer as 1.

0
source

Source: https://habr.com/ru/post/972379/


All Articles