BUFSIZ is a constant macro representing the size of the input buffer. It is defined in stdio.h and represents the size for your C implementation. ANSI C requires that it be at least 256 bytes. A common size is 512. It gives us the maximum number of characters that can be transferred through our standard input stream, stdin.
Showing posts with label computer science must know. Show all posts
Showing posts with label computer science must know. Show all posts
Thursday, March 6, 2014
Thursday, January 12, 2012
What would be the result of the expression -6/5?
The ANSI C standard says that the result is "implementation defined." This means that ANSI C has no hard and fast rule, therefore, the result depends on the compiler that you use. Check your compiler; it should give either -1 or -2 as a result.
Note that the "correct" answer is -1.2. The compiler therefore has a choice of rounding up or down.
Note that the "correct" answer is -1.2. The compiler therefore has a choice of rounding up or down.
Wednesday, January 11, 2012
What happens if we try to assign an integer type value to a float type variable?
C puts a decimal point at the end and converts the method of storage to exponential binary form and store the result in the variable's memory cell. Thus,
p = 6/4;
takes the integer value (6/4, which becomes 1 due to the cutting off of the fractional part), converts this to 1.0 (with a decimal point meaning it is in exponential binary form), and stores this in the memory cell for the variable.
p = 6/4;
takes the integer value (6/4, which becomes 1 due to the cutting off of the fractional part), converts this to 1.0 (with a decimal point meaning it is in exponential binary form), and stores this in the memory cell for the variable.
Tuesday, January 10, 2012
What happens if we try to assign a real type value to a variable that has been declared to be int?
C cuts off the fractional part of the real value, converts the remaining part to int, and stores the result in 2's complement binary form in the memory cell for the variable. For instance, the statement
n = 6/4.0
takes the real value (6/4.0, which is 1.5), cuts off the 0.5 to give 1.0, converts this to 1 (without a decimal point meaning it us in 2's complement binary form), and stores this in memory cell for the variable n.
n = 6/4.0
takes the real value (6/4.0, which is 1.5), cuts off the 0.5 to give 1.0, converts this to 1 (without a decimal point meaning it us in 2's complement binary form), and stores this in memory cell for the variable n.
What is the value of 6/4.0?
For this operation, one operand is a real type and the other operand is an integer type. When this occurs, C converts the integer to a real type temporarily (meaning that 6 is converted to 6.0) then performs the operation and the result is a real type. Thus, 6/4.0 gives the result 1.5.
Monday, January 9, 2012
What is the value of 6/4?
Here, we have an integer being divided by an integer. When this occurs, if both operands are negative or positive, the fractional part of the quotient is discarded. Therefore, 6/4 is not equal to 1.5. It is equal to 1.
How do we initialize variables?
The first method uses an assignment statement to initialize a variable; for example,
e=3;
Method 2 initializes a variable in a declaration statement; for example,
float a=7, b=6;
e=3;
Method 2 initializes a variable in a declaration statement; for example,
float a=7, b=6;
Sunday, January 8, 2012
What happens if we try to divide a number by 0?
In general, this causes a run-time error and termination of execution. A common error message displayed by the computer when this occurs uses the word overflow, because division by 0 or a number close to 0 produces a very large number. The number being too large to store in the allocated memory causes an interpretation of an overflow problem.
Saturday, January 7, 2012
Can a single variable also be considered an expression?
Yes, for instance, a single variable located alone on the right ride of an assignment statement is considered to be an expression. You will see other times when a single variable is considered to be an expression.
Is an arithmetic expression a complete C statement and how are arithmentic expressions used in assignment statements?
An arithmetic expression is not a complete C statement, but only a component of a statement. The value evaluated from the expression may be stored in a variable using an assignment statement. For example, the arithmetic expression x / y is part of a C assignment statement
d = x / y;
The statement assigns the value obtained from the arithmetic expression on the right to the variable on the left. Thus, the assignment statement
i = i + 1
although not looking correct algebraically, is a valid C assignment. The arithmetic expression i + 1 creates a new value that is 1 greater than the current value of i. The assignment statement then gives i this new value.
Note that we cannot write these two assignment statements as
x / y = d;
i + 1 = i;
because on the left side of assignment statements we can have only single variables, not expressions. Single variables are allowed to be lvalues (pronounced "ell-values"), meaning they are allowed to be on the left side of assignment statements. Expressions are rvalues (pronounced "are-values") because they are allowed on the right side of assignment statements.
d = x / y;
The statement assigns the value obtained from the arithmetic expression on the right to the variable on the left. Thus, the assignment statement
i = i + 1
although not looking correct algebraically, is a valid C assignment. The arithmetic expression i + 1 creates a new value that is 1 greater than the current value of i. The assignment statement then gives i this new value.
Note that we cannot write these two assignment statements as
x / y = d;
i + 1 = i;
because on the left side of assignment statements we can have only single variables, not expressions. Single variables are allowed to be lvalues (pronounced "ell-values"), meaning they are allowed to be on the left side of assignment statements. Expressions are rvalues (pronounced "are-values") because they are allowed on the right side of assignment statements.
Friday, January 6, 2012
What are the meanings of operators ++, --, and % ?
The operator ++ is an increment operator, which can be placed before or after (but not both) a variable. The operator will increase the value of the variable by 1. For example, assuming a variable i is equal to 1, then after the statement
i++;
or
++i;
is executed, the value of i is 2 (i++ is not exactly the same as ++i).
Note that the C statement
i++;
or
++i;
can be understood as the statement
i = i + 1;
which also causes the value of the variable i to increase by 1. Similarly, the operator -- is a decrement operator, which decreases the value of a variable by 1. Also, the statement
i--;
or
--i;
can be understood as the statement
i = i - 1;
The operator % is a remainder operator, which must be placed between two integer variables or constants. Assuming k and p are two integer variables, the meaning of k%p is the remainder of k divided by p. For example, if k = 11 and p = 3, then k%p is equivalent 11%3, which is equal to 2 (because 3 goes into 11 three times with a remainder of 2). The operator % is pronounced "mod." So this example would be k mod p. ANSI C states that, if either operand is negative, the sign of the result of the % operation is implementation defined; that is, it is free for the C compiler designer to decide. For example, depending on the compiler you use, the results of -50 % 6 and 50 % (-6) may be 2 or -2.
i++;
or
++i;
is executed, the value of i is 2 (i++ is not exactly the same as ++i).
Note that the C statement
i++;
or
++i;
can be understood as the statement
i = i + 1;
which also causes the value of the variable i to increase by 1. Similarly, the operator -- is a decrement operator, which decreases the value of a variable by 1. Also, the statement
i--;
or
--i;
can be understood as the statement
i = i - 1;
The operator % is a remainder operator, which must be placed between two integer variables or constants. Assuming k and p are two integer variables, the meaning of k%p is the remainder of k divided by p. For example, if k = 11 and p = 3, then k%p is equivalent 11%3, which is equal to 2 (because 3 goes into 11 three times with a remainder of 2). The operator % is pronounced "mod." So this example would be k mod p. ANSI C states that, if either operand is negative, the sign of the result of the % operation is implementation defined; that is, it is free for the C compiler designer to decide. For example, depending on the compiler you use, the results of -50 % 6 and 50 % (-6) may be 2 or -2.
What can be an operand?
An operand can be a variable, such as x or y, or a constant, such as 3.1416, or anything that represents a value, such as a function.
What are the components of an arithmetic expression?
An arithmetic expression consists of a sequence of operand(s) and operator(s) that specify the computation of a value. For example, the expression, -x + y, consists of two operands x and y and two operators + and -.
What is an arithmetic expression?
An arithmetic expression is a formula for computing a value. For example,
x + y
computes x plus y.
x + y
computes x plus y.
Why is so much attention given to the printf statement?
There are two reasons. One is that you will write printf statements very frequently and other aspects of programming will become easier for you if you feel comfortable writing printf statements. It is a good idea to become proficient at writing them now so that you can easily go on to other programming issues. The other reason is that improperly written printf statements are the source of many errors for beginning programmers. If you understand printf statements, you will substantially reduce your programming errors.
For engineering programming, is any danger involved in using a %f type display format?
If your numbers are very small they could be printed as 0. For instance, if your are working with small measurements and you want to print 3.5 x 10^-12 meters and you use as a format $f, your output will be 0.000000. Therefore, in working with small numbers, to display meaningful results, you must use an exponential type format such as %e.
What happens if we try to display an int with %f or a float with %d?
If you try to do this you will most likely get completely nonsensical values or zeros displayed on the screen. This is a common error that beginners (and even some veterans) make. It is an extremely frustrating error because everything else in your program may be correct but a simple %d instead of %f will make it appear like you have major errors in your program. Then you may spend a lot of time looking at your logic, doing hand calculations and in the end it is a simple conversion specification that has caused the problem. If you get total nonsense or zeros for your output, check your conversion specifications before you investigate other, more difficult to trace, sources of error.
Given the same value and using the same format, will programs created using different compilers display exactly the same output?
No, because various compilers implement the ANSI C standard differently, so, in general, given the same value and using the same format, the output displayed by programs created with different compilers may be slightly different.
What does the flag - do?
It left-justifies a value that is put in a field width that is greater than the actual.
What is displayed if the precision specified for a real is less than or greater than the actual or not specified?
-Less than actual, the printf function displays only the number of digits in the specified precision (trailing digits are not lost from memory, they simply are not displayed)
-Greater than actual, the printf function adds trailing zeros to make the displayed precision equal to the precision specified.
-Not specified, the printf function makes the precision equal to six (it adds trailing zeros or truncates digits if necessary to get a precision of six; these actions do not change the value stored in memory.)
-Greater than actual, the printf function adds trailing zeros to make the displayed precision equal to the precision specified.
-Not specified, the printf function makes the precision equal to six (it adds trailing zeros or truncates digits if necessary to get a precision of six; these actions do not change the value stored in memory.)
Subscribe to:
Posts (Atom)