Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 934 of 2547
Write a C program using time.h library function
The time.h library in C provides various functions to work with date and time. This library allows you to get the current system time, format it, and perform time-related calculations. Syntax #include time_t time(time_t *timer); char *strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr); struct tm *localtime(const time_t *timer); Key Functions time() − Returns the current time as seconds since January 1, 1970 (Unix timestamp) strftime() − Formats time into a string according to specified format specifiers localtime() − Converts time_t to local time structure (struct tm) ...
Read MoreWrite a C program to check the atexit() function
The atexit() is a function that allows the user to register a function that has to be called when the program terminates normally. These registered functions are called in the reverse order of their registration. It is a predefined function that is included in the stdlib.h header file. Syntax int atexit(void (*function)(void)); Parameters: function − Pointer to a function that takes no parameters and returns no value. Return Value: Returns 0 on success, non-zero on failure. Example 1: Basic Usage This example demonstrates how atexit() calls registered functions in ...
Read MoreCalculate interest amount by using formula in C language
In C programming, calculating interest on deposited amounts is a common financial calculation. This program demonstrates how to compute the final amount after applying continuous compound interest over a specified period. Syntax A = P * exp((r/100) * t) Where: P = Principal amount (initial deposit) r = Annual interest rate (percentage) t = Time period in years A = Final amount after interest Algorithm START Step 1: Declare double variables P, r, t, A, M Step 2: Read principal amount to be deposited Step 3: Read annual interest rate ...
Read MoreExplain the functions fread() and fwrite() used in files in C
In C programming, fread() and fwrite() functions are used for binary file operations to read and write entire structures or data blocks at once. These functions are particularly useful when working with structured data like records. Syntax size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); Parameters ptr − Pointer to the memory location to read/write data size − Size of each element in bytes nmemb − Number of elements to read/write stream − File pointer Return Value Both functions ...
Read MoreConcatenating n characters from source string to destination string in C
In C programming, the strncat() function is used to concatenate a specified number of characters from a source string to the end of a destination string. This function provides more control than strcat() by allowing you to limit the number of characters to be concatenated. Syntax char *strncat(char *dest, const char *src, size_t n); Parameters dest − Pointer to the destination string src − Pointer to the source string n − Maximum number of characters to concatenate Example 1: Basic strncat() Usage This example demonstrates concatenating only 4 characters from ...
Read MoreWrite a C program to read a data from file and display
In C programming, reading data from a file and displaying it is a fundamental file handling operation. This involves opening a file in read mode, reading its contents character by character, and formatting the output for better readability. Syntax FILE *fopen(const char *filename, const char *mode); int getc(FILE *stream); int fclose(FILE *stream); File Opening Modes Write mode ("w"): FILE *fp; fp = fopen("sample.txt", "w"); If the file does not exist, then a new file will be created. If the file exists, then old content gets erased and current content will ...
Read MoreWrite a C program to display the size and offset of structure members
In C programming, structures allow us to group different data types under a single name. When working with structures, it's important to understand how memory is allocated for each member and their positions within the structure. The sizeof() operator helps determine the size of structure members, while the offsetof() macro shows the byte offset of each member from the beginning of the structure. Syntax struct structure_name { datatype member1; datatype member2; datatype memberN; }; sizeof(variable_or_type) offsetof(struct_type, member_name) Structure Declaration A structure ...
Read MoreLegal and illegal declaration and initializations in C
In C programming, variable declaration and initialization must follow specific rules. Understanding legal and illegal practices helps write error-free code and avoid compilation issues. Syntax // Variable Declaration datatype variable1, variable2, ..., variableN; // Variable Initialization datatype variable = value; Variable Declaration Variables can be declared in two scopes − Global declaration: Variables declared outside any function, accessible throughout the program. Local declaration: Variables declared inside a function, accessible only within that function. #include int globalVar; /* global declaration */ int main() { ...
Read MoreHow to calculate sum of random numbers in between 0 to 100 using files in C Programming?
In this program, we generate random numbers between 0 and 100, calculate their sum, and store the result in a file. The sum will be different on each execution because random numbers are generated based on the current time. Syntax FILE *fptr = fopen("filename.txt", "w"); fprintf(fptr, "format_string", value); fclose(fptr); Algorithm The steps to calculate the sum of random numbers and store in a file are − Generate 100 random numbers between 1 and 100 Calculate the sum of all generated numbers Open a file in write mode Write the sum to the ...
Read MoreWrite a C program to maintain cricketer's information in tabular form using structures
In C programming, we can use structures to organize and store cricketer information such as name, age, number of matches, and average runs. This program demonstrates how to input multiple cricketer records and display them in a sorted tabular format based on their average runs. Syntax struct structure_name { data_type member1; data_type member2; // ... more members }; Approach We will create a structure to hold cricketer information, input data for multiple cricketers, sort them based on average runs using bubble sort, ...
Read More