存档

2007年8月 的存档

Specifiers and Function

2007年8月27日 没有评论

If they limit the scope of their variable to their pieces of code, they do not have to worry about conflicting with variales of the same name used by ohters in other parts of the program. In C, you can declare a variable and indicate its visiblity level by designing its scope. Thus, variables with local scope can only be accessed with the block in which they are declared.

1.Block scope
In this section, a block refers to any sets of statements enclosed in braces. A variable declared within block has block scope. Thus, the variable is active and accessiable from the declaration point to the end of the block. Sometimes, block scope is also called local scope. Usually, a variable with block scope is called a local variable.

2.Nested block size
If a variable declared in the outer block shares the same name with one of the variables in the inner block, the variable within the outer block is hidden by the one within the inner block for the scope of the inner block.

3.Program scope
A variable is said to have program scope when it is declared outside a function. Here variable with program scope and also called global variables, which are visiable among different files.

Since a global variable is visible among different source file of a program, using global variable increases your program’s complexity, which in turn makes your program hard to maintain or debug. It’s not recommended that you declare and are global variables unless it’s very necessary. For instance, you can declare a global variable whose value is used but never changed by several subroutines in your program(use #define directive to define constants that are used in many places in a program).

4.File scope
The program file that contains the source code.

Auto specifier:
A variable’s reserved space in the memory can be erased or relocated when the variable is out of its scope.

Static specifier:
Permanent duration, in other words, the memory storage allocated for the variabl is exited, the value of the variable is maintained outside the scope, and if execution even returns to the scope of the variable, the last stored in the variable is still there.

The register specifer:
In CPU, it’s much quicker to access a register than a memory location. Therebefore, storing variables in register may help to speed up your program. The C language provides you with the register specifer. You can apply this specifer to variables when you think it’s necessary to put the variable into the computer registers. However, the register specifier only gives the compiler a suggestion. In other words, a variable specifierd by the register keyword is not guaranteed to be stored in a register. The compiler can ignore the suggestion if there is no register available, if some other restrictions have to apply.

It is illegal to take the address of a variable that is declared with register specifier because the variable is intended to be stored in a register, not in memory.

Extern specifer:
How can a global variable declare in file A, for instance, be seen in file B? In other words, how does the compiler know the variable used in file B is actually the same variable in file A?
The answer is : Use the extern specifier provided by the C language to allude to a global variable defined elsewhere. In this case, we declare a global variable in file A, and then declare the variable again using the exterm specifier in file B.

To make your program portable across different computer platforms, you can apply the following rules in your program when you declare or allude to a global variables:
1. Ignore the extern specififer, but include an initialize when you declare a global variable.
2. Use extern specifier without an initializer.

Storage class modifiers, used to indicate to C compiler how variable many be accessed.
1.The const modifier:
The content of the variable can not be changed after it is initialized. eg:
char const *ptr_str = “hello”;
2.Volatile modifer, content can be changed without any explicite assignment statement. Ask compiler to turn off certain optimizations on a variables, can declare with valatile specifier.

A function can be declared to return any data type, except an array or function. The return statement used in a function definition returns a single value whose type should match the one declared in the function declaration. By default, the return type of a function is int, if no explicit data is specifier for the function.

syntax:
data-type-specifier function-name()
Here data-type-specifer indicates the data type that the function should return.

Function prototype(with ANSI standard, the number and types fo arguments passed to a function are allowed to be added into the function declaration). The number and types of an argument are called the function prototype.

The purpose of using a function prototype is to help the compiler check whether the data types of arguments passwd to a function match what function expects. The compiler issues an error message if the data types do not match.

“void” is used in the declaration to indicate to the compiler that no argument is needed by this function.

time() return calendar time(current data and time).
locatetime(), local time converted from calendar time
asctime: convert the date and time represented by the structure tm.

vg_start, vg_arg, vg_end.
vg_arg: use to get the next argument passed to the function.

分类: 科技 标签:

array and string

2007年8月24日 没有评论

In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all hte variables collectively in the format fo an array. Each variable, as an element fo the array, can be accessed either through the array reference or through a pointer that references the array.

An array consists of consecutive memory locations.
datatype Array_Name[Array-Size];
The total bytes of the array:
sizeof(data-type)* Array-size;
Or sizeof(Array_Name);

A character string is actually a character array ended with a null character .

Displaying Arrays of characters:
In C, a character string is defined as character array whose last element is the null character ‘’. For instances:
printf(“%s”,c);
1. Tell the printf() where to find the first element to start with.
2. Use the string format specifier %s in the printf() function.
Note, c contains the address of the first element in the array. That is, the start address of the array.
3. How the printf() function knows where the end of the character array is?
The answer: the last element in array is a ‘’ character. This is null character that marks the end of character array.

The Null character():
The null character ‘’ is treated as one character in C language; it is a specical character that makes the end of string. Therebefore, when function like printf() act on a character string, they process one character after another untill they encounter the null character.

The null character ‘’, which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement.

A series of characters enclosed in double quotes, is called a string constant.The C compiler can automatically add a null character at the end of a string constant to indicate the end of the string.

Initialize a character array str, with string constant:
char str[7] = “hello!”;
Here sizeof(str) = 6 + 1 = 7, ‘1’ is the position of null character.
char *ptr_str = “Hello world”;

Note, don’t specify the size of a character array as too small. Otherwise, it cannot string constant plus an extra null character. eg, the following declaration is considered illegal:
char str[4] = “test”;
That many C compilers will not issue a warning or an error that could eventually arise as result could be very difficult to debug. Therebefore, it’s your responsiblity to make sure you specify enough space for a string.

char *ptr_str;
ptr_str = “hello”;

These above two statments are equal to:
char *ptr_str = “hello”;

How long is a string, use strlen().
#include
size_t strlen(const char *s);
The return value from the function is the number fo bytes. Size_t is a data type defined in the string.h headfile. The size of data type depends on the paraticular computer systems.
strlen(array_name);
strlen(ptr_str);
Note, the result returned by strlen does not include null character.

Copying strings with strcpy()
If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array ot use strcpy().
strcpy() syntax:
# include
char *strcpy(char *dest, const char *size);
Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of the src if it is successful. eg:
char a[] = “hello”;
char *ptr_b = “world”;
strcpy(a, ptr_b)
Now, a[] is “world’.

The gets() function can be used to read characters from the standard input stream.
gets() syntax:
# include
char *gets(char *s);
Here the characters read from standard input stream are started in the character array identified by s. The gets() function stops reading, and appends a null character to the array, when a newline or end_of_file(EOF) is encounted. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.

puts(), writes characters to the standard output stream.
#include
init puts(const char *s);
s refers to the character array that contains a string. If puts() is successful, it returns 0. Otherwise, a nonzero is returned. The puts() function appends a newline character to replace the null character at the end of the character array.

Scanf syntax:
#include
int scanf(const char *format,…);
If concludes successfully, it returns the number of data items real from the stdin.If an error occurs, the scanf() function returns EOF.

Note, using %s causes the scanf to read character until a space, a newline, a tab, a form feend as encounted. Characters read by the scanf are stored into an array referenced by the corresponding argument.

A null character is automatically appended to the array after the reading.
scanf(“%d %d”);
scanf(“%s”,str);
scanf(“%d %d”,&x,&y);

getc() stops reading: new line or EOF.
scanf() stops reading: space, newline, a tab, a vertical tab or a form feed.

Here, &x,&y are memory location for the integer variables x,y.

In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all hte variables collectively in the format fo an array. Each variable, as an element fo the array, can be accessed either through the array reference or through a pointer that references the array.

An array consists of consecutive memory locations.
datatype Array_Name[Array-Size];
The total bytes of the array:
sizeof(data-type)* Array-size;
Or sizeof(Array_Name);

A character string is actually a character array ended with a null character .

Displaying Arrays of characters:
In C, a character string is defined as character array whose last element is the null character ‘’. For instances:
printf(“%s”,c);
1. Tell the printf() where to find the first element to start with.
2. Use the string format specifier %s in the printf() function.
Note, c contains the address of the first element in the array. That is, the start address of the array.
3. How the printf() function knows where the end of the character array is?
The answer: the last element in array is a ‘’ character. This is null character that marks the end of character array.

The Null character():
The null character ‘’ is treated as one character in C language; it is a specical character that makes the end of string. Therebefore, when function like printf() act on a character string, they process one character after another untill they encounter the null character.

The null character ‘’, which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement.

A series of characters enclosed in double quotes, is called a string constant.The C compiler can automatically add a null character at the end of a string constant to indicate the end of the string.

Initialize a character array str, with string constant:
char str[7] = “hello!”;
Here sizeof(str) = 6 + 1 = 7, ‘1’ is the position of null character.
char *ptr_str = “Hello world”;

Note, don’t specify the size of a character array as too small. Otherwise, it cannot string constant plus an extra null character. eg, the following declaration is considered illegal:
char str[4] = “test”;
That many C compilers will not issue a warning or an error that could eventually arise as result could be very difficult to debug. Therebefore, it’s your responsiblity to make sure you specify enough space for a string.

char *ptr_str;
ptr_str = “hello”;

These above two statments are equal to:
char *ptr_str = “hello”;

How long is a string, use strlen().
#include
size_t strlen(const char *s);
The return value from the function is the number fo bytes. Size_t is a data type defined in the string.h headfile. The size of data type depends on the paraticular computer systems.
strlen(array_name);
strlen(ptr_str);
Note, the result returned by strlen does not include null character.

Copying strings with strcpy()
If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array ot use strcpy().
strcpy() syntax:
# include
char *strcpy(char *dest, const char *size);
Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of the src if it is successful. eg:
char a[] = “hello”;
char *ptr_b = “world”;
strcpy(a, ptr_b)
Now, a[] is “world’.

The gets() function can be used to read characters from the standard input stream.
gets() syntax:
# include
char *gets(char *s);
Here the characters read from standard input stream are started in the character array identified by s. The gets() function stops reading, and appends a null character to the array, when a newline or end_of_file(EOF) is encounted. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.

puts(), writes characters to the standard output stream.
#include
init puts(const char *s);
s refers to the character array that contains a string. If puts() is successful, it returns 0. Otherwise, a nonzero is returned. The puts() function appends a newline character to replace the null character at the end of the character array.

Scanf syntax:
#include
int scanf(const char *format,…);
If concludes successfully, it returns the number of data items real from the stdin.If an error occurs, the scanf() function returns EOF.

Note, using %s causes the scanf to read character until a space, a newline, a tab, a form feend as encounted. Characters read by the scanf are stored into an array referenced by the corresponding argument.

A null character is automatically appended to the array after the reading.
scanf(“%d %d”);
scanf(“%s”,str);
scanf(“%d %d”,&x,&y);

getc() stops reading: new line or EOF.
scanf() stops reading: space, newline, a tab, a vertical tab or a form feed.

Here, &x,&y are memory location for the integer variables x,y.

In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all hte variables collectively in the format fo an array. Each variable, as an element fo the array, can be accessed either through the array reference or through a pointer that references the array.

An array consists of consecutive memory locations.
datatype Array_Name[Array-Size];
The total bytes of the array:
sizeof(data-type)* Array-size;
Or sizeof(Array_Name);

A character string is actually a character array ended with a null character .

Displaying Arrays of characters:
In C, a character string is defined as character array whose last element is the null character ‘’. For instances:
printf(“%s”,c);
1. Tell the printf() where to find the first element to start with.
2. Use the string format specifier %s in the printf() function.
Note, c contains the address of the first element in the array. That is, the start address of the array.
3. How the printf() function knows where the end of the character array is?
The answer: the last element in array is a ‘’ character. This is null character that marks the end of character array.

The Null character():
The null character ‘’ is treated as one character in C language; it is a specical character that makes the end of string. Therebefore, when function like printf() act on a character string, they process one character after another untill they encounter the null character.

The null character ‘’, which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement.

A series of characters enclosed in double quotes, is called a string constant.The C compiler can automatically add a null character at the end of a string constant to indicate the end of the string.

Initialize a character array str, with string constant:
char str[7] = “hello!”;
Here sizeof(str) = 6 + 1 = 7, ‘1’ is the position of null character.
char *ptr_str = “Hello world”;

Note, don’t specify the size of a character array as too small. Otherwise, it cannot string constant plus an extra null character. eg, the following declaration is considered illegal:
char str[4] = “test”;
That many C compilers will not issue a warning or an error that could eventually arise as result could be very difficult to debug. Therebefore, it’s your responsiblity to make sure you specify enough space for a string.

char *ptr_str;
ptr_str = “hello”;

These above two statments are equal to:
char *ptr_str = “hello”;

How long is a string, use strlen().
#include
size_t strlen(const char *s);
The return value from the function is the number fo bytes. Size_t is a data type defined in the string.h headfile. The size of data type depends on the paraticular computer systems.
strlen(array_name);
strlen(ptr_str);
Note, the result returned by strlen does not include null character.

Copying strings with strcpy()
If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array ot use strcpy().
strcpy() syntax:
# include
char *strcpy(char *dest, const char *size);
Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of the src if it is successful. eg:
char a[] = “hello”;
char *ptr_b = “world”;
strcpy(a, ptr_b)
Now, a[] is “world’.

The gets() function can be used to read characters from the standard input stream.
gets() syntax:
# include
char *gets(char *s);
Here the characters read from standard input stream are started in the character array identified by s. The gets() function stops reading, and appends a null character to the array, when a newline or end_of_file(EOF) is encounted. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.

puts(), writes characters to the standard output stream.
#include
init puts(const char *s);
s refers to the character array that contains a string. If puts() is successful, it returns 0. Otherwise, a nonzero is returned. The puts() function appends a newline character to replace the null character at the end of the character array.

Scanf syntax:
#include
int scanf(const char *format,…);
If concludes successfully, it returns the number of data items real from the stdin.If an error occurs, the scanf() function returns EOF.

Note, using %s causes the scanf to read character until a space, a newline, a tab, a form feend as encounted. Characters read by the scanf are stored into an array referenced by the corresponding argument.

A null character is automatically appended to the array after the reading.
scanf(“%d %d”);
scanf(“%s”,str);
scanf(“%d %d”,&x,&y);

getc() stops reading: new line or EOF.
scanf() stops reading: space, newline, a tab, a vertical tab or a form feed.

Here, &x,&y are memory location for the integer variables x,y.

分类: 科技 标签:

Pointer

2007年8月23日 没有评论

The duties of the pointer were to point out, by calling their names, those in the congregation who should take note of some point made in the sermon.

What is a pointer?

A pointer is a variable whose value is used to point to another variable. From the definition, you know two things: first, that a pointer is a variable, so you can assign different values to a pointer and second, that the values contained by a pointer must be an address that indicates the location of another variable in the memory. That’s why a pointer is also called an address variable.

Address(Left Value) versus Contents(Right Value)
Each memory location must have a unique address so that the computer can read from or write to the memory location without any confusion. This is similar to the concept that each house in a city must have a unique address. Right value, can be thought as a letter deliveved to the mailbox.

Note, when your C program is being compiled, and a value is being assigned to a variable, the C compiler has to check the left value of the variable. If the compiler cannot find the left value, if will issue an error message saying that the variable is undefined in your program. That’s why in C, you have to declare a variable before you can use it.

The Address-of operator: &
Return the address(that is, the left value) of a variable.
eg: long int x,y;
y = &x;
assignes the address of the x to the address variable y.

The format specifier %p is used in the printf() funtion for displaying the address.(Hexadecimal Format)

Declare Pointers
A pointer is a variable, which means that a pointer has a left value and right value as well. However, both the left and right values are addresses. The left value of a pointer is used to refer to the pointer itself, whereas the right value of a pointer, which is the content of the pointer, is the address of another variable.

The general form of a pointer declaration:
data-type *pointer-name
data-type specifies the type of data to which the pointer points.
pointer-name is the name of the pointer variable.
asterisk indicates the variable is a pointer, is called dereference operator(indirection operator).

Null pointer, a pointer is said to be a null pointer when its right value is 0. Remember, a null pointer can never point to valid data. Simply assign 0 to the pointer value. eg: ptr_c = 0

Pointing to the same thing: a memory location can be pointed by more than one pointer.
eg: ptr_c1 = &c; ptr_c2 = &c;
Point to the same location in the memory.

Pointer and Guidepost:
Memory — City
Pointer — Guidepost
Pointer variable Left Value — The position of Guidepost in the city
Pointer variable Right Value — The text on the Guidepost, which is somebody’s home address
Null Pointer: Set Pointer variable Right Value as 0, means no content on the Guidepost, as empty.
Multiple Pointers: Many Guideposts are mapping to the same home address.
*Pointer — People who lives in the house that is indicated on the Guidepost.
data-type *Pointer — What’s kind of people?

分类: 科技 标签:

Control Flow and Char type

2007年8月23日 没有评论

Condition branching(or jumping), loop category are both used for controlling flow.

if syntax:
if(expression){
statement1;
statement2;
}

else if(expression){
statement1;
statement2;
}
else{
statement1;
statement2;
}

Here expression is the condition criterion. If the expression is logical TRUE(nonzero), the statements inside the braces, are executed. If the expression is logical FALSE(zero), then the statements are skipped. Note, that the braces form a block of statements that is under the control of the if statement. If there is only one statement inside the block, the braces can be ignored. The parentness, however, must always be used to closed the condition expression. For instance:
if(x>0)
printf(“x>0n”);

The general form of the switch statement is:
switch(expression){
case expression1:
statement1;
break; //break is optional
case expression2:
statement2;
break;
default:
statement-default;
break;
}

When there’s no break, this is important feature of the switch statement. The computer continutes to execute the statements following the selected case until the end of the switch statement. If the return value of expression is equal to the constant expression expression1, the statement statement1 is executed. If however, the value of expression is not equal to any value of the constante expressions labeled by the case word, the statement-default key word is executed.

Add a break statement at the end of the statement list following every case label, if you want to exit the switch constant after the statements within a selected cases are executed.

The continute statement: Instead of breaking loop, there’re timeswhen you want to stay in a loop but skip over some statements within the loop. The continute statement causes execution to jump to the top of the loop immediately. Be aware to use this statement, as well as the break statement, may make your program hard to debug.

Goto is not recommended because it may cause your program to be unreliable and hard to debug.

The char Data Type:
An object of the char data type represents a single character of the character set used by your computer. But a computer can only store numeric code. Therebefor, characters such as A,a,B,b and so on all have a unique numeric code that is used by computers to represent the characters. Usually, a character takes 8 bits to store its numeric code.

For many computers, the ASCII code are the standard codes to represent a character set. ASCII, stands for American Standard Code for Information Interchange). The original ASCII character has set only 128 characters because it was the lower 7 bits that can be represent 2**7(128) characters. On IBM-compatible PCs, the character set is extended to contain a total of 2**8=256 characters.

分类: 科技 标签:

Review: C program

2007年8月21日 没有评论

Manipulating Bits:
Computer data and files are made of bits(or bytes).

There’re six bit-manipulation operators in C:
&: The bitwise AND operation
|: The bitwise OR operation
^: The bitwise exclusive OR(XOR)
~: The bitwise complement operator
>>: right-shift operator
<<: left-shift operator Decimal number converts to Hexadecimal or Binary. Each digit in a hex number consists of fours bits. A bit represents a digit in a binary number. As we know, the binary is a 2-based numbering system. Each digit in a binary number is called a bit, and can be 1 or 0. If the position of a bit in a binary number is n, then the bit can have a value of 2 to the power of n. The position of a bit in a binary number is counted from the right of the binary number. The most-right bit is at the position of zero. For example: (Binary) 1000 -> 1*2**3+0*2**2+0*2**1+0*2**0 -> 8 (Decimal)
(Decimal) 10 -> 1*2**3+0*2**2+1*2**1+0*2**0 -> 1010 (Binary)

&: Compare each bit of x to the corresponding bit in y. If both bits are 1, 1 is placed at the same position of the bit in the result. If one of the bits, or two of them, is 0, 0 is placed in the result.
eg: 01 & 11 returns 01

|: However, places 1 in the result if either operand is 1.
eg: 01 | 11 returns 11

^: Place 1 in the result if either operand, but not both.
eg: 01 ^ 11 returns 10

~: Takes just one operand. This operator reverses each bit in the operand.
eg: ~01 returns 10

>>: Shifts the bits of and operand to the right.

<<: Operand shifts the bits to the left. Note that the unsigned integer format specifier with a minimum field width of 6, %6u, and the uppercase Hex format specifier with the minimum width of 4, %04X, and used in the printf() function. The unsigned integer data type(that is, the non-negative integer data type) is chosen so that the complementary value of an integer can be shown and understood easily. ?: is called condition operator x?y:z, Here x, y and z are three expressions. X contains the test condition. Y and Z present the final value of the expression, if X returns nonzero(TRUE), y is chosen; if X returns zero(FALSE), Z is chosen. ~(1< 1111 1100
3. Finally, add one to the complemented binary.
eg. 1111 1100 + 0000 0001 —-> 1111 1101

As shown above, -3 is represented as “1111 1101” in binary format. There’s another method to find its binary format(Also contains three steps):
1. Set sign bit at leftmost position, 1 represent negative, 0 represent integer:
eg. -3 —-> 1000 0011
2. Reverse all the bits except of sign bit.
eg. 1000 0011 —-> 1111 1100
3. Add 1 to the above binary format.
eg. 1111 1100 + 0000 0001 —-> 1111 1101

Compare with above two method, their results are same. (-3 has binary format as 1111 1101).

Note the follow cases:

Case One. 4 – 3 = 1
4 —-> 0000 0100
-3 —-> 1111 1101
——————–
+ (1)0000 0001 Here, the blue marked “1” is ignored.
Note, integer(indicates number > 0) has three same format(orginal,reverse,complement codes). See the result “0000 0001”,its left-most bit is set as 0, which means it is an integer(>0), and the corresponding decimal number is 1.
More attention, the sign bits of both 4 and (-3) must be took part in adding calculation.

Case Two. 1 – 2 = -1
-2 original code: 1000 0010
reverse code: 1111 1101
complement code: 1111 1101 + 0000 0001 —> 1111 1110
Hence, 1 —> 0000 0001
-2 1111 1110
———————-
+ 1111 1111 (Complement code)
Convert negative number 1111 1111 from its complement code to original code:
Complement Code: 1111 1111
Reverse Code: 1111 1111 – 0000 0001 = 1111 1110
Original Code: 1111 1110 —> 1000 0001 —> Decimal 10
Here, when convert negative number from reverse code to its original code, the sign bit must NOT be changed.

Case Three: -2 – 2 = -4
-2 Complement Code: 1111 1110
-2 1111 1110
—————————–
+ Result Comple. Code: (1)1111 1100

Here blue marked 1 is ignored.

Convert the result to decimal:
Complement Code: 1111 1100
Reverse Code: 1111 1100 – 0000 0001 = 1111 1101
Original Code: 1111 1101 —> 1000 0010 —> -4 Decimal

The ANSI standard allows you to indicate that a constant is of type unsigned by suffixing u or U to the constant.

Change Data Sizes:
Sometimes, you want to reduce the memory taken by variables, or you need to increase the storage space of certain data types. C language gives you the flexiblity to modify size of data types.

The short modifier, a data type can be modified to take less memory by using the short modifier. For instance, you can apply the short modifier to an integer variable that is 32 bits long, which migh reduce the memory taken by the variable to as litter as 16 bits. By default, a short int data type is signed number:
eg: printf(“Hex of -12345 is 0x%X”, -12345); result is 0xFFFF CFCT
printf(“Hex of -12345 is 0x%hX”, -12345); result is 0xCFCT

Long modifier: more memory to keep values from a wide range.

You can add h into the integer format specifiet like this: %hd, %hu. Long is represented as “%L” or “%l”.

Have to include the header file math.h before you can use any math functions defined in the header file.

Pow syntax:
# include double pow(double x, double y);
x is raised to the power of y.

Sqrt syntax:
# include double sqrt(double x);
non-negative sqart root of x in double data type. The function returns error if x is negative.

Pow(x, 0.5) equals to Sqrt(x).

Note, all floating point calculations including both the float and double data type, are done in double-precision arithmetic. That is, a float data variable must be converted to a double in order to carry on the calculation. After the calculation, the double has to be converted to the float variable. Therefor a float calculation may take more time. THe main reason the C supports the float type is to save memory spaces, because the double data type takes twice as much memory space for storage as the float data type does.

分类: 科技 标签:

Use ScribeFire(Firefox addon) to publish MSN blog

2007年8月21日 没有评论

In the past three years, I had to edit my MSN space by IE in Windows, because firefox does not support MSN blog well(Sometimes format messes). Today I find a powerful tool to resolve this problem. Goes to the following link: “https://addons.mozilla.org/en-US/firefox/addon/1730”, and install ScribeFire for my firefox 1.5.07. After online installation, restart filefox, and there will a yellow iron on the below-right. Yes, it is ScribeFire.

Now I can write blog and publish on firefox. However, it seemes that I can not configure username/password by ScribeFire “Launch Account Wizard”. I have to edit in Scribe and paste the texts to blog window.

分类: 科技 标签:

Review: C program

2007年8月20日 没有评论

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.

分类: 科技 标签:

CTDB Introduction

2007年8月17日 没有评论

CTDB, Cluster Trival Database.
1. Consistent data and consistant locking
2. If one node failed, CTDB will automatically recovery and repair all CTDB database.
3. CTDB is PCIFS, parallel CIFS with samba 3/4
4. Support TCP and Infiniband

Requirement:
This filesystem must be mounted and available on all nodes in the CTDB cluster.

HA Features:
The CTDB nodes in a cluster designates one nodes as recovery master through an election process. If the recovery master node failes a new election is initiated so that the cluster will always guarantee there will be a recovery master. The recovery master will continously monitor the cluster to verify that all nodes contain a consistant configuration and view of the cluster and will initiate a recovery process when required.

During the recovery phase, the recovery master will automatically rebuild/recovery all clustered TDB database to ensure that the database are consistent. Recovery typically takes between 1 and 3 seconds. During the recovery period the database are frozen, and all database IO operation by CTDB clients are suspend.

To speed up the process of IP takeover and when clients attached to a failed node recovery as fast as possible. CTDB will automatically generate gratutions ARP packets to inform all nodes of the changed MAC address for that IP. CTDB will also send ‘tickle ack’ packets to all attached clients to trigger that clients to immediatlly recognized that the TCP connection needs to be re-established and to shortcut any TCP restransmission time outs that may be active in the clients.

CTDB current Architecture:
1. Multi-process server:
Multiple smbd daemons, one per client
Each daemon attached to a number of database
Database store all shared meta_data
2. Clustering this should be easy:
Why not just use a cluster database
Each smbd talkes to cluster database instead of local database
Obvious solution can be wrong.

CTDB is similarin conceps to Berkeley DB.

分类: 科技 标签:

Mandriva 2007: KMPlayer play rmvb

2007年8月4日 没有评论

I installed Mandriva 2K7 in my home PC. Fix the problem using KMPlay to play rmvb media. KMplayer can support the following types: GStream, xine and MPlayer. First, xine and Mplayer are able to play Mpeg format. When I try to open a rmvb file, Mplayer has only the sound without pictures, and it seems that xine does not support realplayer media.

As google suggestion, I set plf as Mandriva update source, and issue the below commands:

# urpmi xine-faad
# urpmi xine-flac
# urpmi real-codecs
# urpmi win32-codecs

Only flad can be installed,”xine-faad”,”real-codecs” and “win32-codecs” packagea are not found in the plf source. Hmm, I have to manually download the relevant packages from “rpm.pbone.net”. Note, “libstdc++5-3.3.6-3mdk.i586.rpm” is needed before install real-codes and win32-codes.

#wget ftp://mandrake.contactel.cz/plf/mandrake/2007.1/non-free/release/binary/i586/real-codecs-1.2-3plf.i586.rpm
#wget ftp://mirror.switch.ch/mirror/mandrake/official/2007.0/extra/i586/libstdc++5-3.3.6-3mdk.i586.rpm
#wget ftp://mandrake.contactel.cz/plf/mandrake/2007.0/non-free/release/binary/i586/win32-codecs-1.8-1plf2007.0.i586.rpm

# rpm -ivh libstdc++5-3.3.6-3mdk.i586.rpm
# rpm -ivh real-codecs-1.2-3plf.i586.rpm
# rpm -ivh win32-codecs-1.8-1plf2007.0.i586.rpm

OK, the KMplayer can play rmvb now.However, Mplayer trouble still exists.

分类: 科技 标签:

Alexandre Pato To Milan!

2007年8月3日 没有评论

From www.acmilan.com 8/2/2007
MILANO – The great talent Alexandre Pato will train with Milan and will be able to play the friendly matches starting from the 3rd of September 2007. From teh 3rd of January 2008 onwards he will also be able to play in the official games.

分类: 科技 标签: