Q4 1-40 Flashcards
What is wrong with the following code? #include /This is a directive line/ main /* This function will be automatically called/ (void/ I do not have arguments to pass/) { / This is the first line in the program /* I am so excited to write this program / printf(“Hello world\n”)} / Finally I am done with it /* But I am not sure why it does not work :-( */ }
Extra curly bracket. Wrong quotations marks, should be “ not “
Which of the following are legal C identifier? 100_bottles, _100bootles, one-hundred-bottles, one_hundred_bottles, one_hundred_bottles, one__hundred__bottles, one__hundred__bottles__, If, main, Printf, and while.
Identifier may contain digits, letters and underscores but must only begin with letter or underscore. Keywords cannot be used as identifiers: 100bootles,one_hundred_bottles,one_hundred_bottles,one_hundred_bottles,one_hundred_bottles,main, Printf, are legal apparently :P
How many token are there in the following C statement? x=answer=(a*b/++c+6-a%f)/(k);
24 tokensx: Identifier, =: operator, answer: Identifier, =: operator, (: punctuation, a: identifier, *: operator, b: identifier, etc.
What is the output of the following C statement? printf(“%6d, %4d\n” , 86, 1040);
%d is a placeholder for integers.%6d indicates 6 figures for the integer.%4d indicates 4 figures for the integer.86, 1040
What is the output of the following C statement? printf(“%12.5e\n”, 30.253) ;
%e placeholder for exponent.%12.5 indicates 12 figures and 5 decimal places.3.02530e+01
What is the output of the following C statement? printf(“%.4f\n”, 83.162) ;
%f is a placeholder for float.%.4f indicates 4 decimal places83.1620
What is the output of the following C statement? printf(“%-6.2g\n”, .0000009979);
1e-06 %g converts it into exponential or fixed decimal. if it is more than 4 figures, it will show as exponent. if it is less than 4 decimal places of zeros, it will also show as exponent. in the example, it has gone back 7 decimal places.
Write calls to printf that display a float variable x in the following formats: (a) Exponential notation; left-justified in a field of size 8; one digit after the decimal point. (b) Exponential notation; right-justified in a field of size 10; six digits after the decimal point. (c) Fixed decimal notation; left-justified in a field of size 8; three digits after the decimal point. (d) Fixed decimal notation; right-justified in a field of size 6; no digits after the decimal point.
(a) %-8.1e (b) %10.6e (c) %-8.3f (d) %6.f(inclusion of minus sign indicates left-justified)
What is the output of the following C statement? printf(“%d\n%-d\n%d\n%-d\n”,123456,123456,-123456,-123456);
123456 123456 -123456 -123456
What is the output of the following C statement? printf(“%.10d\n%-.10d\n%.10d\n%-.10d\n”, 123456,123456,-123456,-123456);
0000123456 0000123456 -0000123456 -0000123456
What is the output of the following C statement? printf(“%20d\n%-20d\n%20d\n%-20d\n”, 123456,123456,-123456,-123456);
- 123456* 123456 * * -123456 *-123456 *
What is the output of the following C statement? printf(“%20.10d\n%-20.10d\n%20.10d\n%-20.10d\n”, 123456,123456,-123456,-123456);
- 0000123456* 0000123456 * * -0000123456 *-0000123456 *
What is the output of the following C program? #include main(void) { float m = 6, n = 6, o = 6, p = 6, q = 6; int i = 3, j = 3; m ++; ++ m; m += n += o -= p *= q /= i %= ++j ; printf(“%f\n%f\n %f\n %f\n %f\n %d\n%d”, m, n, o, p, q, i, j); m += 3 + (n += 3 + (o -= 3 + (p *= 3 + (q /= 3 + (i %= j++))))); printf(“%f %f\n%f %f\n%f %d %d\n”, ++m, n++, –o, p–, ++q, i–, –j); }
Should include otherwise won’t compile. once included. It should produce: 8.000000 0.000000 -6.000000 12.000000 2.000000 3 4-34.000000 -46.000000 -50.000000 40.000000 1.333333 3 4
Suppose that we call scanf as follow: scanf (“%d%f%d” , &i, &x, &j); If the user entered 10.3 5 6 what will be the values of i, x, and j after the call? (Assume that i and j are int variables and x is a float variable.)
i will be 10, x will be 5.000000 and j will be 6
Suppose that we call scanf as follow: scanf(“%f%d%f”, &x, &i, &y); If the user entered 12.3 45.6 789 what will be the values of x, i, and y after the call? (Assume that x and y are float variables and i is an int variable.)
x will be 12.300000, i will be 45, 789 will be 789.000000
For each of the following pairs of scanf format strings, indicate whether or not the two strings are equivalent. If they’re not, show how they can be distinguished. (a) “%d” versus “ %d” (b) “%d-%d-%d” versus “%d -%d -%d” (c) “%f” versus “%f “ (d) “%f,%f” versus “%f, %f”
(a) equivalent (b) equivalent (c) Different, space after %f seems to prompt for another entry, and but only execute the first entry (d) Equivalent