Sie sind auf Seite 1von 3

What is pre-increment and post-increment with example?

Answer: When you use a post-increment (n++) you are first making a copy of the variable . This is the value used in your statement. The variable will be incremented, bu t not until the current line is done executing. When it finishes, the incremente d value will replace n. When you use a pre-increment (++n) you are first making a copy of the variable, as in post-increment. The difference is that the value you copied will be increm ented right away. This value will then be copied back into n after the current l ine is done executing Just remember that post-increment happens AFTER you use it, and pre-increment ha ppens right when you use it. Try running the following code to see it in action.

int main(int argc, char** argv) { int n = 0; printf("%d\n", n); // this would print out 0, since n = 0 printf("%d\n", n++); // this would print out 0, since n isn't incremented until after this line printf("%d\n", n); // now n would print out as 1 printf("%d\n", ++n); // now n would print out as 2 // more illustrative examples: printf("%d\t%d\n", n++, n); // this time we see that both ns are printed out as 2 printf("%d\n", n); // n is only shown as 3 after the post-increment line printf("%d\t%d\n", ++n, n); // and now we see that the first n is printed as a 4 // while the second is still a 3 printf("%d\n", n); // and once again n is set to 4 after the last line return 0; } ------------------------------------------------------------------------------------------------------------18.2.2: Cast Operators [This section corresponds to the second half of K&R Sec. 2.7] Most of the time, C performs conversions between related types automatically. (S ee section 18.2.3 for the complete story.) When you assign an int value to a flo at variable or vice versa, or perform calculations involving mixtures of arithme tic types, the types are converted automatically, as necessary. C even performs some pointer conversions automatically: malloc returns type void * (pointer-to-v

oid), but a void * is automatically converted to whatever pointer type you assig n (say) malloc's return value to. Occasionally, you need to request a type conversion explicitly. Consider the cod e int i = 1, j = 2; float f; f = i / j; Recall that the division operator / results in an integer division, discarding t he remainder, when both operands are integral. It performs a floating-point divi sion, yielding a possibly fractional result, when one or both operands have floa ting-point types. What happens here? Both operands are int, but the result of th e division is assigned to a float, which would be able to hold a fractional resu lt. Is the compiler smart enough to notice, and perform a floating-point divisio n? No, it is not. The rule is, ``if both operands are integral, division is inte ger division and discards any remainder'', and this is the rule the compiler fol lows. In this case, then, we must manually and explicitly force one of the opera nds to be of floating-point type. Explicit type conversions are requested in C using a cast operator. (The name of the operator comes from the term typecast; ``typecasting'' is another term for explicit type conversion, and some languages have ``typecast operators.'' Yet an other term for type conversion is coercion.) A cast operator consists of a type name, in parentheses. One way to fix the example above would be to rewrite it as f = (float)i / j; The construction (float)i involves a cast; it says, ``take i's value, and conver t it to a float.'' (The only thing being converted is the value fetched from i; we're not changing i's type or anything.) Now, one operand of the / operator is floating-point, so we perform a floating-point division, and f receives the valu e 0.5. Equivalently, we could write f = i / (float)j; or f = (float)i / (float)j; It's sufficient to use a cast on one of the operands, but it certainly doesn't h urt to cast both. A similar situation is int i = 32000, j = 32000; long int li; li = i + j; An int is only guaranteed to hold values up to 32,767. Here, the result i + j is 64,000, which is not guaranteed to fit into an int. Even though the eventual de stination is a long int, the compiler does not look ahead to see this. The addit ion is performed using int arithmetic, and it may overflow. Again, the solution is to use a cast to explicitly convert one of the operands to a long int: li = (long int)i + j; Now, since one of the operands is a long int, the addition is performed using lo ng int arithmetic, and does not overflow. Cast operators do not have to involve simple types; they can also involve pointe r or structure or more complicated types. Once upon a time, before the void * ty pe had been invented, malloc returned a char *, which had to be converted to the type you were using. For example, one used to write things like int *iarray = (int *)malloc(100 * sizeof(int)); and struct list *lp = (struct list *)malloc(sizeof(struct list)); These casts are not necessary under an ANSI C compiler (because malloc returns v oid * which the compiler converts automatically), but you may still see them in older code. What is the difference between pointer variable and simple variable? Answer:

A variable has 3 things when you make them : int <- type x <- name =3 <- data A pointer doesn't have any data in themself, they "point" to a variable. Like this : int x = 3; int *x = x now the *x is 3, because it "points" to the x variable ( actually it points to i t's memory address ) *x = 6 means that where the pointer is pointing ( the memory part ) will be fill ed with this data. a little more thing: incrementing ( i++ ) a pointer can do two different things : (*x) ++ adds +1 to the value , while *x++ will move the "pointing" to the next thing in the memory ( if we are not ta lking about an array, then it's usually memory garbage ) Usual variable used so called value type mechanism, meaning that if you have pas sed the variable to a function the variable itself was not passed, only its copy . Which makes value type mechanism safe. The only problem is that you use a lot of memory because a copy of your variable has been created. Pointers allow to avoid creating copies and operate with addresses of variables. It means when you pass your variable to any function, you actually pass only th e variable's address. This mechanism is called reference type. Pointers work much faster and allow to use memory more effectively. The only pro blem is you have to take extra care when you are working with pointers. Using po inters any area of memory including protected by OS can be accessed. Such event will cause "blue screen" (under windows).

Das könnte Ihnen auch gefallen