Discussion of chess software programming and technical issues.
Moderators: hgm , Rebel , chrisw
Look
Posts: 365 Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini
Post
by Look » Fri Sep 18, 2015 3:59 pm
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
Post
by brtzsnr » Fri Sep 18, 2015 4:14 pm
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.
Look
Posts: 365 Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini
Post
by Look » Fri Sep 18, 2015 7:18 pm
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
Post
by brtzsnr » Fri Sep 18, 2015 7:28 pm
warning: variable 'x' set but not used [-Wunused-but-set-variable]
Because x is never used (read) only set (x = ... ).
Look
Posts: 365 Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini
Post
by Look » Fri Sep 18, 2015 8:02 pm
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.
Look
Posts: 365 Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini
Post
by Look » Sat Sep 19, 2015 7:52 am
[...]
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
Post
by Sven » Sat Sep 19, 2015 1:07 pm
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);
}
Look
Posts: 365 Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini
Post
by Look » Sun Sep 20, 2015 1:37 pm
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.