Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C Program Create a Check structure. Include: Check number (should be an

ID: 3592615 • Letter: C

Question

Create a C Program

Create a Check structure. Include:

Check number (should be an integer).

Date (use type char[ ]).

To

Amount

Description

Add functions to:

Add a Check. Collect all information for a single check and return the check. Should not take any parameters. Gather all data in the function from the user. Call the function to display the values for the check before returning the Check.

Display the values for a single check. Format the amount for two decimal places.

Display the values for the entire checkbook, one check at a time.

In the main function:

Create a checkbook. It should be an array of at least size ten.

Repeat until the checkbook is full or the user quits. Give the user these options:

Add a check.

Display a single check. Ask the user for the check number to be displayed.

Display the checkbook.

Quit.

Explanation / Answer

C – Structure

PREV NEXT

C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.

DIFFERENCE BETWEEN C VARIABLE, C ARRAY AND C STRUCTURE:

C Structure:

C Variable:

C Array:

BELOW TABLE EXPLAINS FOLLOWING CONCEPTS IN C STRUCTURE.

EXAMPLE PROGRAM FOR C STRUCTURE:

This program is used to store and access “id, name and percentage” for one student. We can also store and access these data for many students using array of structures. You can check “C – Array of Structures” to know how to store and access these data for many students.

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <stdio.h>

#include <string.h>

struct student

{

           int id;

           char name[20];

           float percentage;

};

int main()

{

           struct student record = {0}; //Initializing to null

           record.id=1;

           strcpy(record.name, "Raju");

           record.percentage = 86.5;

           printf(" Id is: %d ", record.id);

           printf(" Name is: %s ", record.name);

           printf(" Percentage is: %f ", record.percentage);

           return 0;

}

OUTPUT:

Id is: 1
Name is: Raju
Percentage is: 86.500000

EXAMPLE PROGRAM – ANOTHER WAY OF DECLARING C STRUCTURE:

In this program, structure variable “record” is declared while declaring structure itself. In above structure example program, structure variable “struct student record” is declared inside main function which is after declaring structure.

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include <stdio.h>

#include <string.h>

struct student

{

            int id;

            char name[20];

            float percentage;

} record;

int main()

{

            record.id=1;

            strcpy(record.name, "Raju");

            record.percentage = 86.5;

            printf(" Id is: %d ", record.id);

            printf(" Name is: %s ", record.name);

            printf(" Percentage is: %f ", record.percentage);

            return 0;

}

OUTPUT:

Id is: 1
Name is: Raju
Percentage is: 86.500000

C STRUCTURE DECLARATION IN SEPARATE HEADER FILE:

In above structure programs, C structure is declared in main source file. Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.

HEADER FILE NAME – STRUCTURE.H

Before compiling and executing below C program, create a file named “structure.h” and declare the below structure.

struct student
{
int id;
char name[20];
float percentage;
} record;

MAIN FILE NAME – STRUCTURE.C:

In this program, above created header file is included in “structure.c” source file as #include “Structure.h”. So, the structure declared in “structure.h” file can be used in “structure.c” source file.

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// File name - structure.c

#include <stdio.h>

#include <string.h>

#include "structure.h"   /* header file where C structure is

                            declared */

int main()

{

   record.id=1;

   strcpy(record.name, "Raju");

   record.percentage = 86.5;

   printf(" Id is: %d ", record.id);

   printf(" Name is: %s ", record.name);

   printf(" Percentage is: %f ", record.percentage);

   return 0;

}

OUTPUT:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Syntax struct student
{
int a;
char b[10];
} Example a = 10;
b = “Hello”;