0% found this document useful (0 votes)
53 views4 pages

A (Simple) Generic C Program: Type Description

1) The document discusses an introductory lecture on scientific programming in C. It covers basic C programming concepts like data types, variables, arithmetic expressions, input/output, and selection and iteration statements. 2) The lecture introduces basic C data types like integers, floating point numbers, and characters. It also discusses declaring and initializing variables. 3) The lecture explains C statements and how they are terminated with semicolons. It discusses compound statements grouped in curly braces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

A (Simple) Generic C Program: Type Description

1) The document discusses an introductory lecture on scientific programming in C. It covers basic C programming concepts like data types, variables, arithmetic expressions, input/output, and selection and iteration statements. 2) The lecture introduces basic C data types like integers, floating point numbers, and characters. It also discusses declaring and initializing variables. 3) The lecture explains C statements and how they are terminated with semicolons. It discusses compound statements grouped in curly braces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introductory Course in Scientific Programming Lecture 2 (1) Introductory Course in Scientific Programming Lecture 2 (2)

A (simple) generic C program


preprocessor directives

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
}

int main(int argc, char **argv) {


declarations
statements
}

Marco Kupiainen Marco Kupiainen


[email protected] [email protected]
NADA NADA

Introductory Course in Scientific Programming Lecture 2 (3) Introductory Course in Scientific Programming Lecture 2 (4)

Statements Basic types


The major part of a C program consists of All variables in C must have a type, which
statements. A statement is a command which specifies what kind of data it will hold. A
is executed when the program runs. variable must be declared before use.
All statements must be terminated by a Some basic types:
semi-colon, e.g. Type Description
area = pi * radius * radius; int Integer values

A statement can span several lines, float Floating point numbers


double Floating point numbers
area = pi *
radius * radius; (higher accuracy, larger numbers)

Several statements can be collected in a char Character


compound statement which is treated as one
statement by the compiler. This is There are different variants of the basic types,
accomplished by braces, e.g. unsigned int which can only hold
{ non-negative integers, and long int which can
area = pi * radius * radius; hold larger integers.
circumference = 2 * pi * radius; In addition to the basic types C allows the user
} to specify his/her own data types.

Marco Kupiainen Marco Kupiainen


[email protected] [email protected]
NADA NADA
Introductory Course in Scientific Programming Lecture 2 (5) Introductory Course in Scientific Programming Lecture 2 (6)

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;)

This declaration defines two integers, i and j, • a = b * c;


and a floating point number f which can be • a = b / c;
used in the program.
Operator precedence as in mathematics. The
Note: C is case-sensitive. statement
A variable which is not supposed to change a = (1 + 2) * 3 - 4;
value can be declared as a constant, e.g. will result in a being assigned the value 5.

const double pi = 3.14; Additional mathematical functions declared in


math.h (trigonometric, exponential, power,
This improves efficiency and simplifies
. . . ).
debugging.

Marco Kupiainen Marco Kupiainen


[email protected] [email protected]
NADA NADA

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

Marco Kupiainen Marco Kupiainen


[email protected] [email protected]
NADA NADA
Introductory Course in Scientific Programming Lecture 2 (9) Introductory Course in Scientific Programming Lecture 2 (10)

Type conversion (cont.) Formatted output (stdio.h)


The second case is handled by “safe Output in C is written to output streams. The
conversion”. If one type can be expressed in two most common are stdout (for standard
the other (e.g. int special case of double) that output, usually screen) and stderr (for error
type is converted to obtain a more accurate messages).
result.
Output written to stderr is shown as soon as
There are some pathological cases. the statement is executed.
Sometimes it is advisable to do an explicit type Output written to stdout is buffered.
cast,
A programmer can define new streams.
(type ) expression
which converts the value of the expression to int fprintf(FILE *stream,
the given type. const char *format, ...);
Example: int printf(const char *format, ...);
int i , j; fprintf allows the programmer to specify the
double f; output stream. printf writes to stdout.
i = 3 ; j = 2;
The appearance of the output is specified by
f = i / j; /* f will hold 1.0 */
the format string.
f = (double) (i / j);/* f will hold 1.0 */
f = (double) i / j; /* f will hold 1.5 */ The ellipsis ,. . . , should be replaced by the
f = i / (double) j; /* f will hold 1.5 */ values to print.

Marco Kupiainen Marco Kupiainen


[email protected] [email protected]
NADA NADA

Introductory Course in Scientific Programming Lecture 2 (11) Introductory Course in Scientific Programming Lecture 2 (12)

Formatted output (cont.)


A simple example: Formatted output (cont.)
The basic specifiers can be modified. The
printf("Area = %f\n", pi*r*r);
specification %p.qX prints a value of type X
"Area = %f\n" is a format string instructing with precision q and a minimum field width p
the computer to write the string "Area = " (for tables).
followed by a floating point number (pi*r*r
The meaning of “precision” depends on X.
evaluated) and a linebreak to stdout.
Typically the number of decimals (X = f or
The format string must contain a conversion X = e) or significant digits (X = g or X = d).
specification for each value to print.
Escape sequences instructs the computer to
Some common conversion specifiers: print special characters. Some useful examples:
Specifier Displays Sequence Displays
%f Floating point numbers \n New line
%e – ” – , exponential form \t Tab
%g Combination of two above \" Double quote (”)
%d Integers \\ Backslash (\)
%c Characters \? Question mark (?)
%s Strings

Marco Kupiainen Marco Kupiainen


[email protected] [email protected]
NADA NADA
Introductory Course in Scientific Programming Lecture 2 (15)

Introductory Course in Scientific Programming


NADA

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

int fscanf(FILE *stream,


int scanf(const char *format, ...);
Execution of example program:
c2m2-20>a.out

Formatted input
Enter radius of circle: 1

const char *format, ...);


Area = 3.140000e+00 , Circumference = 6.28000

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

Introductory Course in Scientific Programming Lecture 2 (14)

Input/output – an example
#include <stdio.h>

main() {
const double pi = 3.14;
double radius;

printf("Enter radius of circle: ");


scanf("%lf", &radius);

printf("Area = %e , Circumference = %.5f\n\n",


pi*radius*radius, 2*pi*radius);
}

Marco Kupiainen
[email protected]
NADA

You might also like