Pelles C forum

C language => Beginner questions => Topic started by: Grincheux on January 02, 2020, 05:54:24 PM

Title: lstrlen
Post 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 :
Code: [Select]
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 :

Quote
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:

Code: [Select]
char str1[] = "String" ;
char str2[] = "\n"
"a file to \"generate\""
"%s\n" ;

From Pelle's help file :
Quote
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.
Title: Re: lstrlen
Post by: Grincheux on January 02, 2020, 06:45:43 PM
The only way to compute string length when the string is known at compile time is :


int   _i = sizeof(szString) / sizeof(char) ;
Title: Re: lstrlen
Post by: TimoVJL on January 03, 2020, 11:30:56 AM
you presumably mean:
int   _i = sizeof(szString) / sizeof(TCHAR) ;
Title: Re: lstrlen
Post by: Grincheux on January 03, 2020, 07:02:42 PM
Yes >:(
Title: Re: lstrlen
Post by: Pelle on January 05, 2020, 05:38:04 PM
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.