Въведение - 1
Chapter 1 - A Tutorial Introduction
1.1 Getting Started
1.2 Variables and Arithmetic Expressions
1.3 The for statement
1.4 Symbolic Constants
Hello World
#include<stdio.h>
main()
{
printf("hello,world\n");
}
>cc hello.c
>./a.out
Променливи и аритметични изрази
Температура по Целзии и Фаренхайт: C = (5/9) * (F
- 32)
1 -17
20 -6
40 4
60 15
80 26
100 37
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0;
/* lower limit of temperature scale */
upper = 300;
/* upper limit */
step = 20; /*
step size */
fahr = lower;
while (fahr <= upper) {
celsius = 5 *
(fahr-32) / 9;
printf("%d\t%d\n", fahr,
celsius);
fahr = fahr + step;
}
}
Втора версия на програмата.
0 -17.8
20 -6.7
40 4.4
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
main()
{
float fahr, celsius;
float lower, upper, step;
lower = 0;
/* lower limit of temperature scale */
upper = 300;
/* upper limit */
step = 20; /*
step size */
fahr = lower;
while (fahr <= upper) {
celsius = 5 *
(fahr-32) / 9;
printf("%3.0f %6.1f\n",
fahr, celsius);
fahr = fahr + step;
}
}
Оператор for
#include <stdio.h>
/* print Fahrenheit-Celsius table */
main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr =
fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
Знакови константи
#include <stdio.h>
#define LOWER 0 /*
lower limit of table */
#define UPPER 300 /*
upper limit */
#define STEP 20 /*
step size */
/* print Fahrenheit-Celsius table */
main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER;
fahr = fahr + STEP)
printf("%3d %6.1f\n",
fahr, (5.0/9.0)*(fahr-32));
}