my_err functions

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Look
Posts: 365
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

my_err functions

Post by Look »

Hi,

In the following code, what is the difference between my_err1 and my_err2, if there is any.

Code: Select all

#include <stdlib.h>
#include <stdio.h>


void my_err1&#40;const char* const message&#41;
&#123;
	perror&#40;message&#41;;
	exit&#40;EXIT_FAILURE&#41;;
&#125;

void my_err2&#40;const char* const message&#41;
&#123;
	fprintf&#40;stderr,message&#41;;
	exit&#40;EXIT_FAILURE&#41;;
&#125;

int main&#40;void&#41;
&#123;
    //my_err1&#40;"Oops\n");
    //my_err2&#40;"Oops\n");
	return EXIT_SUCCESS;
&#125;
Farewell.
brtzsnr
Posts: 433
Joined: Fri Jan 16, 2015 4:02 pm

Re: my_err functions

Post by brtzsnr »

man 3 perror
The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a system or library function.
perror prints the errno when something like open, creat fails.

fprintf(stderr, "...") just prints to stderr.
User avatar
Look
Posts: 365
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: my_err functions

Post by Look »

brtzsnr wrote:man 3 perror
The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a system or library function.
perror prints the errno when something like open, creat fails.

fprintf(stderr, "...") just prints to stderr.
Why I get this nasty warning?

Code: Select all

#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main&#40;void&#41;
&#123;
    int x;
    int Y=0;
    errno = 0;
    x = sqrt&#40;Y&#41;;
    if &#40;errno&#41; 
    &#123;
	    perror&#40;"sqrt failed");
	    x = 0;
    &#125;
    //x=0;
    return EXIT_SUCCESS;
&#125;

Code: Select all

sh-4.3$ gcc -o main *.c -Wall -lm                                                                                                                                               
main.c&#58; In function 'main'&#58;                                                                                                                                                     
main.c&#58;8&#58;9&#58; warning&#58; variable 'x' set but not used &#91;-Wunused-but-set-variable&#93;                                                                                                  
     int x;                                                                                                                                                                     
         ^    
Farewell.
brtzsnr
Posts: 433
Joined: Fri Jan 16, 2015 4:02 pm

Re: my_err functions

Post by brtzsnr »

warning: variable 'x' set but not used [-Wunused-but-set-variable]
Because x is never used (read) only set (x = ... ).
User avatar
Look
Posts: 365
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: my_err functions

Post by Look »

brtzsnr wrote:man 3 perror
The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a system or library function.
perror prints the errno when something like open, creat fails.

fprintf(stderr, "...") just prints to stderr.
OK, now I understand it better:

Code: Select all

#include <math.h> 
#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main&#40;void&#41; 
&#123; 
    int i; 
    int Y=-1; 
    errno = 0; 
    i = sqrt&#40;Y&#41;; 
    if &#40;errno&#41; 
    &#123; 
       perror&#40;"sqrt failed"); 
       exit&#40;EXIT_FAILURE&#41;;
       //i = 0; 
    &#125; 
    //i++; 
    return EXIT_SUCCESS; 
&#125;
http://www.tutorialspoint.com/compile_c_online.php
GCC output (without -Wall):

Code: Select all

sh-4.3$ gcc -o main *.c -lm                                                                                                                                                     
sh-4.3$ main                                                                                                                                                                    
sqrt failed&#58; Numerical argument out of domain                                                                                                                                   
sh-4.3$  
Farewell.
User avatar
Look
Posts: 365
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: my_err functions

Post by Look »

[...]

Code: Select all

#include <math.h> 
#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main&#40;void&#41; 
&#123; 
    int i; 
    int Y=-1; 
    errno = 0; 
    i = sqrt&#40;Y&#41;; 
    if &#40;errno&#41; 
    &#123; 
       perror&#40;"sqrt failed"); 
       exit&#40;EXIT_FAILURE&#41;;
       //i = 0; 
    &#125; 
    //i++; 
    return EXIT_SUCCESS; 
&#125;
[...]

An improved version:

Code: Select all

#include <math.h> 
#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h>

void my_err1&#40;const char* const message&#41; 
&#123; 
        assert&#40;message!=NULL&#41;;
        perror&#40;message&#41;; 
        exit&#40;EXIT_FAILURE&#41;; 
&#125; 

void my_err2&#40;const char* const message&#41; 
&#123; 
        assert&#40;message!=NULL&#41;;
        fprintf&#40;stderr,message&#41;;
        fprintf&#40;stderr,"\n");
        exit&#40;EXIT_FAILURE&#41;; 
&#125; 

int main&#40;void&#41; 
&#123; 
    int i; 
    int Y=-1; 
    errno = 0; 
    i = sqrt&#40;Y&#41;; 
    if &#40;errno&#41; 
    &#123; 
        my_err1&#40;"sqrt failed");//my_err1&#40;NULL&#41;; //my_err1&#40;""); 
        //my_err2&#40;"sqrt failed");
    &#125; 
    //i++; 
    return EXIT_SUCCESS; 
&#125;
Farewell.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: my_err functions

Post by Sven »

Look wrote:

Code: Select all

void my_err2&#40;const char* const message&#41; 
&#123; 
        assert&#40;message!=NULL&#41;;
        fprintf&#40;stderr,message&#41;;
        fprintf&#40;stderr,"\n");
        exit&#40;EXIT_FAILURE&#41;; 
&#125;
Even better, since printf-like functions should always get a format string as an argument (for safety reasons):

Code: Select all

void my_err2&#40;const char* const message&#41;
&#123;
        assert&#40;message!=NULL&#41;;
        fprintf&#40;stderr, "%s\n", message&#41;;
        exit&#40;EXIT_FAILURE&#41;;
&#125;
User avatar
Look
Posts: 365
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: my_err functions

Post by Look »

Sven Schüle wrote:
Look wrote:

Code: Select all

void my_err2&#40;const char* const message&#41; 
&#123; 
        assert&#40;message!=NULL&#41;;
        fprintf&#40;stderr,message&#41;;
        fprintf&#40;stderr,"\n");
        exit&#40;EXIT_FAILURE&#41;; 
&#125;
Even better, since printf-like functions should always get a format string as an argument (for safety reasons):

Code: Select all

void my_err2&#40;const char* const message&#41;
&#123;
        assert&#40;message!=NULL&#41;;
        fprintf&#40;stderr, "%s\n", message&#41;;
        exit&#40;EXIT_FAILURE&#41;;
&#125;
Yes, I remembered about this rule. Why online compilers GCC (-Wall -Wextra -Wpedantic) or "Visual C++" (/W4) did not put a diagnostic for my questionable my_err2 function?
Farewell.