Program 62 - CSU1128 - CSE 2026 - Shoolini University

Program to demonstrate the structures in C by values entered by the user

Explanation

The program demonstrates the use of structures in C by defining a structure for a point in 2D space and allowing the user to enter values for the point.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Structure Definition

The program defines a structure class_62_Point to represent a point in 2D space:

  • struct class_62_Point: Defines the structure with two integer fields x and y.
  • int x;: Represents the x-coordinate of the point.
  • int y;: Represents the y-coordinate of the point.

Variable Declaration

The main() function is defined, and a variable of the structure type is declared:

  • struct class_62_Point p;: Declares a variable p of type class_62_Point.

Program Description

The program prints a description message for the user to understand its functionality.

Assign Initial Values

The program assigns initial values to the fields of the structure:

  • p.x = 10;: Initializes the x-coordinate to 10.
  • p.y = 20;: Initializes the y-coordinate to 20.

User Input

The user is prompted to enter new values for the fields of the structure:

  • printf("Enter the value for x and y of the p struct: ");: Prompts the user to enter the values of x and y.
  • scanf("%d %d", &p.x, &p.y);: Reads the values entered by the user and stores them in p.x and p.y.

Output Result

The program prints the values of the fields of the structure:

  • printf("Point: (%d, %d)\n", p.x, p.y);: Prints the coordinates of the point.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
// Define a structure for a point in 2D space
struct class_62_Point
{
    int x;
    int y;
};
int main()
{
    printf("\n\n Program to demonstrate the structures in C by values entered by the user. \n\n");

    // Declare a variable of the Point structure type
    struct class_62_Point p;

    // Assign values to the fields of the structure
    p.x = 10;
    p.y = 20;
    printf("Enter the value for x and y of the p struct: ");
    scanf("%d %d", &p.x, &p.y);

    printf("Point: (%d, %d)\n", p.x, p.y);

    return 0;
}
                

Output

Program to demonstrate the structures in C by values entered by the user.

Enter the value for x and y of the p struct: 23 43
Point: (23, 43)