my_err functions

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Look
Posts: 382
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(const char* const message)
{
	perror(message);
	exit(EXIT_FAILURE);
}

void my_err2(const char* const message)
{
	fprintf(stderr,message);
	exit(EXIT_FAILURE);
}

int main(void)
{
    //my_err1("Oops\n");
    //my_err2("Oops\n");
	return EXIT_SUCCESS;
}
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: 382
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(void)
{
    int x;
    int Y=0;
    errno = 0;
    x = sqrt(Y);
    if (errno) 
    {
	    perror("sqrt failed");
	    x = 0;
    }
    //x=0;
    return EXIT_SUCCESS;
}

Code: Select all

sh-4.3$ gcc -o main *.c -Wall -lm                                                                                                                                               
main.c: In function 'main':                                                                                                                                                     
main.c:8:9: warning: variable 'x' set but not used [-Wunused-but-set-variable]                                                                                                  
     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: 382
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(void) 
{ 
    int i; 
    int Y=-1; 
    errno = 0; 
    i = sqrt(Y); 
    if (errno) 
    { 
       perror("sqrt failed"); 
       exit(EXIT_FAILURE);
       //i = 0; 
    } 
    //i++; 
    return EXIT_SUCCESS; 
}
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: Numerical argument out of domain                                                                                                                                   
sh-4.3$  
Farewell.
User avatar
Look
Posts: 382
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(void) 
{ 
    int i; 
    int Y=-1; 
    errno = 0; 
    i = sqrt(Y); 
    if (errno) 
    { 
       perror("sqrt failed"); 
       exit(EXIT_FAILURE);
       //i = 0; 
    } 
    //i++; 
    return EXIT_SUCCESS; 
}
[...]

An improved version:

Code: Select all

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

void my_err1(const char* const message) 
{ 
        assert(message!=NULL);
        perror(message); 
        exit(EXIT_FAILURE); 
} 

void my_err2(const char* const message) 
{ 
        assert(message!=NULL);
        fprintf(stderr,message);
        fprintf(stderr,"\n");
        exit(EXIT_FAILURE); 
} 

int main(void) 
{ 
    int i; 
    int Y=-1; 
    errno = 0; 
    i = sqrt(Y); 
    if (errno) 
    { 
        my_err1("sqrt failed");//my_err1(NULL); //my_err1(""); 
        //my_err2("sqrt failed");
    } 
    //i++; 
    return EXIT_SUCCESS; 
}
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(const char* const message) 
{ 
        assert(message!=NULL);
        fprintf(stderr,message);
        fprintf(stderr,"\n");
        exit(EXIT_FAILURE); 
}
Even better, since printf-like functions should always get a format string as an argument (for safety reasons):

Code: Select all

void my_err2(const char* const message)
{
        assert(message!=NULL);
        fprintf(stderr, "%s\n", message);
        exit(EXIT_FAILURE);
}
User avatar
Look
Posts: 382
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(const char* const message) 
{ 
        assert(message!=NULL);
        fprintf(stderr,message);
        fprintf(stderr,"\n");
        exit(EXIT_FAILURE); 
}
Even better, since printf-like functions should always get a format string as an argument (for safety reasons):

Code: Select all

void my_err2(const char* const message)
{
        assert(message!=NULL);
        fprintf(stderr, "%s\n", message);
        exit(EXIT_FAILURE);
}
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.