Friday, September 23, 2011

C STRUCTURE









REVISED: Sunday, March 15, 2015




CONTENTS:
I.     C STRUCTURE INTRODUCTION
II.    C STRUCTURE VOCABULARY
III.   THE C STRUCTURE
IV.   C STRUCTURE EXAMPLE PROGRAM SOURCE CODE
V.    EXAMPLE PROGRAM OUTPUT
VI.   C STRUCTURE REFERENCES

YOU WILL LEARN: 
1. C structure vocabulary.
2. How to declare a C structure.
3. How memory for a C structure is allocated.
4. How to declare an instance of a C structure.

I. INTRODUCTION

Welcome to the “C Structure Tutorial.”



The C code shown in the tutorial were written using the Microsoft Visual C++ Express Edition Integrated Development Environment (IDE) and the “C99 subset.”

II. VOCABULARY

A. What is a structure?


A structure is a collection of logically related variables of different types. A structure groups logically related standard or user defined data types. Once declared, a structure defines a template for creating compound variable type objects which are instances of a structure.


Structures can be nested as elements in other structures. Structures can be used recursively. Structures can be used to define new data types.

B. What is a tag?


A structure type name is referred to as a tag. The tag appears right after the keyword struct.

If your program uses only one structure_variable, you do not need the structure_type_name.

C. What do you call variables that make up a structure?


Members, elements, or fields are the names of variables that make up the structure.

III. THE C STRUCTURE

A. How do you declare a structure?


The syntax for simultaneously declaring a structure template and creating compound variable type objects is as follows:

struct structure_type_name{
//keyword
struct and tag, user defined type
type member_name;
//members, elements, or fields
type member_name;
type member_name;
.
.
.
}structure_variables;
//compound variable type objects


B. How do you allocate memory for a structure?


Memory is automatically allocated for all the members by the compiler when a structure variable is declared.

C. How do you define an instance of a structure?


After the structure template has been defined, an instance of a physical compound variable type object, a structure_variable can be declared using the following syntax:

struct structure_type_name structure_variable;

IV. EXAMPLE PROGRAM

//**********************************
//  C STRUCTURE TUTORIAL
//**********************************

#include<conio.h>
//contains function prototypes
#include<ctype.h>
//character handling
#include<stdio.h>
//input/output
#include<stdlib.h>
//general utilities
#include<string.h>
//string handling

struct item
//structure declaration
{
    int item_stock_number;
//member, element, or field

    char *item_name;
//member, element, or field

    char *item_description;
//member, element, or field

    float item_cost;
//member, element, or field

    float item_sales_price;
//member, element, or field

    int item_inventory_quantity;
//member, element, or field

}item1, item2, item3;
//compound variable type objects

void f_item1(void);
//function prototype

void f_item2(void);
//function prototype

void f_item3(void);
//function prototype

void p_item1(void);
//function prototype

void p_item2(void);
//function prototype

void p_item3(void);
//function prototype

int main(void)
{
    f_item1();
//function call

    f_item2();
//function call

    f_item3();
//function call

    p_item1();
//function call

    p_item2();
//function call

    p_item3();
//function call

    char ch;
    ch = getch();
//keeps screen from closing
//until a key is pressed
         
    return 0;
}

void f_item1(void)
//function definition
{
    item1.item_stock_number = 128;

    item1.item_name = "Diamond Cufflinks";

    item1.item_description = "Each 3 Carat Skull Shaped, 9K (375Au) yellow gold.";

    item1.item_cost = 250.00;

    item1.item_sales_price = 2500.00;

    item1.item_inventory_quantity = 10;
}

void f_item2(void)
//function definition
{
    item2.item_stock_number = 142;

    item2.item_name = "Ruby Cufflinks";

    item2.item_description = "Each 3 Carat Skull Shapped, 9K (375Au) yellow gold.";

    item2.item_cost = 500.00;

    item2.item_sales_price = 5000.00;

    item2.item_inventory_quantity = 20;
}

void f_item3(void)
//function definition
{
    item3.item_stock_number = 187;

    item3.item_name = "Sapphire Cufflinks";

    item3.item_description = "Each 3 Carat Skull Shapped, 9K (375Au) yellow gold.";

    item3.item_cost = 1000.00;

    item3.item_sales_price = 10000.00;

    item3.item_inventory_quantity = 30;
}

void p_item1(void)
//function definition
{
    printf("Stock Number: %i \n", item1.item_stock_number);

    printf("Name: %s \n", item1.item_name);

    printf("Description: %s \n", item1.item_description);

    printf("Cost: $%.2f \n", item1.item_cost);

    printf("Sales Price: $%.2f \n", item1.item_sales_price);

    printf("Inventory: %i \n\n\n", item1.item_inventory_quantity);
}

void p_item2(void)
//function definition
{
    printf("Stock Number: %i \n", item2.item_stock_number);

    printf("Name: %s \n", item2.item_name);

    printf("Description: %s \n", item2.item_description);

    printf("Cost: $%.2f \n", item2.item_cost);

    printf("Sales Price: $%.2f \n", item2.item_sales_price);

    printf("Inventory: %i \n\n\n", item2.item_inventory_quantity);
}

void p_item3(void)
//function definition
{
    printf("Stock Number: %i \n", item3.item_stock_number);

    printf("Name: %s \n", item3.item_name);

    printf("Description: %s \n", item3.item_description);

    printf("Cost: $%.2f \n", item3.item_cost);

    printf("Sales Price: $%.2f \n", item3.item_sales_price);

    printf("Inventory: %i \n\n\n", item3.item_inventory_quantity);

}

V. EXAMPLE PROGRAM OUTPUT

Stock Number: 128
Name: Diamond Cufflinks
Description: Each 3 Carat Skull Shaped, 9K (375Au) yellow gold.
Cost: $250.00
Sales Price: $2500.00
Inventory: 10

Stock Number: 142
Name: Ruby Cufflinks Description: Each 3 Carat Skull Shapped, 9K (375Au) yellow gold.
Cost: $500.00
Sales Price: $5000.00
Inventory: 20

Stock Number: 187
Name: Sapphire Cufflinks
Description: Each 3 Carat Skull Shapped, 9K (375Au) yellow gold. Cost: $1000.00
Sales Price: $10000.00
Inventory: 30


VI. REFERENCES

The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).

C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).

YOU HAVE LEARNED: 
1. C structure vocabulary.
2. How to declare a structure.
3. How memory for a structure is allocated.
4. How to declare an instance of a structure.

Elcric Otto Circle






-->





-->





-->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






No comments:

Post a Comment