LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2011, 10:16 AM   #1
e3399
Member
 
Registered: Nov 2010
Location: China
Distribution: Fedora12
Posts: 54

Rep: Reputation: 0
(void)time(&cur_time); Why did the author put (void) here?


Code:
A program from 《BeginningLinuxProgramming》.
#include <stdio.h>
#include<unistd.h>
#include<time.h>

int main (int argc, char **argv)
{
    int i;
    time_t cur_time;
    struct tm *the_time;

    (void)time(&cur_time);////I don't think it needs (void) here.
    the_time = gmtime(&cur_time);
    printf("Year  Month  day\n");
    printf("%d  %d  %d\n", the_time->tm_year + 1900, the_time->tm_mon + 1,\
            the_time->tm_mday);
    printf("Time:%d:%d:%d\n", the_time->tm_hour, the_time->tm_min,\
            the_time->tm_sec);

    return 0;
}

Any help will be appreciated.
 
Old 03-28-2011, 10:19 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The void is merely to tell the program that the return value should be ignored. Certain compiler flag(s) may issue a warning if the void is not specified. The author of the code could just have easily done something like:
Code:
time_t cur_time = time(NULL);
You should read the man-page for time() to get further enlightened.
 
Old 03-28-2011, 11:21 AM   #3
e3399
Member
 
Registered: Nov 2010
Location: China
Distribution: Fedora12
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
The void is merely to tell the program that the return value should be ignored. Certain compiler flag(s) may issue a warning if the void is not specified. The author of the code could just have easily done something like:
Code:
time_t cur_time = time(NULL);
You should read the man-page for time() to get further enlightened.
Thanks.
But I still don't understand.Why the return value should be ignored here.Without (void),gcc didn't show any warning.
Do you mean we should put(void) before a function whose return value is not used.
 
Old 03-28-2011, 11:45 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Pardon my choice of words earlier... I erred in using the word "should"; I meant to use "can".

One does not need to explicitly ignore the return value; some programmers just like to be over-zealous with their code. I briefly glanced through the man-page for gcc to see if I can locate an option that would produce a warning that the return value is not be used, but I could not find one.

Suffice to say, I've been developing code for over 20 years; not once have I ever explicitly placed a void in front of a function that is called.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rpm&#347; installing to void BobNz Linux - Newbie 3 04-26-2004 02:17 AM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void? Patchorus Programming 9 10-25-2003 07:24 PM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM
Virtual void Cyth Linux - General 7 01-02-2002 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration