0% found this document useful (0 votes)
7 views9 pages

C Programs: Structs, Unions, Enums, Files

The document contains multiple C programs demonstrating the use of structures, unions, enumerated data types, and file operations. It includes examples for managing student information, performing basic calculator operations, controlling traffic light signals, and reading/writing data to files. Each program is self-contained and illustrates specific programming concepts effectively.

Uploaded by

vidhya.ooty
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

C Programs: Structs, Unions, Enums, Files

The document contains multiple C programs demonstrating the use of structures, unions, enumerated data types, and file operations. It includes examples for managing student information, performing basic calculator operations, controlling traffic light signals, and reading/writing data to files. Each program is self-contained and illustrates specific programming concepts effectively.

Uploaded by

vidhya.ooty
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Student Information using Structure

#include <stdio.h>

struct Student {

int rollNo;

char name[50];

float marks;

};

int main() {

struct Student s1; // Declare a structure variable

// Input student details

printf("Enter Roll Number: ");

scanf("%d", &[Link]);

printf("Enter Name: ");

scanf("%s", [Link]);

printf("Enter Marks: ");

scanf("%f", &[Link]);

// Display student details

printf("\n--- Student Details ---\n");

printf("Roll Number: %d\n", [Link]);

printf("Name: %s\n", [Link]);

printf("Marks: %.2f\n", [Link]);

return 0;

}
Union program in c
#include <stdio.h>

union Data {

int i;

float f;

char str[20];

};

int main() {

union Data data;

// Assign integer value

data.i = 10;

printf("data.i = %d\n", data.i);

// Assign float value

data.f = 220.5;

printf("data.f = %.2f\n", data.f);

// Assign string value

strcpy([Link], "Hello");

printf("[Link] = %s\n", [Link]);

// Notice how previous values are overwritten

printf("\nAfter all assignments:\n");

printf("data.i = %d\n", data.i);

printf("data.f = %.2f\n", data.f);


printf("[Link] = %s\n", [Link]);

return 0;

Enumerated data type in c program

#include <stdio.h>

enum Weekday {

Sunday, // 0

Monday, // 1

Tuesday, // 2

Wednesday, // 3

Thursday, // 4

Friday, // 5

Saturday // 6

};

int main() {

enum Weekday today;

// Assign a value

today = Wednesday;

// Display the value

printf("Day number: %d\n", today);

// Print day name using switch

switch (today) {

case Sunday: printf("It's Sunday!\n"); break;

case Monday: printf("It's Monday!\n"); break;

case Tuesday: printf("It's Tuesday!\n"); break;


case Wednesday: printf("It's Wednesday!\n"); break;

case Thursday: printf("It's Thursday!\n"); break;

case Friday: printf("It's Friday!\n"); break;

case Saturday: printf("It's Saturday!\n"); break;

default: printf("Invalid day!\n");

return 0;

Calculator using enumerated datatype in C


#include <stdio.h>

// Define an enumerated type for calculator operations

enum Operation {

ADD = 1,

SUBTRACT,

MULTIPLY,

DIVIDE,

EXIT

};

int main() {

int choice;

float num1, num2, result;

while (1) {

printf("\n=== Simple Calculator ===\n");

printf("1. Add\n");

printf("2. Subtract\n");

printf("3. Multiply\n");
printf("4. Divide\n");

printf("5. Exit\n");

printf("Enter your choice (1-5): ");

scanf("%d", &choice);

if (choice == EXIT) {

printf("Exiting the program. Goodbye!\n");

break;

printf("Enter two numbers: ");

scanf("%f %f", &num1, &num2);

switch (choice) {

case ADD:

result = num1 + num2;

printf("Result: %.2f\n", result);

break;

case SUBTRACT:

result = num1 - num2;

printf("Result: %.2f\n", result);

break;

case MULTIPLY:

result = num1 * num2;

printf("Result: %.2f\n", result);

break;

case DIVIDE:

if (num2 != 0)
printf("Result: %.2f\n", num1 / num2);

else

printf("Error: Division by zero!\n");

break;

default:

printf("Invalid choice! Please try again.\n");

return 0;

Traffic Light Signal using enumerated datatype in C


#include <stdio.h>

// Define an enumerated type for traffic lights

enum TrafficLight {

RED = 1,

YELLOW,

GREEN

};

int main() {

enum TrafficLight signal;

printf("Traffic Light Signals:\n");

printf("1. RED\n");

printf("2. YELLOW\n");

printf("3. GREEN\n");

printf("Enter the signal number (1-3): ");


scanf("%d", &signal);

switch (signal) {

case RED:

printf("\nSignal: RED\n");

printf("Action: STOP! The light is red.\n");

break;

case YELLOW:

printf("\nSignal: YELLOW\n");

printf("Action: WAIT! The light is about to change.\n");

break;

case GREEN:

printf("\nSignal: GREEN\n");

printf("Action: GO! The road is clear.\n");

break;

default:

printf("\nInvalid signal! Please enter 1, 2, or 3.\n");

return 0;

Write and Read from a File in C


#include <stdio.h>

int main() {

FILE *fptr;

char name[50];

int age;
// ---------- Writing to a file ----------

fptr = fopen("[Link]", "w"); // Open file in write mode

if (fptr == NULL) {

printf("Error opening file!\n");

return 1;

printf("Enter your name: ");

scanf("%s", name);

printf("Enter your age: ");

scanf("%d", &age);

fprintf(fptr, "Name: %s\nAge: %d\n", name, age); // Write data to file

fclose(fptr);

printf("\nData written to file successfully!\n");

// ---------- Reading from the file ----------

fptr = fopen("[Link]", "r"); // Open file in read mode

if (fptr == NULL) {

printf("Error opening file for reading!\n");

return 1;

printf("\n--- Reading from file ---\n");

char ch;

while ((ch = fgetc(fptr)) != EOF) { // Read character by character

putchar(ch);

fclose(fptr);

return 0;
}

You might also like