Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP C/HP-UX Reference Manual: HP-UX Systems > Chapter 2 Program Organization

Structuring a C Program

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

When you write a C program, you can put all of your source code into one file or spread it across many files. A typical C source file contains some or all of the following components:

  • Preprocessor directives

  • Variables

  • Functions

Example 2-1 Example

The following shows how a program can be organized:

/* preprocessor directives */[LINEBREAK]#include <stdio.h>[LINEBREAK]#define WEIGHTING_FACTOR 0.6[LINEBREAK]/* global typedef declaration */[LINEBREAK]typedef float THIRTY_TWO_BIT_REAL;[LINEBREAK]/* global variable declaration */[LINEBREAK]THIRTY_TWO_BIT_REAL correction_factor = 1.15;[LINEBREAK]/* prototype */[LINEBREAK]float average (float arg1, THIRTY_TWO_BIT_REAL arg2)[LINEBREAK]/* start of function body */[LINEBREAK]{[LINEBREAK] /* local variable declaration */[LINEBREAK] float mean;[LINEBREAK] /* assignment statement */[LINEBREAK] mean = (arg1 * WEIGHTING_FACTOR) +[LINEBREAK] (arg2 * (1.0 - WEIGHTING_FACTOR));[LINEBREAK]/* return statement */[LINEBREAK] return (mean * correction_factor);[LINEBREAK]/* end of function body */[LINEBREAK]}[LINEBREAK]

int main(void)[LINEBREAK]/* start of function body */[LINEBREAK]{[LINEBREAK]/* local variable declarations */ [LINEBREAK] float value1, value2, result;[LINEBREAK]/* statements */[LINEBREAK] printf("Enter two values -- ");[LINEBREAK] scanf("%f%f", &value1, &value2);[LINEBREAK] result = average(value1, value2);[LINEBREAK]/* continuation line */[LINEBREAK] printf("The weighted average using a correction \[LINEBREAK]factor of %4.2f is %5.2f\n", correction_factor, result);[LINEBREAK]/* end of function body */[LINEBREAK]}

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.