Deitel & Deitel, C How to Program 6th Edition, Exercise 9.8 (Displaying Values and Their Number of Characters)
// Displaying values and their number of characters
#include <stdio.h>
int main() {
int i, a[10] = { 124, 324, 232, 43, 4, 123, 356, 76, 1000, 1 }, totalDigit = 0, digit = 0;
_set_printf_count_output(1); // enable %n for printf family
for (i = 0; i < 10; i++) {
printf("Number%d: ", i+1); //number1, number2...
printf("%d%n\n", a[i], &digit); // %n counts characters that printf contains before its present position
totalDigit += digit;
printf("Digit: %d\tTotal digit: %d\n\n", digit, totalDigit);
}
printf("\n\n\n");
return 0;
}
#include <stdio.h>
int main() {
int i, a[10] = { 124, 324, 232, 43, 4, 123, 356, 76, 1000, 1 }, totalDigit = 0, digit = 0;
_set_printf_count_output(1); // enable %n for printf family
for (i = 0; i < 10; i++) {
printf("Number%d: ", i+1); //number1, number2...
printf("%d%n\n", a[i], &digit); // %n counts characters that printf contains before its present position
totalDigit += digit;
printf("Digit: %d\tTotal digit: %d\n\n", digit, totalDigit);
}
printf("\n\n\n");
return 0;
}
Comments
Post a Comment