Beruflich Dokumente
Kultur Dokumente
/ccsrac 2
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> >
/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.
/ccsrac 7 /ccsrac 8
* a lifetime 2008
fValue
200A
Example: int nNum1, nMaxVal;
float fValue, fAverage; Example: 200C
/ccsrac 9 /ccsrac 10
/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.
/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
i = i + 1; i++;
i = i + 2; i += 2;
j = j – 1; j--;
x = x – 15; x -= 15;
Other Assignment Operators:
j = j % 2; j %= 2;
it can be written as follows:
/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>
/ccsrac 19 /ccsrac 20
s As a string
Screen Output:
I am 18 years old.
% The % character is printed instead I weigh 43.230000 kilograms.
/ccsrac 21 /ccsrac 22
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
/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
/ccsrac 31 /ccsrac 32
Exercises
/ccsrac 33