📜 ⬆️ ⬇️

Hello World! how should he be on C in Linux

Very many novice programmers think they know how to write Hello World. Naturally, most of the textbooks begin with this example.
And let's see how this is done.
Usually in a C textbook, this program looks like this:
#include <stdio.h>
void main ( )
{
printf ( "Hello world \n " ) ;
}


The first thing we see here is that the person running this program will never know how it was executed. So you need to add a return code. The printf function, as we know, returns the number of characters printed. Or, if there was an error, a negative value. Given this, the program will look something like this:
#include <stdio.h>
#include <string.h>

int main ( )
{
const char * msg = "Hello world!\n "
;
int printf_res = printf ( msg ) ;
if ( printf_res < strlen ( msg ) )
{
return 1 ;
} else {
return 0 ;
}
}


Forgive me, dear readers, but then I will add many corrections to this at once, otherwise this post will grow so that no one can read it to the end. Whoever wants, can correct his code every time the next paragraph ends. And it will be right. This is the best way to understand what I want to say with this post.

Now let's think a little further.
Is it really a critical error that we did not manage to print the entire message right away? Of course not! After all, not always what we run, writes lines to the console. And there is a difference. We are now writing under Linux (have you forgotten about this?), But it’s very different. And the output can be sent, for example, to some strange device that simply cannot record more than one character at a time. So one printf will not be enough for us; need to write until it works.
And now, what if we don't have “Hello World! \ N”? And if there is a line like "Use% s to print string"? We cannot use printf here if we want this code to be used elsewhere. But we want this, otherwise what kind of programmers are we?
And by the way, what does an error mean for printf ()? Yes, whatever, we do not know anything about it until we get into the code of libraries.
So apparently we have to use something lower level. What do we have then? puts ()? Moreover, we do not know anything about errors. Apparently you need to use write ().
So, what we have at the moment? We use write (). If not everything is recorded in the output, we continue from where we left off. We handle errors.
By the way, what about errors? You have already watched man 2 write? Did not see? And in vain. After all, if the write () function returns a negative value, this does not necessarily mean that we are not lucky. Let's look at man. And in fact, if we look carefully, we will see there that a negative value can be returned to us if the call was interrupted by a signal. (You do not know what signals are? Find out urgently if you want to write under Linux). But this is not necessarily a mistake. What if the user just at that moment changed some configuration file and sent SIGHUP to all processes, which usually means “reconfigure”?

And at this moment I - surprise surprise - show the code of what we should have turned out by this moment.
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main ( )
{
const char * const msg = "Hello World! \n " ;
const char * begin = msg ;
const char * const end = begin + strlen ( msg ) ;

while ( begin < end )
{
size_t remaining = end - begin ;
ssize_t res = write ( STDOUT_FILENO , begin , remaining ) ;
if ( res >= 0 )
{
begin += res ;
continue ; // Let's send the remaining part of this message
}
if ( EINTR == errno )
{
continue ; // It's just a signal, try again
}
return 1 ; // It's a real error
}
return 0 ; // OK! Let's celebrate and drink some beer!
}


So in my opinion should be the correct hello world. Maybe I forgot to process something somewhere. In this case, better not minus immediately, but correct in the comments.
If this article is liked by habragiers, there will be more articles on the topic “how textbooks differ from real life”.
')
______________________
The text was prepared in the Habr Editor from © SoftCoder.ru

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


All Articles