|Next Page: Numerical Differentiation>
On this page, you will learn the fundamentals of programming using C Language. Specifically, it explains how to write basic code, compile it, and execute programs. Additionally, topics like basic arithmetic operations, for loops, and if statements, which are essential for numerical calculations, are covered.
First, let's look at the basic steps for executing a C language program on a computer. A C language program can be executed with the following steps:
.c
extension in C language.To learn the steps for executing a C language program, let's write and execute the famous "Hello World" program. The "Hello World" program is a typical program often used when learning programming for the first time, and is one of the most well-known examples. (Reference: Wikipedia)
.c
extension in C language.First, using your preferred editor (such as Emacs, Vim, Notepad, or Sakura Editor), write the following C language code and save it with the file name lesson0_1.c
.
#include
int main() {
printf("Hello world!!\n");
return 0;
}
The first line of the program, #include <stdio.h>
, includes the standard input/output (I/O) library of the C language, preparing for data input and output operations. Next, int main() {}
defines the main function main()
of the program. Within the curly braces {} of this function definition, the main program's processing content is described. In this program, the printf
function is used to output the string "Hello World" to the screen. The \n
at the end of the string inserts a newline after the string. At the end of the main function, the return
statement terminates the program and returns a value of 0. The return value will be explained in more detail later when learning about function definitions.
After preparing the above C code (lesson0_1.c
), compile this code to generate an executable file that the computer can run. Please execute the following command in the terminal:
gcc lesson0_1.c -o hello -lm
Here, the gcc
compiler is used, but other compilers can also be used. By executing the above command, the C source file (lesson0_1.c
) is compiled, and an executable file (hello
) is generated. The name of the executable file can be specified using the -o
option during compilation. Additionally, the -lm
option is used to link the library necessary for numerical computations, so include it if needed.
To run the executable file (hello
) prepared above, enter the following command in the terminal:
./hello
When executed, the string "Hello World!!" will be displayed on the terminal.
This output is generated by the line printf("Hello World!!\n");
on line 2 of the code in lesson0_1.c.
Let’s review the C programming process you’ve learned here. To execute a C program, the following three steps are required: 1) Write code in C with the .c
extension. 2) Compile the source code to generate an executable file. 3) Run the generated executable file to obtain the result. This is the general flow of C programming.
Most numerical computation methods and code are implemented using combinations of basic mathematical operations such as arithmetic (addition, subtraction, multiplication, and division). Here, we will learn about these basic mathematical operations (arithmetic and exponentiation). Let’s first take a look at the sample program below.
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, d, e, f, g;
a = 2.0;
b = 3.0;
printf("a=%.1f\n", a);
printf("b=%.1f\n", b);
c = a + b;
d = a - b;
e = a * b;
f = a / b;
g = pow(a, b);
printf("a+b =%.1f\n", c);
printf("a-b =%.1f\n", d);
printf("a*b =%.1f\n", e);
printf("a/b =%.1f\n", f);
printf("a**b=%.1f\n", g);
return 0;
}
In this program, calculations are performed on the variables a
and b
, including basic arithmetic operations (addition, subtraction, multiplication, and division) as well as exponentiation. As seen in Lesson 0-1, this program also starts with the statement #include <stdio.h>
, which includes the C standard input-output library. Additionally, the #include <math.h>
statement is used to include the mathematics library, preparing the program for numerical calculations. Like Lesson 0-1, the main function is defined using int main(){}
, and the code inside this function is executed sequentially.
At the beginning of the main function, the variables a
through g
are defined as double-precision floating-point numbers using double a, b, c, d, e, f, g;
.
The data type double
is known as double-precision floating-point, and it handles real numbers with approximately 16 digits of precision. In the declaration above, seven variables (a
through g
) are declared as double-precision floating-point numbers.
In addition, other data types such as integer int
and character string char
can also be used.
After the type declarations for the variables mentioned above, the following code continues:
a = 2.0;
b = 3.0;
In the C language, the equals sign (=
) does not represent equality but is used as an assignment operator.
In lines 10 and 11 of the program, the values assigned to the variables a
and b
are displayed on the screen:
printf("a=%.1f\n", a);
printf("b=%.1f\n", b);
Here, the printf
statement is used to output the variables. This printf
statement is also used in Lesson 0-1. Unlike the Hello World example program mentioned above, this case outputs floating-point variables. The %.1f
within the printf
function specifies that the number will be displayed with one digit after the decimal point.
Next, let’s look at the part of the code where the actual calculations are performed. Lines 13 to 17 of the above program perform arithmetic operations and exponentiation using the variables a
and b
.
c = a + b;
d = a - b;
e = a * b;
f = a / b;
g = pow(a, b);
Here, basic mathematical operations are performed on double-precision floating-point variables, and the results are assigned to double-precision floating-point variables c
, d
, e
, f
, and g
. In the C language, the +
operator represents addition, the -
operator represents subtraction, the *
operator represents multiplication, and the /
operator represents division. Additionally, the pow
function is used for exponentiation. After performing these operations, this example uses the printf
statement to output the results.
Next, let’s compile and execute the program. To compile, execute the following command in the terminal:
gcc lesson0_2.f90 -o lesson0_2 -lm
After compilation, run the executable file as follows:
./lesson0_2
The output of the calculations should appear on the screen as follows:
a=2.0
b=3.0
a+b =5.0
a-b =-1.0
a*b =6.0
a/b =0.7
a**b=8.0
Are these results as you expected? Also, in the above program, try assigning different values to the double-precision real variables a
and b
, and see how the results change.
In many numerical computation methods, it is often necessary to repeatedly execute specific procedures. In C language, the for
loop is provided as a feature to perform such iterative processing efficiently. In this section, we will learn how to use the for
loop.
To understand how to use the for
loop, let us consider an example program that calculates the sum of integers from \(1\) to \(10\). This problem can be solved using a simple formula:
\[ S=1+2+3+\cdots+n = \frac{1}{2}n(n+1). \tag{1} \]
However, let us create a program that numerically calculates this sum using a for
loop.
#include
int main() {
int i, n, sum;
n = 10;
sum = 0;
for (i = 1; i <= n; i++) {
sum += i;
}
printf("calculated sum = %d\n", sum);
printf("expected sum = %d\n", n * (n + 1) / 2);
return 0;
}
In the code above, we first include the standard library with #include <stdio.h>
and define the main function using int main()
. On line 4, three integer variables are declared: i
(for the loop counter), n
(upper limit), and sum
(to store the total).
Here, n
is set to 10
, and the program calculates the sum of numbers from 1 to 10.
Additionally, on line 7, the variable sum
, which stores the calculated total, is initialized with sum = 0;
. Variables can contain undefined values if they are only declared. Therefore, explicit assignment is necessary to initialize a variable.
Next, starting from line 8, we move to the for
loop block. This for
loop executes iterative processing within the {}
braces. As shown, the for
statement is written in the following format:
for (i = 1; i <= n; i++) {}
Here, i
is an integer variable used as the loop counter. In this line, i
is initialized to 1
, and as long as the condition i <= 10
is met, the operations inside the {}
braces are executed iteratively. After each iteration, 1
is added to i
, and the condition is re-evaluated. This process continues until the value of i
exceeds n
. During each iteration, the statement sum += i;
adds the value of i
to the integer variable sum
.
Now, let’s compile and run the above program (lesson0_3.c). You should get the following result:
calculated sum = 55
expected sum = 55
Here, calculated sum
represents the result of adding numbers from 1
to 10
as computed by the program, while expected sum
represents the value calculated using the formula (\(1\)). This result confirms that the calculation performed by the program accurately reproduces the value derived mathematically.
When performing numerical calculations, it is often necessary to execute different processes depending on given conditions. For this purpose, conditional branching can be implemented using the if
statement. To understand how the if
statement works, let’s look at the following C language code example.
#include
int main() {
double a, b;
a = 2.0;
b = 3.0;
printf("a=%.1f\n", a);
printf("b=%.1f\n", b);
if (a == b) {
printf("a==b\n");
} else if (a > b) {
printf("a>b\n");
} else {
printf("a<b\n");
}
return 0;
}
In the program above, the if (a == b) {}
statement on line 12 checks whether a
and b
are equal or not. If they are equal, the next printf("a==b\n");
is executed to display "a==b" on the screen. In this case, no further processing in the if
statement is performed, and the conditional branch ends.
If the condition of the first if (a == b)
statement is not met, the program moves to the next conditional statement, else if (a > b)
. Here, it checks whether a
is greater than b
. If so, the printf("a>b\n");
statement outputs "a>b" and terminates the if
statement. By using else if
, it becomes possible to sequentially evaluate multiple conditions.
So far, we have examined the processing when a condition is met, but handling cases where the condition is not satisfied is also important in programming. In the program example, an else
statement follows if (a == b)
and else if (a > b)
. This else
statement describes the process to execute when none of the above conditions are met. Here, it determines that a
is less than b
, outputs "a<b" on the screen, and ends the if
statement processing.
In an if
statement, the following conditional expressions can be used. Knowing these allows for handling many programming tasks:
a==b
: a
and b
are equal.a>b
: a
is greater than b
.a<b
: a
is less than b
.a>=b
: a
is greater than or equal to b
.a<=b
: a
is less than or equal to b
.a!=b
: a
and b
are not equal.In this page, we learned about the basics of C programming. In the next page, we will learn about numerical differentiation using C language.
|Next Page: Numerical Differentiation>