03 - Basic Program Structure

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 03 - Basic Program Structure as PDF for free.

More details

  • Words: 1,370
  • Pages: 6
Overview of C Program Structure A C program has five (5) sections: 1. 2. 3. 4. 5.

The Basic Program Structure

Introductory Comment Header Files Constant Definitions Function Definitions Main Program

Note: Discussion of Function Definitions will be reserved till Chapter 6. ☺

/ccsrac

/* Filename: Programmed by: Last Modified: Version: This program students */

/* Getting the 3 quiz scores */ scanf(“%f”, &fQ1); scanf(“%f”, &fQ2); scanf(“%f”, &fQ3);

grade.c Rafael A. Cabredo May 28, 2003 1.0 computes the final grade for COMPRO1

/* Getting the Final exam score, Machine Project and grade for SW, Assign, Recitation, & Cases */ scanf(“%f”, &fFE); scanf(“%f”, &fMP); scanf(“%f”, &fSARC);

#include <stdio.h> #define GREETING “Mabuhay! Let’s compute the grade.”

/* Computation of final grade */ fFG = 0.40*(fQ1+fQ2+fQ3)/3 + 0.30*FE + 0.15*fMP + 0.15*fSARC;

/* If you have function definitions place them here */ /* **** MAIN PROGRAM **** */ main() { float fQ1, fQ2, fQ3, fFE; float fMP, fSARC, fFG;

/* Displaying the result */ printf(“FG = %.2f\n”, fFG); } /* **** END OF PROGRAM **** */

/* Variable Declarations */

/ccsrac

3

/ccsrac

Program Comments

Header Files

Comments are text added in the source code which serves as internal documentation.

Header files are statements that tell the compiler what additional files to process before processing the actual program.

Enclose your comments with these: /*

2

4

*/

Format: #include < > /* This formula computes the interest */

Example:

/ccsrac

5

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

/ccsrac

6

Constant Declarations

Main Program

Constants are defined for values which will not change throughout the program.

This is the main section of the entire program. It is the first part of the program to be executed.

Format:

#define

Example: #define #define #define

SCHOOL PI MAX_STUD

Format: main() { /* Variable Declarations */ /* Statements */ }

“De La Salle University” 3.1416 45

/ccsrac

7

Variable Declarations

A variable has: * a symbolic name * an associated physical memory space * a data type * a value 2006 nNum 123 * a scope 2008 * a lifetime fValue

;

Example: int float double char

200A

nNum1, nMaxVal; fValue, fAverage; dCapital, dProfit; cFirstInit, cLast; /ccsrac

8

A Closer Look at Variables

Variable declarations tell the C compiler what type of data will be stored in each variable and how that data will be represented in memory.

Format:

/ccsrac

200C

Example: int nNum; float fValue; 9

200E

/ccsrac

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 Data Type

Range

int

-2147483648 to +2147483647

long

-2147483648 to +2147483647

float

3.4e-38 to 3.4e38

double

1.7e-308 to 1.7e308 /ccsrac

Different C Statements

11

10

Assignment Statement

Assignment Statement

An assignment statement stores a value or a computational result in a variable.

Format:

Variables can be given an initial value upon declaring them. This is often referred to as initialization.

= <expression>;

Format:

= <expression>;

Example: x = 1;

fVal = PI; nQ1 = nQ2;

/* x is asssigned a numeric literal Read as x “gets 1” OR “x takes the value of one” */ /* fValue is asssigned the value of a constant (assuming PI is defined)*/ /* cMid gets the value of a variable */

/ccsrac

Example: int nTotalSec = 11; float fWeight = MY_WEIGHT; char

/* Assuming MY_WEIGHT is a constant */

cChoice = ‘Y’;

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;

To check if two values are the same, use (equality operator) ==

/ccsrac

nValue = 2.3 * 2;

/* nValue stores 4 */

nCounter = nCounter + 1;

15

/* result of expression will be stored to fAveQ */

/* Adds 1 to current value of nCounter and stores it back to itself */ /ccsrac

16

Assignment Statement Shortcuts

Assignment Statement Shortcuts

Increment/Decrement Operators:

Examples:

i = i + 1; j = j – 1;

i++; j--;

Other Assignment Operators: Whenever the following format is seen, = <expr>;

it can be written as follows:

i = i + 2;

i += 2;

x = x – 15;

x -= 15;

fAmt = fAmt * (1 + fRate);

fAmt *= 1 + fRate;

fShare = fShare/4;

fShare /= 4;

j = j % 2;

j %= 2;

= <expr>;

/ccsrac

17

/ccsrac

18

Input and Output Statements

The printf Statement Anything between [ ] is optional

The following functions can be used: for input: scanf() for output: printf()

Format: printf( [, <print list>]);

Note: 1. The f in scanf and printf stand for “formatted.”

Example:

2. These functions are in the standard input/output (stdio) library. Therefore, the stdio.h header file must be included if they will be used.

%s and %c are

Conversion Characters printf(“Hello World!”); printf(“Hello %s”, “World!”); pritnf(“Hello %c%c%c%c%c!”, ‘W’,‘o’,‘r’,‘l’,‘d’);

#include <stdio.h>

Screen Output:

/ccsrac

19

The printf Statement

c d, i f

/ccsrac

20

The printf Statement

Other Conversion Characters: Conversion Character

Hello World!

Example: int nAge = 18; float fWeight = 43.23;

How the corresponding argument is printed As a character As a decimal integer

printf (“I am %d years old.\n”, nAge); printf(“I weigh %f kilograms.”, fWeight);

As a real number; example: 1.230000

s

As a string

%

The % character is printed instead

Screen Output:

I am 18 years old. I weigh 43.230000 kilograms.

Not nice to look at? Read about the other ways of formatting (pp.31-33). /ccsrac

21

The printf Statement

/ccsrac

22

The scanf Statement

Try the following: #include <stdio.h> main() { int nNum = 65; printf(“%d\n”, nNum++); printf(“%d\n”, nNum); printf(“%c”, nNum); }

Screen Output:

Format:

65 66 B

scanf( , ); Store in “Address of” nAge

Example: 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() { int nSection;

main() { char a, b, c; 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 Character

What characters in the input stream are converted to

c

To a character

d

To a decimal integer

f, lf

To a real number (double), float /ccsrac

25

Exercise

/ccsrac

26

Exercises

Self Evaluation Exercise, Page 37, #1

1. Write a program that inputs a 3-digit number and then display each of the digits.

/ccsrac

2. Create a program that converts a Fahrenheit measure to a Celsius measure (C = 5/9 X (F-32))

27

/ccsrac

Exercises

Exercises

3. Create a program that will get as input from the user the base and the height of a triangle. Display the area of the triangle.

4. Write a program that inputs two real numbers and then exchange their values.

/ccsrac

29

/ccsrac

28

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

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 old salary as input and output the amount of the retroactive pay (the increase that was not given the last 2 months) as well as the employee’s new salary. /ccsrac

The Basic Program Structure

33

32

Related Documents