Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is ctime in C?

Sheza Munir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ctime function returns a string containing the local time. It is based on the timer argument given to the function.

The ctime function converts the local time state to a readable text timestamp.

Library

The ctime function can be accessed via time.h library.

Parameter

The parameter of the ctime function is a pointer to a time_t object.

The time_t datatype is used to store system time values. It contains a calendar time value that is the pointer to which is given as a parameter to the function.

Syntax

char *ctime(const time_t *timer)

Return value

The ctime function returns a string of readable text containing the date and time.

Return value format

The return string has the format: Www Mmm dd hh:mm:ss yyyy

where:

Www: Day of the week
Mmm: Month of the year
dd: Day of the month
hh: Hour of the day
mm: Minute of the hour
ss: Second of the minute
yyyy: Year

Code

#include <stdio.h>
#include <time.h> // this library contains ctime function

int main() {
  time_t current_time; // this variable stores current time state
  
  time(&current_time);

  printf("Time returned in the specified format is: %s", ctime(&current_time));
  return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring