C Programming Code Examples
C > Conversions Code Examples
Convert time_t value to tm structure as UTC time: how to use gmtime
/* Convert time_t value to tm structure as UTC time: how to use gmtime */
#include <stdio.h>
#include <time.h>
#define PST (-8)
#define CET (1)
#define BJ (8)
int main ()
{
time_t rawtime;
struct tm *p;
time ( &rawtime );
p = gmtime ( &rawtime );
printf ("Time in Los Angeles: %2d:%02d\n", p->tm_hour+PST, p->tm_min);
printf ("Time in Berlin: %2d:%02d\n", p->tm_hour+CET, p->tm_min);
printf ("Time in Bei Jing: %2d:%02d\n", p->tm_hour+BJ, p->tm_min);
return 0;
}
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Convert time_t to tm as UTC time. Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed as a UTC time (i.e., the time at the GMT timezone). The gmtime() function returns a pointer to the broken-down form of time in the form of a tm structure. The time is represented in Coordinated Universal Time (UTC), which is essentially Greenwich mean time. The time pointer is usually obtained through a call to time(). If the system does not support Coordinated Universal Time, NULL is returned. The structure used by gmtime() to hold the broken-down time is statically allocated and is overwritten each time the function is called. If you wish to save the contents of the structure, you must copy it elsewhere.
Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. But structure on the other hand, can store data of any type, which is practical more useful. Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.
The time() function is defined in time.h header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.
In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Write a C program to 'Replace all Occurrence' of a character with another in a string using a Function. 'Input a string' from user, store it in some variable say str. Input old character and
Write a C program to 'Search all Occurrences' of a character in a String using Loop. Logic to search occurrences of a character in the given string. Input String from user, store it in some
C program, Using Recursion, performs binary search. In this C program, an array of random number is generated. User is asked to enter a key. The array of random numbers are sorted