The "sizeof operator with string" bug when use command line in "output window"

I do not speak english,so i try to show my meaning

When you use command line in debug mode,
sizeof("a") //value is 1
sizeof('a') //value is 2
would got wrong value.

seems been upended.

IDE-Version:
礦ision3 V3.53
Copyright (c) Keil Elektronik GmbH / Keil Software, Inc. 1995 - 2007
Tool Version Numbers:
Toolchain Path: C:\KeilC51\C51\BIN\
C Compiler: C51.Exe V8.09
Assembler: A51.Exe V8.00d
Linker/Locator: BL51.Exe V6.05
Librarian: LIB51.Exe V4.24
Hex Converter: OH51.Exe V2.6
CPU DLL: S8051.DLL V3.12
Dialog DLL: DCYG.DLL V2.44d

Parents
  • There's no such thing as a string data type in C. The standard library supports a type, which happens to be built on the types "char" and "pointer to char". There's also a "string literal" useful for initializing arrays of char. But "string" is not really a first-class data type in the language.

    sizeof returns the size (in units of the size of a char, which is not necessarily one byte, though that's extremely common) of the type of the expression to which it is applied. Note that it's technically an operator, not a function, even though almost everyone writes their code as though it were a function. The parens are there from habit, but also so you don't have to worry about the precedence of sizeof relative to the other operators in your intended expression.

    sizeof("a") returns the size of a pointer to char. Depending on the memory model, this might actually be one byte long. A pointer-to-data or pointer-to-pdata is one byte. Pointers to xdata are two. "Generic" pointers (to unknown memory type, so they're type-tagged) or pointers to far memory are three bytes long.

    sizeof('a') is the size of the type of 'a'. 'a' is more formally known as an "integer character constant", and -- oddly enough -- has type "int" and not type "char". So, sizeof('a') is the same as sizeof(int), which is 2 for Keil C51.

    If you want to count the number of characters in a string, use the library function strlen().

Reply
  • There's no such thing as a string data type in C. The standard library supports a type, which happens to be built on the types "char" and "pointer to char". There's also a "string literal" useful for initializing arrays of char. But "string" is not really a first-class data type in the language.

    sizeof returns the size (in units of the size of a char, which is not necessarily one byte, though that's extremely common) of the type of the expression to which it is applied. Note that it's technically an operator, not a function, even though almost everyone writes their code as though it were a function. The parens are there from habit, but also so you don't have to worry about the precedence of sizeof relative to the other operators in your intended expression.

    sizeof("a") returns the size of a pointer to char. Depending on the memory model, this might actually be one byte long. A pointer-to-data or pointer-to-pdata is one byte. Pointers to xdata are two. "Generic" pointers (to unknown memory type, so they're type-tagged) or pointers to far memory are three bytes long.

    sizeof('a') is the size of the type of 'a'. 'a' is more formally known as an "integer character constant", and -- oddly enough -- has type "int" and not type "char". So, sizeof('a') is the same as sizeof(int), which is 2 for Keil C51.

    If you want to count the number of characters in a string, use the library function strlen().

Children
More questions in this forum