Skip to main content

How to Use Structs in C : complete guide

 

How to Use Structs in C: Get and Print Structure Variable Values

Structs are a powerful feature in C programming that allow you to group variables of different data types under a single name. This makes handling related data more efficient and organized. In this tutorial, we will learn how to create a structure, input its values, and display them with a practical example. This tutorial is beginner-friendly and uses easy-to-understand concepts.

C programming tutorial showing how to define a structure, take input, and print structure variable values



What is a Struct in C?

A struct (short for structure) is a user-defined data type that groups related variables under one name. These variables, known as members, can have different data types. Structs are widely used in C for organizing complex data and simplifying operations.

Key Features of Structs:

  • Enables grouping of variables with different types.
  • Uses a single memory block for all members.
  • Allows easy access to members via the struct's name or pointers.

Example: Program to Input and Print Struct Values

In this example, we define a struct called student with attributes for student number, name, marks, and percentage. We'll input the values for two student records and print them.

Code:

#include <stdio.h>

struct student {
    int sno, m;
    char sname[20];
    float per;
};

void main() {
    struct student s1, s2;

    printf("\nEnter sno, marks, and name for student 1: ");
    scanf("%d %d %s", &s1.sno, &s1.m, s1.sname);
    s1.per = s1.m / 3.0;

    printf("\nEnter sno, marks, and name for student 2: ");
    scanf("%d %d %s", &s2.sno, &s2.m, s2.sname);
    s2.per = s2.m / 3.0;

    printf("\nStudent 1: %d  %d  %s  %.2f", s1.sno, s1.m, s1.sname, s1.per);
    printf("\nStudent 2: %d  %d  %s  %.2f", s2.sno, s2.m, s2.sname, s2.per);
}


Pseudocode: Student Structure Program

Step 1: Start the program

Step 2: Define a structure student with:

  • Integer sno (student number)

  • Integer m (marks)

  • Character array sname

  • Float per (percentage)

Step 3: Declare two variables:

  • s1 and s2 of type student


Step 4: Input details of Student 1

  • Read sno, marks, and name

  • Calculate percentage

  • per = marks / 3.0

Step 5: Input details of Student 2

  • Read sno, marks, and name

  • Calculate percentage

  • per = marks / 3.0

Step 6: Display Student 1 details

  • Student Number

  • Marks

  • Name

  • Percentage


Step 7: Display Student 2 details

  • Student Number

  • Marks

  • Name

  • Percentage


Step 8: End the program


Conclusion

If you're looking for a detailed guide on C structures, including examples on how to retrieve and print structure variables, check out this informative article: C Structures Examples – Get Value of Structure Variable and Print It. This resource provides clear explanations with practical examples to help you understand how to work with structures in C programming efficiently. Whether you're a beginner or an experienced developer, this guide will enhance your knowledge of C structures and their usage.

About the Author

Read similar article 









Comments

Popular posts from this blog

How to Check if a Header Is Available in a Linux File – A Complete Guide

   How to Check if a Header Is Available in a Linux File – A Complete Guide When working in Linux environments, developers and system administrators often need to verify whether a specific header , field name, or column exists inside a file. This is especially common when dealing with CSV files , log files , configuration files , or any structured data. This guide explains multiple methods to check whether a header is present using simple Linux command-line tools. Why Check for a Header in Linux? Checking for a header is useful when you want to: Validate data files Ensure correct file formats Prevent script failures Perform conditional processing Linux provides multiple commands to check headers efficiently. 1. Using grep (Simple & Fast) grep -q "HeaderName" filename && echo "Header exists" || echo "Header not found" 2. Check Only the First Line head -n 1 filename | grep -q "HeaderName" 3. Using awk for Ex...

Building Multi-Agent Systems: Practical Tutorial for 2026

Building Multi-Agent Systems: Practical Tutorial for 2026 Introduction Multi-Agent Systems (MAS) are becoming one of the most powerful architectures in modern AI. In 2026, they are widely used in automation, trading bots, robotics, distributed AI, smart cities, and enterprise AI systems. Instead of relying on one large AI model, multi-agent systems use multiple intelligent agents that collaborate, compete, or coordinate to solve complex problems. This tutorial explains how to build multi-agent systems from scratch in a practical and beginner-friendly way. What is a Multi-Agent System? A Multi-Agent System (MAS) consists of multiple autonomous AI agents that: Perceive the environment Make decisions independently Communicate with other agents Work toward shared or individual goals Each agent has its own role, memory, and reasoning capability. Experts from IBM, Google Cloud, Gartner, Deloitte, and others are calling 2026 the "year of multi-agent systems" and "multi-agent o...

How to Improve Node.js Performance (100% Working Techniques)

How to Improve Node.js Performance (100% Working Techniques) Optimize Express.js for Speed, Security & SEO Node.js is known for its high performance, but improper configuration can significantly slow down your application. In this article, you’ll learn proven and production-ready techniques to optimize Node.js performance, improve server response time, and boost SEO rankings. Why Node.js Performance Matters for SEO Google ranking heavily depends on: Server Response Time (TTFB) Page Speed Security Headers Reduced Server Load A slow Node.js backend directly affects: SEO ranking User experience Crawl budget 1. Disable x-powered-by Header Default Behavior Express exposes the following header: X-Powered-By: Express This reveals your backend technology and slightly increases response size. Best Practice app.disable('x-powered-by'); Benefits Improves security Reduces header size Prevents fingerprinting Recommended by OWASP 2. Use Weak ETag for Better Performance Problem with Def...