Pelles C forum
C language => Beginner questions => Topic started by: Grincheux on January 02, 2020, 05:54:24 PM
-
Why when a string is defined as a constant the c compiler calls the lstrlen function? It knows its length at compile time. I don't understand.
Example :static void Main_OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
RECT rc;
int _i = lstrlen("Hello, Windows!") ;
BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
DrawText(ps.hdc, _T("Hello, Windows!"), -1, &rc, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd, &ps);
}
The asm code is :
lea rcx,[StringAdr]
call qword ptr [__imp_lstrlenA]
Is there an other way to compute a string length in that case.
I have strings as the following:
char str1[] = "String" ;
char str2[] = "\n"
"a file to \"generate\""
"%s\n" ;
From Pelle's help file : 6. Adjacent string literal tokens are concatenated into one string literal token ("abc" "def" becomes "abcdef").
An other question. In the previous code I have noticed that if a string exists more than once, it is created only once. Please confirm.
-
The only way to compute string length when the string is known at compile time is :
int _i = sizeof(szString) / sizeof(char) ;
-
you presumably mean:
int _i = sizeof(szString) / sizeof(TCHAR) ;
-
Yes >:(
-
Why when a string is defined as a constant the c compiler calls the lstrlen function?
Because all Windows functions are just that: functions. Black boxes. Unknown what the function does.
Use one of the standard C functions, that the compiler have any reason to care about, like strlen() or wcslen().
It knows its length at compile time.
No it doesn't. See above.