write a c program for printing time date using Pre-define Macros

28 Dec 2022 Balmiki Mandal 0 C Programming

C Program: Print Date and Time using Pre-defined Macros

Introduction

Welcome to our tutorial on how to print the current time and date using pre-defined macros in the C programming language.

Objective

The goal of this tutorial is to demonstrate how to utilize pre-defined macros in C to efficiently retrieve and display the current time and date.

Prerequisites

  • Basic understanding of C programming language.
  • A C compiler installed on your system.

Step 1: Including Necessary Libraries

#include <stdio.h>

We include the header file for standard input and output functions and for time-related functions.

Step 2: Writing the Main Function

int main() {
    // Code goes here
    return 0;
}

We start by defining the main function, which is the entry point of our program.

Step 3: Defining Variables

time_t rawtime;
struct tm *info;
char buffer[80];
  • time_t rawtime;: A variable to hold the raw time data.
  • struct tm *info;: A pointer to a structure (tm) that holds time information.
  • char buffer[80];: A character array to store the formatted time and date.

Step 4: Getting Current Time

time(&rawtime);
info = localtime(&rawtime);
  • time(&rawtime);: Retrieves the current time and stores it in the rawtime variable.
  • localtime(&rawtime);: Converts the raw time to a structure (tm) representing the local time.

Step 5: Formatting and Printing

strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
printf("Current date and time: %s\n", buffer);
  • strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);: Formats the time and date and stores it in the buffer variable.
  • printf("Current date and time: %s\n", buffer);: Prints the formatted time and date.

Example 01: Full source code

#include
#include 
int main() {
    time_t rawtime;
struct tm *info;
char buffer[80];
time(&rawtime);
info = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
printf("Current date and time: %s\n", buffer);

    return 0;
}

Step 6: Running the Program

  • Save the file with a .c extension (e.g., datetime.c) and compile it using your preferred C compiler.

  • Execute the program to see the current date and time printed on the screen.


Example 02: Full source code

#include
#include 

int main() {
    // Declare a structure to hold time information
    struct tm *local;
    time_t t;

    // Get the current time
    t = time(NULL);
    local = localtime(&t);

    // Print the date and time
    printf("Current Date and Time: %s\n", asctime(local));

    return 0;
}

Example 03:Writing a C Program to Print Time and Date Using Predefined Macros

#include
void main()
{
    printf("%s\n",__FILE__);
    printf("%d\n",__LINE__);
    printf("%s\n",__DATE__);
    printf("%s\n",__TIME__);
}

Output:

5
Feb 27 2022
23:52:20

Conclusion

This program demonstrates a simple way to access and print the current date and time using pre-defined macros provided by the C Standard Library. Understanding how to utilize macros and system time functions is an essential skill for any C programmer.

Top Resources

write a macro basic program in c

write a program to Multiplication of two number using macros in c

define a macros for finding the biggest of two numbers

write a c program for printing time date using Pre-define Macros

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.