ctime_r() warnings

A

arnuld

I wonder why warnings are coming. I following the man page exactly:


/* Print time without a newline */
#include <stdio.h>
#include <string.h>
#include <time.h>

void print_time(void);

int main(void)
{
print_time();
return 0;
}


void print_time(void)
{
char arrt[100] = {0};
time_t now = time(NULL);
char* p;

if(NULL == ctime_r(&now, arrt))
{
printf("Failed to get time\n");
}
else
{
for(p = arrt; *p && (*p != '\n'); ++p)
{
printf("%c", *p);
}
printf(" <--- this is the time\n");
}
}

====================== OUTPUT =========================
~/programs/C $ gcc -ansi -pedantic -Wall -Wextra time.c
time.c: In function ‘print_time’:
time.c:21: warning: implicit declaration of function ‘ctime_r’
time.c:21: warning: comparison between pointer and integer
~/programs/C $ ./a.out
Tue Apr 10 19:37:41 2012 <--- this is the time
~/programs/C $
 
D

David RF

I wonder why warnings are coming. I following the man page exactly:

/* Print time without a newline */
#include <stdio.h>
#include <string.h>
#include <time.h>

void print_time(void);

int main(void)
{
  print_time();
  return 0;

}

void print_time(void)
{
  char arrt[100] = {0};
  time_t now = time(NULL);
  char* p;

  if(NULL == ctime_r(&now, arrt))
    {
      printf("Failed to get time\n");
    }
  else
    {
      for(p = arrt; *p && (*p != '\n'); ++p)
        {
          printf("%c", *p);
        }
      printf(" <--- this is the time\n");
    }

}

====================== OUTPUT=========================
~/programs/C $ gcc -ansi -pedantic -Wall -Wextra time.c
time.c: In function ‘print_time’:
time.c:21: warning: implicit declaration of function ‘ctime_r’
time.c:21: warning: comparison between pointer and integer
~/programs/C $ ./a.out
Tue Apr 10 19:37:41 2012 <--- this is the time
~/programs/C $

Turn off -ansi flag

WARNINGS
Users of asctime_r(), ctime_r(), gmtime_r(), and localtime_r()
should
also note that these functions now conform to POSIX.1c. The old
prototypes of these functions are supported for compatibility
with
existing DCE applications only.

STANDARDS CONFORMANCE
ctime(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.
1,
ANSI C

asctime_r(), ctime_r(), localtime_r(), gmtime_r(): POSIX.1c
 
J

James Kuyper

I wonder why warnings are coming. I following the man page exactly:


/* Print time without a newline */
#include <stdio.h>
#include <string.h>
#include <time.h>

void print_time(void);

int main(void)
{
print_time();
return 0;
}


void print_time(void)
{
char arrt[100] = {0};
time_t now = time(NULL);
char* p;

if(NULL == ctime_r(&now, arrt))
{
printf("Failed to get time\n");
}
else
{
for(p = arrt; *p && (*p != '\n'); ++p)
{
printf("%c", *p);
}
printf(" <--- this is the time\n");
}
}

====================== OUTPUT =========================
~/programs/C $ gcc -ansi -pedantic -Wall -Wextra time.c
time.c: In function ‘print_time’:
time.c:21: warning: implicit declaration of function ‘ctime_r’

On my system, the man page that documents ctime_r() also documents most
of the other functions declared in <time.h>. That man page says

"CONFORMING TO
SVr4, POSIX.1-2001, 4.3BSD, C89, C99."

I don't know about the other standards on that list, but while many of
the functions described on that man page are conforming with C89 and
C90, ctime_r() is not one of them - it is not mentioned in any version
of the C standard.

You've told it to compile in C90. Since ctime_r() is not a C standard
library function, <time.h> is not allowed to declare such a function
when the compiler is invoked in a strictly conforming mode. Therefore,
ctime_r is implicitly declared as a function returning an 'int' value.
This implicit declaration was a very dangerous feature of C90, which is
why it was removed in C99.
time.c:21: warning: comparison between pointer and integer

Apparently NULL is #defined as ((void*)0), or something equivalent,
which is allowed by the C standard. As such it can't be directly
compared with the 'int' value that the compiler thinks is returned by
ctime_r(). That's one of the reasons why it's a good idea to #define
NULL that way, and a bad idea to rely on implicit int declarations.
 
A

arnuld

On my system, the man page that documents ctime_r() also documents most
of the other functions declared in <time.h>. That man page says

"CONFORMING TO
SVr4, POSIX.1-2001, 4.3BSD, C89, C99."

same here.

I don't know about the other standards on that list, but while many of
the functions described on that man page are conforming with C89 and
C90, ctime_r() is not one of them - it is not mentioned in any version
of the C standard.

Yes, I have checked out both n1256 and n1336 and ctime_r() is not
mentioned anywhere in index or in section 7.23 (time.h)

You've told it to compile in C90. Since ctime_r() is not a C standard
library function, <time.h> is not allowed to declare such a function
when the compiler is invoked in a strictly conforming mode. Therefore,
ctime_r is implicitly declared as a function returning an 'int' value.
This implicit declaration was a very dangerous feature of C90, which is
why it was removed in C99.

compiling with -std=c99 gives same warning.


My concern is thread-safety. Several threads are calling same print_time
() and its crashing at while printing the time in that while() loop. So I
wanted to know if ctime_r() can be used or anything else thread-safe
(which is off-topic I guess)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top