
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
strftime() function in C/C++
The function strftime() is used to format the time and date as a string. It is declared in “time.h” header file in C language. It returns the total number of characters copied to the string, if string fits in less than size characters otherwise, returns zero.
Here is the syntax of strftime() in C language,
size_t strftime(char *string, size_t size, const char *format, const struct tm *time_pointer)
Here,
string − Pointer to the destination array.
size − Maximum number of characters to be copied.
format − Some special format specifiers to represent the time in tm.
time_pointer − Pointer to tm structure that contains the calendar time structure.
Here is an example of strftime() in C language,
Example
#include <stdio.h> #include <time.h> int main () { time_t tim; struct tm *detl; char buf[80]; time( &tim ); detl = localtime( &tim ); strftime(buf, 20, "%x - %I:%M%p", detl); printf("Date & time after formatting : %s", buf ); return(0); }
Output
Date & time after formatting : 10/23/18 - 10:33AM
In the above program, three variables of multiple data types are declared. The function localtime() is storing the current date and time. The function strftime() is copying the string and formatting it in some special structure by using some special specifiers.
detl = localtime( &tim ); strftime(buf, 20, "%x - %I:%M%p", detl);
- Related Articles
- strftime() function in PHP
- POSIX Function strftime() in Perl
- How do I display the date, like "Aug 5th", using Python's strftime?
- iswblank() function in C/C++
- iswpunct() function in C/C++
- strchr() function in C/C++
- strtod() function in C/C++
- memmove() function in C/C++
- memcpy() function in C/C++
- atexit() function in C/C++
- raise() function in C/C++
- mbrlen() function in C/C++
- iswlower() function in C/C++
- towupper() function in C/C++
- iswdigit() function in C/C++
