Structure Assignment 8 — Initialize and Display the Coordinates of a Point
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Initialize and Display the Coordinates of a Point Using a Structure

📑 On this page:
  • Filling a point in one go
  • Two ways to set x and y
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How to fill a point struct at the same line where you declare it
  • How to name the fields (.x = 2.5f) so the order stops mattering
  • How to read a point from the user with scanf
  • How to print a point as (x, y) so it reads like a coordinate
  • Why a point struct is the starting point for distance, midpoint, and line calculations

Filling a point in one go

In the last assignment, we defined the point struct and filled each field with a separate line. Here we do the same thing, but tighter: one line for the whole point. struct point p = {2.5f, 3.1f}; is the one-liner. The first value goes into x, the second into y, simply because that's the order they appear in the struct. That's it.

There's also a second style where you name each field: {.x = 2.5f, .y = 3.1f}. This is a bit longer but you can write the fields in any order. If you later swap x and y in the struct definition, the named form still fills the right fields. The unnamed form would silently put 2.5 into y and 3.1 into x — which is a nasty bug if you're not paying attention.

Both styles are worth knowing. The short one is fine for a two-field struct like this. The named one is the habit you want to build for anything bigger. We'll show both in the program.

💡 In one line: struct point p = {2.5f, 3.1f}; fills in order, and {.x = 2.5f, .y = 3.1f} fills by name. Pick one and be consistent.

Left side shows a point struct with values 2.5 and 3.1 in the order x, y. Right side shows the same values but each labelled with .x and .y.

Two ways to set x and y

Here are the two forms side by side. Both create the same point. Pick whichever feels clearer — most people start with the short one and switch to names once they're editing code that others might touch.

/* Fill in order */
struct point a = {2.5f, 3.1f};

/* Fill by name (C99) */
struct point b = {.x = 2.5f, .y = 3.1f};
                                

Both end up with the same numbers. The order of values in the first form has to match the order of fields in the struct. The second form doesn't care — you can even swap the two: {.y = 3.1f, .x = 2.5f} works fine.

Style Example Good for Watch out for
In order {2.5f, 3.1f} Small structs, quick code Breaks if fields are reordered
By name {.x = 2.5f, .y = 3.1f} Real projects, longer structs Needs C99 or later (fine on modern compilers)

Complete C Program

Program: fill a point and print its coordinates

#include <stdio.h>

struct point {
    float x;
    float y;
};

int main(void) {
    struct point p = {2.5f, 3.1f};

    printf("Coordinates of P\n");
    printf("----------------\n");
    printf("x = %.2f\n", p.x);
    printf("y = %.2f\n", p.y);
    printf("As a pair: (%.2f, %.2f)\n", p.x, p.y);

    return 0;
}
                                    

And here's the same idea with names on the fields, plus a small variant that reads a point from the user:

Variant 1: named-field form

#include <stdio.h>

struct point {
    float x;
    float y;
};

int main(void) {
    struct point p = {.x = 2.5f, .y = 3.1f};

    printf("Point P = (%.2f, %.2f)\n", p.x, p.y);
    return 0;
}
                                    

Variant 2: read a point from the user

#include <stdio.h>

struct point {
    float x;
    float y;
};

int main(void) {
    struct point p;

    printf("Enter x and y: ");
    scanf("%f %f", &p.x, &p.y);

    printf("You entered P = (%.2f, %.2f)\n", p.x, p.y);
    return 0;
}
                                    

All three versions do the same thing: fill the point, print the point. The first is the main answer. The second shows the named-field form. The third shows the pattern you'd use in a real program — ask the user, then display what they entered. The &p.x and &p.y in the scanf are important: scanf needs the address of each field, just like it needs the address of any other variable.

Program Output

Coordinates of P
----------------
x = 2.50
y = 3.10
As a pair: (2.50, 3.10)
                                

And for the user-input version:

Enter x and y: 4.75 -1.2
You entered P = (4.75, -1.20)
                                

Try negative x and y to see the minus sign handled correctly. Try whole numbers — 3 4 — and notice that they print as 3.00 4.00, because %.2f always shows two decimal places. That's normal for coordinates; you want consistent formatting.

Step-by-Step Explanation

Short program, few moving parts. Here's what each line is doing.

1. The struct

Same struct point as the last assignment. Two floats, one for each coordinate. Nothing new here — the type was settled in assignment 7.

2. Filling the point

struct point p = {2.5f, 3.1f}; is the whole initialization. The compiler copies 2.5 into p.x and 3.1 into p.y, because that's the order the fields appear in the struct. The little f at the end of each number says "this is a float" — without it, C treats them as doubles and quietly converts them down, which usually works but is worth avoiding.

3. Reading from the user

In the third variant, scanf("%f %f", &p.x, &p.y); reads two floats from the user and stores them directly into the two members of the struct. The & is not optional — scanf needs a pointer to where the value should go. And &p.x is perfectly valid: it's the address of the x member of the struct pointed to by p... well, in this case p is not a pointer, it's a struct, so &p.x is the address of the x field inside p. The compiler handles that fine.

4. Printing the coordinates

The first two printf lines print x and y separately. The third prints them as a pair: (2.50, 3.10). Both are useful — the first is good when you want to see each value clearly, the second is the standard coordinate notation. For display purposes, the pair form is usually nicer because it matches how people write coordinates on paper.

5. Why "%.2f" and not "%f" or "%g"?

%f without a precision prints 6 decimal places by default: 2.500000. That's more than you need for most points, and it makes the output noisy. %.2f prints exactly 2 decimal places: 2.50. That's a good balance for 2D coordinates — enough precision to tell nearby points apart, not so much that the output is cluttered. If your points need more precision (say, physics simulations), use %.4f or %.6f.

6. Why this matters for the next assignment

The next assignment — distance between two points — builds directly on this. You'll have two struct point values, both filled and ready to go. The distance calculation subtracts their x's and y's, squares the differences, adds them, and takes the square root. Every step involves reaching into a point struct. So getting comfortable with this small pattern is exactly what you need before the distance program.

🎯 Remember this: fill a point with {2.5f, 3.1f} or {.x = 2.5f, .y = 3.1f}. Read from the user with scanf("%f %f", &p.x, &p.y). Print with %.2f for two decimals. The (x, y) pair form is the standard way to show a point.

Frequently Asked Questions

1. Can I fill the point after declaring it?

Yes — struct point p; p.x = 2.5f; p.y = 3.1f; does the same thing, just across three lines. The one-liner with braces is only valid at the point of declaration. If you declare first and fill later, you have to set each field by name. That's also how you'd fill a point read from the user, since the values aren't known at declaration time.

2. Why does %.2f print 2.50 instead of 2.5?

Because %.2f says "show exactly two decimal places". Even if the value is a whole number like 2.5, or 3.0, or 4.0, it will print with two decimal places: 2.50, 3.00, 4.00. That's usually what you want for coordinates — consistent formatting makes tables line up and comparisons easier to read.

3. Do I need the f after 2.5?

It's a good habit. Without the f, C treats 2.5 as a double, which is a larger type. Assigning it to a float field works but triggers an implicit conversion, and some compilers warn about it. Adding the f says "this is a float" and keeps everything consistent. It doesn't change the program's behavior in this case, but it prevents warnings and makes your intent clearer.

4. What if I enter a non-number in the scanf version?

scanf("%f %f", &p.x, &p.y) will fail to read the value if the user types letters. It won't crash, but it will leave the fields unchanged and the next input might be confused. For a teaching example, this is fine. For real code, you'd check scanf's return value: if (scanf("%f %f", &p.x, &p.y) != 2) { error }. That check is worth adding once you're comfortable with the basics.

5. Can I print a point with the printf on one line?

Yes — printf("(%.2f, %.2f)\n", p.x, p.y); does it. The two variants at the top both work; the difference is just formatting. If you have many points to print, the one-line form is more compact and reads better in a loop. If you're printing a single point with labels, the multi-line form is clearer.

6. Can I store these points in an array?

Yes — struct point pts[3] = {{2.5f, 3.1f}, {-1.0f, 4.5f}, {0.0f, 0.0f}}; gives you three points. That's exactly the pattern from the last assignment's array variant. The dots on pts[i].x and pts[i].y read like "the x of the i-th point" and work fine.

💡 Tip: After this, try making a small program that reads two points from the user and prints them as a pair on one line each. Then try computing the midpoint by hand — ((a.x + b.x) / 2, (a.y + b.y) / 2) — and print it. That's basically the next assignment, minus the square root.

📖 Related Tutorials

  • 7. Create a Structure to Represent a Point in 2D Space
  • 9. Find the Distance Between Two Points Using a Structure
  • 5. Initialize and Display the Details of a Book Using a Structure
  • Structure Initialization in C
  • Accessing Structure Members
  • Defining a Structure
  • Array of Structures
  • All 25 Structure Assignments

Previous Topic: -->> 6. Find the Total Price of a Collection of Books Using a Structure   ||   Next Topic: -->> 8. Initialize and Display the Coordinates of a Point Using a Structure


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.