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.
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.
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