What does “Size in TCHARs” means?

So we all know that char is 8-bit, and wchar_t is 16-bit. (This isn’t always true, but it is on Windows with Microsoft compilers.) Many (nearly all) of the Windows APIs are implemented under the hood in two versions: one supports Unicode (multi-byte characters) and the other supports 8-bit national character sets. These two functions actually have slightly different … Read more

How do I lowercase a string in C?

It’s in the standard library, and that’s the most straight forward way I can see to implement such a function. So yes, just loop through the string and convert each character to lowercase. Something trivial like this: or if you prefer one liners, then you can use this one by J.F. Sebastian:

How to trigger SIGUSR1 and SIGUSR2?

They are user-defined signals, so they aren’t triggered by any particular action. You can explicitly send them programmatically: where pid is the process id of the receiving process. At the receiving end, you can register a signal handler for them:

C read file line by line

I wrote this function to read a line from a file: The function reads the file correctly, and using printf I see that the constLine string did get read correctly as well. However, if I use the function e.g. like this: printf outputs gibberish. Why?

getopt_long() — proper way to use it?

First off, you probably don’t want 0 for the has_arg field – it must be one of no_argument, required_arguemnt, or optional_argument. In your case, all of them are going to be required_argument. Besides that, you’re not using the flag field correctly – it has to be an integer pointer. If the corresponding flag is set, getopt_long() will fill it in with the integer you passed in via … Read more

Understanding INADDR_ANY for socket programming

bind() of INADDR_ANY does NOT “generate a random IP”. It binds the socket to all available interfaces. For a server, you typically want to bind to all interfaces – not just “localhost”. If you wish to bind your socket to localhost only, the syntax would be my_sockaddress.sin_addr.s_addr = inet_addr(“127.0.0.1”);, then call bind(my_socket, (SOCKADDR *) &my_sockaddr, …). As it happens, INADDR_ANY is a constant that happens … Read more

Get text from user input using C

You use the wrong format specifier %d– you should use %s. Better still use fgets – scanf is not buffer safe. Go through the documentations it should not be that difficult: scanf and fgets Sample code: Input: Output:

sizeof float (3.0) vs (3.0f)

Because 3.0 is a double. See C syntax Floating point types. Floating-point constants may be written in decimal notation, e.g. 1.23. Scientific notation may be used by adding e or E followed by a decimal exponent, e.g. 1.23e2 (which has the value 123). Either a decimal point or an exponent is required (otherwise, the number is … Read more