This article contains instructions, advice, or how-to content .(October 2014) |
C standard library (libc) |
---|
General topics |
Miscellaneous headers |
The C date and time functions are a group of functions in the standard library of the C programming language implementing date and time manipulation operations. [1] They provide support for time acquisition, conversion between date formats, and formatted output to strings.
The format string used in strftime
traces back to at least PWB/UNIX 1.0, released in 1977. Its date
system command includes various formatting options. [2] [3] In 1989, the ANSI C standard is released including strftime
and other date and time functions. [4]
The C date and time operations are defined in the time.h
header file (ctime
header in C++).
Identifier | Description | |
---|---|---|
Time manipulation | difftime | computes the difference in seconds between two time_t values |
time | returns the current time of the system as a time_t value, number of seconds, (which is usually time since an epoch, typically the Unix epoch). The value of the epoch is operating system dependent; 1900 and 1970 are often used. See RFC 868. | |
clock | returns a processor tick count associated with the process | |
timespec_get (C11) | returns a calendar time based on a time base | |
Format conversions | asctime | converts a struct tm object to a textual representation (deprecated) |
ctime | converts a time_t value to a textual representation | |
strftime | converts a struct tm object to custom textual representation | |
strptime | converts a string with time information to a struct tm | |
wcsftime | converts a struct tm object to custom wide string textual representation | |
gmtime | converts a time_t value to calendar time expressed as Coordinated Universal Time [5] | |
localtime | converts a time_t value to calendar time expressed as local time | |
mktime | converts calendar time to a time_t value. | |
Constants | CLOCKS_PER_SEC | number of processor clock ticks per second |
TIME_UTC | time base for UTC | |
Types | struct tm | broken-down calendar time type: year, month, day, hour, minute, second |
time_t | arithmetic time type (typically time since the Unix epoch) | |
clock_t | process running time type | |
timespec | time with seconds and nanoseconds |
The timespec
and related types were originally proposed by Markus Kuhn to provide a variety of time bases, but only TIME_UTC
was accepted. [6] The functionalities were, however, added to C++ in 2020 in std::chrono.
The following C source code prints the current time to the standard output stream.
#include<time.h>#include<stdlib.h>#include<stdio.h>intmain(void){time_tcurrent_time;char*c_time_string;/* Obtain current time. */current_time=time(NULL);if(current_time==((time_t)-1)){(void)fprintf(stderr,"Failure to obtain the current time.\n");exit(EXIT_FAILURE);}/* Convert to local time format. */c_time_string=ctime(¤t_time);if(c_time_string==NULL){(void)fprintf(stderr,"Failure to convert the current time.\n");exit(EXIT_FAILURE);}/* Print to stdout. ctime() has already added a terminating newline character. */(void)printf("Current time is %s",c_time_string);exit(EXIT_SUCCESS);}
The output is:
Current time is Thu Sep 15 21:18:23 2016
C is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems code, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.
A Berkeley (BSD) socket is an application programming interface (API) for Internet domain sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules. It originated with the 4.2BSD Unix operating system, which was released in 1983.
In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.
In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console, but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.
The C preprocessor is the macro preprocessor for several computer programming languages, such as C, Objective-C, C++, and a variety of Fortran languages. The preprocessor provides inclusion of header files, macro expansions, conditional compilation, and line control.
The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>. The functionality descends from a "portable I/O package" written by Mike Lesk at Bell Labs in the early 1970s, and officially became part of the Unix operating system in Version 7.
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard. Starting from the original ANSI C standard, it was developed at the same time as the C POSIX library, which is a superset of it. Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library.
In the C++ programming language, the C++ Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself.
printf is a C standard library function that formats text and writes it to standard output.
In computing, vectored I/O, also known as scatter/gather I/O, is a method of input and output by which a single procedure call sequentially reads data from multiple buffers and writes it to a single data stream (gather), or reads data from a data stream and writes it to multiple buffers (scatter), as defined in a vector of buffers. Scatter/gather refers to the process of gathering data from, or scattering data into, the given set of buffers. Vectored I/O can operate synchronously or asynchronously. The main reasons for using vectored I/O are efficiency and convenience.
In computer science and software engineering, busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available. Spinning can also be used to generate an arbitrary time delay, a technique that was necessary on systems that lacked a method of waiting a specific length of time. Processor speeds vary greatly from computer to computer, especially as some processors are designed to dynamically adjust speed based on current workload. Consequently, spinning as a time-delay technique can produce inconsistent or even unpredictable results on different systems unless code is included to determine the time a processor takes to execute a "do nothing" loop, or the looping code explicitly checks a real-time clock.
stat is a Unix system call that returns file attributes about an inode. The semantics of stat vary between operating systems. As an example, Unix command ls uses this system call to retrieve information on files that includes:
In C programming, the functions getaddrinfo and getnameinfo convert domain names, hostnames, and IP addresses between human-readable text representations and structured binary formats for the operating system's networking API. Both functions are contained in the POSIX standard application programming interface (API).
In computing, tee
is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.
scanf, short for scan formatted, is a C standard library function that reads and parses text from standard input.
assert.h is a header file in the C standard library. It defines the C preprocessor macro assert
and implements runtime assertion in C.
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior version of the C++ standard, named C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.
In the C Standard Library, signal processing defines how a program handles various signals while it executes. A signal can report some exceptional behavior within the program, or a signal can report some asynchronous event outside the program.
stdarg.h
is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg
.
select is a system call and application programming interface (API) in Unix-like and POSIX-compliant operating systems for examining the status of file descriptors of open input/output channels. The select system call is similar to the poll facility introduced in UNIX System V and later operating systems. However, with the c10k problem, both select and poll have been superseded by the likes of kqueue, epoll, /dev/poll and I/O completion ports.