Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

Union Example in C - Accept Different Data Types

Union Example in C - Accept Different Data Types

✅ C Program to Demonstrate Union with Integer, Float, and String

#include <stdio.h>
#include <string.h>

// Define a union
union Data {
    int intValue;
    float floatValue;
    char stringValue[50];
};

int main() {
    union Data data;
    int choice;

    // Ask user what type of data to enter
    printf("Choose the type of data to enter:\n");
    printf("1. Integer\n2. Float\n3. String\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
    getchar();  // Clear newline after scanf

    // Process based on choice
    if (choice == 1) {
        printf("Enter an integer: ");
        scanf("%d", &data.intValue);
        printf("You entered: %d\n", data.intValue);
    } 
    else if (choice == 2) {
        printf("Enter a float: ");
        scanf("%f", &data.floatValue);
        printf("You entered: %.2f\n", data.floatValue);
    } 
    else if (choice == 3) {
        printf("Enter a string: ");
        scanf(" %[^\n]", data.stringValue);
        printf("You entered: %s\n", data.stringValue);
    } 
    else {
        printf("Invalid choice!\n");
    }

    return 0;
}
  

πŸ“˜ Explanation:

This C program uses a union to demonstrate how a single memory location can store different types of data (integer, float, or string) at different times.

  • The user chooses what type of input they want to provide.
  • Depending on the choice, input is stored in the corresponding field of the union.
  • Only one field is valid at a time due to memory sharing in unions.
  • scanf(" %[^\n]", str) is used for string input with spaces.

🧾 Sample Output:

Choose the type of data to enter:
1. Integer
2. Float
3. String
Enter your choice: 2
Enter a float: 3.1416
You entered: 3.14

Choose the type of data to enter:
1. Integer
2. Float
3. String
Enter your choice: 3
Enter a string: Hello Union
You entered: Hello Union
  

πŸ”‘ Keywords:

union in C, memory sharing in union, scanf for string and float, union with multiple data types, interactive C program, beginner C project

πŸ“Œ Hashtags:

#CProgramming #UnionInC #BeginnerC #MemorySharing #InteractiveProgram #InterviewC

πŸ” Search Description:

This C program demonstrates how a union can store an integer, float, or string based on user input. It shows memory sharing and conditional logic in a clean, beginner-friendly example.

Comments

Popular Posts

πŸŒ™