Review: C program
The getc() function reads the next character from a file stream, and returns the character as an integer.
getchar == getc(stdin)
syntax: int getchar(void)
getchar() has its default file stream-stdin.
putc writes a character to the specified file stream.
syntax: int putc(int c, File * stream)
Here int c indicates that the output is a character saved in an integer variable C.
File stream specifies a file stream.
putchar syntax: int putchar(int c)
printf syntax:
int printf(const char* formating_string,…),it returns a negative value if an error occurs.
%d,%c are format specifiers.Please remember that you should use exactly the same number of expressions as the number of format specifies within the format string.
Hexadecimal, base 16 numbering system. A Hexadecimal number can be represented by four bits. The Hexadecimal number 0 through 9 map to decimal 0~9. upper case A~f means 10~15. Minimum Field Width, integer between the percent sign(%) and the letter in format specifies.
When the minimum field width is shorter than the width of the output, the latter is taken, and the output is still printed in full.
Align output: You can changed this and force outpyt to be left. To do so, you need to prefix the minimum field specifier with the minus sign(-).
Assignment Operator =
Relational Operator == (equal-to operator)
+=: do the addtion and the assignment later.
-: unary minus operator
++x: x = x + 1
pre-increment operator: the operator adds 1 to x first and then get the values of x.
–x: pre-decrement operator.
x++: x = x + 1
post-increment. For exampel, in the y = x++; statement, y is assigned the original value of x, not the one after x is increase by 1. In other words, the post-increment operator makes a copy of the original value of x and stores the copyu in a temporary location. Then x is increased by 1. However, instead of the modified value of x, the copy of the unmodified value of x is returned and assigned to y.
Relational Operations:
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than and Equal
<= Less than and Equal
Important, all relational expression product a result of either 0 or 1. If a relation expression returns 1 if the specified relationship holds. Otherwise, 0 is returned.
Be careful when you compare two values for equality. Because of the truncation, or rounding up, some relational expressions, which are algebraically true, may return 0 instead of 1.
For example, 1/2 + 1/2 == 1 will return 0, which means that the equal-to relationship does not hold. This is because the truncation of the integer division--that is,1/2 produces 0, not 0.5. For the same reason, 1.0/3/0 + 1.0/3/0 + 1.0/3.0 == 1.0 will return 0.
Cast operator: convert one data type to a different one by prefixing the case operator.
Syntax or general form: (data-type)x. Here x is a variable(or, expression) that contains the value of the current data type.
Looping, or iteration, is used in programing to perform the same set of statements over and over untill certain specified conditions are met.
The for statement first evaluates expression 1, which usually initialized one or more variables. In other words, expression 1 is only evaluated once when then for statement is first encounted. The second expression, expression 2, is the conditional part that is evaluated right after the evaluation of expression 1 and then is evaluated after each successful looping by the for statement. If expression 2 returns a nonzero value, the statements within the braces are executed, Usually, the nonzero value is 1, If expression 2 returns 0, the looping is stopped and the execution of the for statement is finished. The third expression in the for statement, expression 3, is not evaluated when the for statement is first encounted. However, expression 3 is evaluated after each looping and before the statement goes back to test expression 2 again.
The null statement:
For example for(i=1;i<9;i++); means
for(i=1;i<9;i++)
;
Compare the following two:
1. for(i=1;i<9;i++);
sum += i;
2. for(i=1;i<9;i++)
sum += i;
The result is not same.
The null statement is perfectly legal in C. You should pay attention to placing semicolons in your for statement.
Playing with an Infinite Loop:
for(;;){
/* statement block*/
}
There're no expressions in the three expression fields. The statements inside the statement block will be executed over and over without stopping.
While (1){
/* statement block*/
} --> Note, there’s NOT semicolon here.
do{
/* statement block*/
} while(expression); –> Note, there’s semicolon here.
The do-while statement ends with a semicolon which is an important distinction form the if and while statement. The for statement does not end with semicolon.
How do you know the size of a data type on your machine? sizeof(expression)
Here expression is the data type or variable whose size is measured. The value of the size is returned ,in writes of bytes.
size = sizeof(int)
sizeof(char)
sizeof(float)
sizeof(double)
&& Logical AND
|| Logical OR
! Logical negation
In C, if an expression or operator returns a nonzero value, the expression return TRUE.If and expression or operator return 0, the expression return FALSE. In other words, TRUE can be used to represent any nonzero value returned by an expression or operator;FALSE is equivalent to 0.