Sie sind auf Seite 1von 6

Overview of C Program Structure

A C program has five (5) sections:


1. Introductory Comment
2. Header Files
3. Constant Definitions
4. Function Definitions
5. Main Program
The Basic Program
Structure Note: Discussion of Function Definitions will be reserved till
Chapter 6. ☺

/ccsrac 2

/* Filename: grade.c /* Getting the 3 quiz scores */


Programmed by: Rafael A. Cabredo scanf(“%f”, &fQ1);
Last Modified: May 28, 2003 scanf(“%f”, &fQ2);
Version: 1.0 scanf(“%f”, &fQ3);
This program computes the final grade for COMPRO1
students /* Getting the Final exam score, Machine Project
and grade for SW, Assign, Recitation, & Cases */
*/
scanf(“%f”, &fFE);
scanf(“%f”, &fMP);
#include <stdio.h> scanf(“%f”, &fSARC);
#define GREETING “Mabuhay! Let’s compute the grade.”
/* Computation of final grade */
/* If you have function definitions place them here */ fFG = 0.40*(fQ1+fQ2+fQ3)/3 + 0.30*FE + 0.15*fMP
+ 0.15*fSARC;
/* **** MAIN PROGRAM **** */
/* Displaying the result */
main()
printf(“FG = %.2f\n”, fFG);
{ }
float fQ1, fQ2, fQ3, fFE; /* Variable Declarations */ /* **** END OF PROGRAM **** */
float fMP, fSARC, fFG;
/ccsrac 3 /ccsrac 4

Program Comments Header Files

Comments are text added in the source code which Header files are statements that tell the compiler what
serves as internal documentation. additional files to process before processing the
actual program.
Enclose your comments with these: /* */
Format: #include < <filename> >

/* This formula computes the interest */


Example: #include <stdio.h>
#include <stdlib.h>

/ccsrac 5 /ccsrac 6
Constant Declarations Main Program

Constants are defined for values which will not change This is the main section of the entire program. It is the
throughout the program. first part of the program to be executed.

Format: #define <constant identifier> <literal> Format:


main()
Example: #define SCHOOL “De La Salle University” { /* Variable Declarations */
#define PI 3.1416 /* Statements */
#define MAX_STUD 45 }

/ccsrac 7 /ccsrac 8

Variable Declarations A Closer Look at Variables


A variable has:
Variable declarations tell the C compiler what type of * a symbolic name
data will be stored in each variable and how that data * an associated physical memory space
will be represented in memory.
* a data type
* a value
Format: <data type> <variable list>; * a scope 2006 123 nNum

* a lifetime 2008
fValue
200A
Example: int nNum1, nMaxVal;
float fValue, fAverage; Example: 200C

double dCapital, dProfit; int nNum; 200E


char cFirstInit, cLast; float fValue;

/ccsrac 9 /ccsrac 10

Different C Data Types


A data type specifies:
– the kind of values that can be assumed by a
variable of that type
– the range of values that can be assumed by a
variable of that type
– the amount of memory (in bytes) needed by a
variable to store a value of that type
Different C
Data Type Range Statements
int -2147483648 to +2147483647

long -2147483648 to +2147483647

float 3.4e-38 to 3.4e38

double 1.7e-308 to 1.7e308

/ccsrac 11
Assignment Statement Assignment Statement
An assignment statement stores a value Variables can be given an initial value
or a computational result in a variable. upon declaring them. This is often referred to as
initialization.

Format: <variable> = <expression>;


Format: <data type> <variable> = <expression>;
Example:
x = 1; /* x is asssigned a numeric literal
Read as x “gets 1” OR Example:
“x takes the value of one” */
int nTotalSec = 11;
fVal = PI; /* fValue is asssigned the value of a
float fWeight = MY_WEIGHT; /* Assuming MY_WEIGHT
constant (assuming PI is defined)*/
is a constant */
nQ1 = nQ2; /* cMid gets the value of a variable */
char cChoice = ‘Y’;

/ccsrac 13 /ccsrac 14

Assignment Statement
Other Examples:
int x, y, z = 1; /* only z will get the value 1
x and y will have garbage values */

int x, y, z;
x = y = z = 1; /* x, y, and z are assigned a value
of 1 (right to left assignment) */
To assign a value to a variable, use =
fAveQ = (fQ1 + fQ2)/2; /* result of expression will
be stored to fAveQ */

To check if two values are the same, nValue = 2.3 * 2; /* nValue stores 4 */
use (equality operator) ==
nCounter = nCounter + 1; /* Adds 1 to current value
of nCounter and stores
it back to itself */

/ccsrac 15 /ccsrac 16

Assignment Statement Shortcuts Assignment Statement Shortcuts


Increment/Decrement Operators: Examples:

i = i + 1; i++;
i = i + 2; i += 2;
j = j – 1; j--;

x = x – 15; x -= 15;
Other Assignment Operators:

fAmt = fAmt * (1 + fRate); fAmt *= 1 + fRate;


Whenever the following format is seen,

fShare = fShare/4; fShare /= 4;


<variable> = <variable> <operator> <expr>;

j = j % 2; j %= 2;
it can be written as follows:

<variable> <operator>= <expr>;

/ccsrac 17 /ccsrac 18
Input and Output Statements The printf Statement
Anything between [ ]
The following functions can be used: is optional
for input: scanf()
for output: printf() Format:
printf( <format string> [, <print list>]);
Note:
1. The f in scanf and printf stand for “formatted.” Example: %s and %c are
2. These functions are in the standard input/output (stdio) printf(“Hello World!”); Conversion Characters
library. Therefore, the stdio.h header file must be included if
they will be used. printf(“Hello %s”, “World!”);
pritnf(“Hello %c%c%c%c%c!”, ‘W’,‘o’,‘r’,‘l’,‘d’);
#include <stdio.h>

Screen Output: Hello World!

/ccsrac 19 /ccsrac 20

The printf Statement The printf Statement


Other Conversion Characters:
Example:
Conversion How the corresponding argument is printed int nAge = 18;
Character float fWeight = 43.23;
c As a character
d, i As a decimal integer printf (“I am %d years old.\n”, nAge);
f As a real number; example: 1.230000 printf(“I weigh %f kilograms.”, fWeight);

s As a string
Screen Output:
I am 18 years old.
% The % character is printed instead I weigh 43.230000 kilograms.

Not nice to look at? Read


about the other ways of
formatting (pp.31-33).

/ccsrac 21 /ccsrac 22

The printf Statement The scanf Statement


Try the following:
#include <stdio.h> Screen Output:
main() Format:
65
{ 66 scanf( <format string>, <input list> );
int nNum = 65; B
printf(“%d\n”, nNum++);
printf(“%d\n”, nNum); Example: Store in “Address of” nAge
printf(“%c”, nNum); scanf(“%d”, &nAge); Screen Output:
} _8
1

2006 18 nAge

2008
200A

/ccsrac 23 /ccsrac 24
The scanf Statement Common Programming Errors
Other examples: main()
main()
{ {
char a, b, c; int nSection;
int n;
float x;
scanf(“What is your section? %d”, nSection);
scanf(“%c%c%c%d%f”, &a, &b, &c, &n, &x);
}
Printf(“Output is: %d %d”, nSection*3);
Commonly Used Conversion Characters:
Conversion What characters in the input stream are
Character converted to
c To a character
d To a decimal integer
f, lf To a real number (double), float
/ccsrac 25 /ccsrac 26

Exercise Exercises
Self Evaluation Exercise, Page 37, #1

1. Write a program that inputs a 3-digit 2. Create a program that converts a


number and then display each of the digits. Fahrenheit measure to a Celsius measure
(C = 5/9 X (F-32))

/ccsrac 27 /ccsrac 28

Exercises Exercises
3. Create a program that will get as input 4. Write a program that inputs two real
from the user the base and the height of a triangle. numbers and then exchange their values.
Display the area of the triangle.

/ccsrac 29 /ccsrac 30
Exercises Exercises
Chapter Exercises, #4 Chapter Exercises, #3

5. Write a program that displays the reverse


of an input 3-digit number while retaining the
value as a whole.

6. Philippine currency is broken down into


1000, 500, 100, 50, 20, 10 bills, and 5 and
1 peso coins. Write a program that asks for
an amount in pesos and outputs the least
number of bills and coins that will add up to
that amount.

/ccsrac 31 /ccsrac 32

Exercises

7. Workers at a particular company were


given a 15.5% salary increase. The
increase was effective two months ago.
Write a program that takes the employee’s
The Basic Program
old salary as input and output the amount Structure
of the retroactive pay (the increase that
was not given the last 2 months) as well as
the employee’s new salary.

/ccsrac 33

Das könnte Ihnen auch gefallen