码迷,mamicode.com
首页 > 其他好文 > 详细

Variables and Arithmetic Expression

时间:2015-02-02 21:26:20      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

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:

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

c = getchar();

The function putchar prints a character each time:

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:

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

#include <stdio.h>

/* copy input to output; 2nd version */
main()
{
	int c;
	
	while((c = getchar()) != EOF)
		putchar(c);
}

 

Variables and Arithmetic Expression

标签:

原文地址:http://www.cnblogs.com/kid551/p/4268623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!