A (Simple) Generic C Program: Type Description
A (Simple) Generic C Program: Type Description
return-type function_1(parameters) {
declarations
Plan statements
• Basic data types: declaration, assignment }
• Arithmetic expressions
/* .
• Formatted input and output . (This is a C comment)
.
• Relational and logical expressions
*/
• Selection statements return-type function_n(parameters) {
• Iteration statements declarations
statements
• Compiling & linking
}
Introductory Course in Scientific Programming Lecture 2 (3) Introductory Course in Scientific Programming Lecture 2 (4)
Declaration of variables
Arithmetic expressions
Each program / function should begin with a
A variable is assigned a value using the
declaration of variables. For example
operator =.
main() {
C supports all basic arithmetic operators.
int i, j;
double f; • a = -b;
• a = b + c;
/* statements */
} • a = b - c; (equivalent to a=b+-c;)
Introductory Course in Scientific Programming Lecture 2 (7) Introductory Course in Scientific Programming Lecture 2 (8)
Compound assignment
When one is only interested in updating the
value of a variable it is possible to use Type conversion
compound assignment. Since all values in C have a specific type some
a += b; (-= , *= , /=) mathematical operations can introduce
is equivalent to ambiguities. For example, what type should
a = a + b; (- , * , /). the sum of an int and a double have?
i=i+1 can be conveniently written using the These questions are resolved by type
increment operator ++. i=i+1 can be written conversion. Implicit type conversion is done by
i++ (postfix) and ++i (prefix). the compiler when
• type of expressions on left and right hand
Prefix: sides differ (assignment)
i = 1; j = 1; k = j + ++i;
i ← 2 , k ← 3 (=1+2) • operands in expression of different type
i updated before evaluation
Since a type can not be changed during
execution the first case is handled by converting
Postfix:
i = 1; j = 1; k = j + i++; the value of the right hand side to the type on
k ← 2 (=1+1) , i ← 2 the left hand side before assignment.
i updated after evaluation
Decrement operator : --
Introductory Course in Scientific Programming Lecture 2 (11) Introductory Course in Scientific Programming Lecture 2 (12)
work.
specification %lf must be used, %f will not
Note: To read a double the format
memory.
&x returns the address of the variable x in
address operator &.
Addresses to variables can be obtained by the
stored.
memory locations where the values are to be
The ellipsis should be replaced by addresses to
fprintf.
The format string is basically the same as for
usually keyboard).
most common is stdin (standard input,
Input in C is read from input streams. The
Input/output – an example (cont.)
Formatted input
Enter radius of circle: 1
c2m2-20>a.out
Enter radius of circle: 4.5
Area = 6.358500e+01 , Circumference = 28.26000
c2m2-20>a.out
[email protected]
Marco Kupiainen
Enter radius of circle: 2.3e1
Lecture 2
Area = 1.661060e+03 , Circumference = 144.44000
(13)
Marco Kupiainen
[email protected]
NADA
Input/output – an example
#include <stdio.h>
main() {
const double pi = 3.14;
double radius;
Marco Kupiainen
[email protected]
NADA