Advanced · C PROGRAMMING

Structures

What you will learn

Group related fields into one record.

Understand the concept

A struct defines a collection of named members that can have different types. The dot operator accesses a member of a struct object. A bounded character array stores the name within this record.

Read the example

The complete program appears below. Read it first, predict the output, then run it.

Expected output

Job 105: Laptop, 450.00

Practise

Add a status field and initialize it. Create a second record with different values.

Common mistake

A fixed character array has limited capacity. Copying arbitrary input into it without bounds checks is unsafe.

Lesson discussion

Log in to participate.

#include <stdio.h>
struct Repair {
    int id;
    char device[24];
    double amount;
};

int main(void) {
    struct Repair job = {105, "Laptop", 450.0};
    printf("Job %d: %s, %.2f\n", job.id, job.device, job.amount);
    return 0;
}
PRACTICE LAB

Read. Edit. Run.

C source · .c

Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.

Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft

Changes save automatically to your account.

Input / output example

Example input
No input required.
Expected output for the original program
Job 105: Laptop, 450.00
Output will appear here.

Log in to save your progress

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA