Variables and Arithmetic Expression

Notes from The C Programming Language

 

A decimal point in a constant indicates that it is floating point, however, so 5.0/9.0 i not truncated because it is the ratio of two floating-point values.

 

 

printf specification

  • %3.0f says that a floating-point number is to be printed at least three characters wide, with no decimal point and no fraction dgits.
  • %6.1f describes another number that is to be printed at least six characters wide, with 1 digit after the decimal point.

Width and precision may be omitted from a specification: %6f says that the number is to be at least six characters wide; %.2f specifies two characters after the decimal point, but the width is not constrained.

  • %o for octal
  • %x for hexadecimal
  • %c for character
  • %s for character string
  • %% for % itself

 

for statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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));
}

 

 

Character input and output

The standard library provides getchar() and putchar() for reading and writing one character at a time. getchar() reads the next input character from a text stream and returns that as its value:

1
c = getchar();

The function putchar prints a character each time:

1
putchar(c);

prints the contents of the integer variable c as a character, usually on the screen.

 

File copy: a program that copies its input to its output one character at a time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
 
/* copy input to output; 1st version */
main()
{
    int c;
     
    c = getchar();
    while(c != EOF)
    {
        putchar(c);
        c = getchar();
    }
}

EOF is defined in <stdio.h>.

 

As an expression has a value, which is the left hand side after the assignment, the code can be concise:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
 
/* copy input to output; 2nd version */
main()
{
    int c;
     
    while((c = getchar()) != EOF)
        putchar(c);
}

 

 

The next program counts characters:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
/* count characters in input; 1st version */
main()
{
    long nc;
     
    nc = 0;
    while(getchar() != EOF)
        ++nc;
     
    printf("%ld\n", nc);
}

long integers are at least 32-bits.

 

It may be possible to cope with even bigger numbers by using a double(double precision float).

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
/* count characters in input; 2nd version */
main()
{
    double nc;
     
    for(nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%.0f\n", nc);
}

printf uses %f for both float and double; %.0f suppresses printing of the decimal point and the fraction part, which is zero.

 

Counts lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
/* count lines in input */
main()
{
    int c, nl;
     
    nl = 0;
    while((c = getchar()) != EOF)
    {
        if(c == '\n')
            ++n1;
        printf("%d\n", nl);
    }
}

 

Word counting with loose definition that a word is any sequence of characters that does not contain blank, tab or newline. This is a bare-bones version of the UNIX program wc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
 
#define IN  1   /* inside a word */
#define OUT 0   /* outside a word*/
 
/* count lines, words, and characters in input*/
main()
{
    int c, nl, nw, nc, state;
     
    state = OUT;
    nl = nw = nc = 0;
    while((c = getchar()) != EOF)
    {
        ++nc;
        if(c == '\n')
            ++nl;
        if(c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if(state == OUT)
        {
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl, nw, nc);
}

Every time the program encouters the first character of a word, it counts one more word.

 

Count the number of occurrences of each digit, of white space character(blank, tab, newline), and of all other characters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
 
/* count digits, white space, others */
main()
{
    int c, i, nwhite, nother;
    int ndigit[10];
     
    nwhite = nother = 0;
    for(i = 0; i < 10; ++i)
        ndigit[i] = 0;
     
    while((c = getchar()) != EOF)
    {
        if(c >= '0' && c <= '9')
            ++ndigit[c - '0'];
        else if(c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;
         
        printf("digits =");
        for(i = 0; i < 10; ++i)
            printf(" %d", ndigit[i]);
        printf(", white space = %d, other = %d\n", nwhite, nother);
    }
}

 

posted @   kid551  阅读(330)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示