C Programming for Beginners: Practice Problem and Step-by-Step Solution

C Programming for Beginners: Practice Problem and Step-by-Step Solution.

 

Q1.Write a C program to illustrate the comparison of two structure variables and print equal or not taking individual members.

SOLUTION:-

#include <stdio.h>

typedef struct {
    int num1;
    float num2;
    char ch;
} MyStruct;

int main() {
    MyStruct data1 = {10, 3.14, 'A'};
    MyStruct data2 = {10, 3.14, 'A'};

    int all_equal = 1;  // Flag to track overall equality

    // Compare entire structures using memcmp (for basic data types)
    if (memcmp(&data1, &data2, sizeof(data1)) != 0) {
        all_equal = 0;
        printf("Structures are NOT equal\n");
    }

    // If overall equality hasn't been determined as false yet,
    // compare individual members for confirmation
    if (all_equal) {
        if (data1.num1 != data2.num1) {
            all_equal = 0;
            printf("num1 values differ\n");
        }
        if (data1.num2 != data2.num2) {
            all_equal = 0;
            printf("num2 values differ\n");
        }
        if (data1.ch != data2.ch) {
            all_equal = 0;
            printf("ch values differ\n");
        }
    }

    if (all_equal) {
        printf("Structures are equal\n");
    }

    return 0;
}
 

Explanation:

  1. We define a structure named MyStruct with three members: num1 (integer), num2 (float), and ch (character).
  2. In main, we create two instances of MyStruct (data1 and data2) and initialize them with the same values.
  3. We introduce a flag all_equal initialized to 1, assuming equality initially.
  4. We use memcmp to compare the entire memory representation of the two structures. If any byte differs, memcmp returns a non-zero value, indicating they are not equal. We set all_equal to 0 and print a message.
  5. If memcmp didn't find a difference yet (all_equal is still 1), we proceed to compare individual members using member access operators (data1.num1, etc.) and print specific messages if any member differs.
  6. Finally, based on the all_equal flag, we print a message indicating whether the structures are entirely equal or not.

This program demonstrates both approaches:

  • Comparing the entire structure using memcmp (suitable for basic data types)
  • Comparing individual members for more detailed information

Note:

  • memcmp might not be suitable for structures containing pointers or complex data types due to potential padding by the compiler. In such cases, individual member comparison is necessary.

 

Q2.Write a program to add two times using structure.


#include <stdio.h>

struct Time {
    int hours;
    int minutes;
    int seconds;
};

struct Time add_times(struct Time time1, struct Time time2) {
    struct Time result;

    // Add hours, handling overflow
    result.hours = time1.hours + time2.hours;
    if (result.hours >= 24) {
        result.hours %= 24;
    }

    // Add minutes, handling overflow
    result.minutes = time1.minutes + time2.minutes;
    if (result.minutes >= 60) {
        result.minutes %= 60;
        result.hours++;  // Carry over to hours if minutes overflow
    }

    // Add seconds, handling overflow
    result.seconds = time1.seconds + time2.seconds;
    if (result.seconds >= 60) {
        result.seconds %= 60;
        result.minutes++;  // Carry over to minutes if seconds overflow
    }

    return result;
}

int main() {
    struct Time time1, time2, total_time;

    // Get input for time1
    printf("Enter time1 (hours minutes seconds): ");
    scanf("%d %d %d", &time1.hours, &time1.minutes, &time1.seconds);

    // Get input for time2
    printf("Enter time2 (hours minutes seconds): ");
    scanf("%d %d %d", &time2.hours, &time2.minutes, &time2.seconds);

    // Add the times using the add_times function
    total_time = add_times(time1, time2);

    // Display the result
    printf("Sum of times: %02d:%02d:%02d\n", total_time.hours, total_time.minutes, total_time.seconds);

    return 0;
}


Explanation:

  1. We define a structure named Time with three members: hours, minutes, and seconds to represent time.
  2. We define a function add_times that takes two Time structures as arguments and returns a new Time structure containing the sum.
  3. Inside add_times:
    • We create a new Time structure named result to store the sum.
    • We add the hours, minutes, and seconds of time1 and time2 and store them in result.
    • We handle overflow for each unit (hours, minutes, seconds):
      • If hours exceed 23, we take the modulo by 24 to keep the result within the 0-23 hour range.
      • If minutes exceed 59, we take the modulo by 60 and increment hours by 1 to carry over the overflow.
      • If seconds exceed 59, we take the modulo by 60 and increment minutes by 1 to carry over the overflow.
  4. In main:
    • We declare three Time structures: time1, time2, and total_time.
    • We prompt the user to enter the values for time1 and time2.
    • We call the add_times function to calculate the sum and store it in total_time.
    • We use %02d format specifier in printf to ensure two-digit display for hours, minutes, and seconds.

This program effectively adds two times represented by structures, handling potential overflows for each unit.


Q3.Write a program to create a pointer variable and display the address and value using pointer variable.

#include <stdio.h>

int main() {
    int num = 10;  // Create an integer variable
    int *ptr;      // Declare a pointer variable of type 'int'

    // Assign the address of 'num' to the pointer 'ptr'
    ptr = &num;

    printf("Address of num: %p\n", ptr);  // Print the address stored in ptr
    printf("Value stored at address pointed by ptr: %d\n", *ptr);  // Print the value using dereferencing

    return 0;
}
 

Explanation:

  1. We include the stdio.h header for standard input/output functions like printf.
  2. In main, we declare an integer variable num and initialize it with the value 10.
  3. We declare a pointer variable ptr of type int. A pointer variable stores memory addresses, in this case, addresses of integer variables.
  4. We use the address-of operator (&) to get the memory address of num and assign it to ptr. Now, ptr points to the memory location where num is stored.
  5. We use the printf function to display the address stored in ptr using the %p format specifier, which is used for pointers.
  6. To access the value stored at the memory location pointed to by ptr, we use the dereference operator (*). So, *ptr is equivalent to the value of num. We use printf again to display this value using the %d format specifier for integers.

This program demonstrates the basic concepts of pointers in C:

  • Declaring a pointer variable
  • Assigning an address to a pointer
  • Dereferencing a pointer to access the value it points to

 



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.