C Articles

Page 17 of 96

Write a C program to read a data from file and display

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 17K+ Views

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 More

Write a C program to display the size and offset of structure members

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 1K+ Views

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 More

Legal and illegal declaration and initializations in C

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 2K+ Views

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 More

How to calculate sum of random numbers in between 0 to 100 using files in C Programming?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 2K+ Views

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 More

Write a C program to maintain cricketer's information in tabular form using structures

Mandalika
Mandalika
Updated on 15-Mar-2026 8K+ Views

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

Write a C program to calculate the average word length of a sentence using while loop

Mandalika
Mandalika
Updated on 15-Mar-2026 1K+ Views

In C programming, calculating the average word length of a sentence involves counting the total characters (excluding spaces) and dividing by the number of words. This program demonstrates how to use a while loop to process user input character by character and calculate the average word length. Syntax while(condition) { // Process each character // Count characters and words } Algorithm Read the sentence character by character using a while loop Count total characters (excluding spaces) and words Calculate average by dividing character count by ...

Read More

Write a C program to print numbers in words using elseif statements

Mandalika
Mandalika
Updated on 15-Mar-2026 5K+ Views

In C programming, we can convert numbers into their word equivalents using else-if statements instead of switch cases. This approach is particularly useful for handling two-digit numbers by breaking them into tens and units places. Syntax if (condition1) { // statements } else if (condition2) { // statements } else if (condition3) { // statements } else { // default statements } Algorithm The program uses multiple else-if conditions to handle different number ranges − Range ...

Read More

Write a C program to print the message in reverse order using for loop in strings

Mandalika
Mandalika
Updated on 15-Mar-2026 870 Views

Here we write a program to reverse the sentence without predefined functions. By using for loop, we can easily print statement in reverse order. Syntax for(initialization; condition; increment/decrement) { // Read characters } for(reverse_initialization; reverse_condition; reverse_increment) { // Print characters in reverse } Method 1: Character-by-Character Input Using getchar() This approach reads each character individually using getchar() and stores them in an array. Then it prints the characters in reverse order − #include int main() { char stmt[100]; ...

Read More

How to print a one-month calendar of user choice using for loop in C?

Mandalika
Mandalika
Updated on 15-Mar-2026 6K+ Views

A one-month calendar can be printed in C using for loops by positioning the first day correctly and then printing all days in a week-by-week format. The key is to add proper spacing before the first day and break lines after every 7 days. Syntax for(i = 1; i < firstDay; i++) printf(" "); // Add spaces before first day for(i = 1; i

Read More

Generate an even squares between 1 to n using for loop in C Programming

Mandalika
Mandalika
Updated on 15-Mar-2026 1K+ Views

Even square numbers are the squares of even integers like 22, 42, 62, 82, which result in 4, 16, 36, 64, 100, and so on. In this tutorial, we will generate even squares between 1 to n using a for loop in C programming. Syntax for (initialization; condition; increment) { // Loop body } Algorithm START Step 1: Declare variables a and n Step 2: Read number n from user Step 3: Use for loop to generate even squares For a = 2; a*a

Read More
Showing 161–170 of 953 articles
« Prev 1 15 16 17 18 19 96 Next »
Advertisements