2
#include <stdio.h>

int main() {
    char a[1000];
    int i = 0;
    scanf("%s", &a);
    while (a[i] != 0) {
        printf("%c\n", a[i]);
        i++;
    }
    printf("\n");
    return 0;
}

"A large integer of up to 1,000 digits is given as input. Write a program that prints out each digit of the integer after receiving the corresponding integer." was the problem and I don't know what is not complete in my code. I got 4/5 point. answer have to be like:

Input: +456

Output:

+
4
5
6
chqrlie
  • 131,814
  • 10
  • 121
  • 189
나가을
  • 37
  • 4
  • 1
    minimum change: `scanf("%s", a);` (remove `&`) – pmg May 17 '20 at 14:25
  • 1
    You need an array if size 1001 to hold a string of 1000 digits plus a terminating thanks byte. You should present overflow using `”%1000s”` too, and remove the `&`. – Jonathan Leffler May 17 '20 at 14:37
  • Hint: `putchar ('\n');` instead of `printf ("\n");` -- there is no conversion needed in the *format string*, so there is no need to call the variadic `printf` function. Simply use `putchar()` to output a single character to `stdout` or `fputc()` to output a single character to a file. – David C. Rankin May 17 '20 at 15:54
  • @나가을: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie May 19 '20 at 22:25

5 Answers5

3

Your code has two issues:

1.

scanf("%s", &a);

The argument a already decays to a pointer to the first element of array a -> so the type of it as argument is actually char *. Thus, &a is of type char (*)[1000].

There is a type mismatch to the expected argument of type char * for the %s conversion specifier.

If you use GCC as compiler, the -Wall option (or respectively -Wformat=) had showed you a warning about this:

warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[1000]' [-Wformat=]

    9 |     scanf("%s",&a);
      |            ~^  ~~
      |             |  |
      |             |  char (*)[1000]
      |             char *

2.

Furthermore, if you need to store an integral number up to 1000 digits - which includes the case of a 1000 digit integer, you forgot that you need one element more to store the string-terminating null character ('\0'):

char a[1001];

Else, if the user inputs a number of 1000 digits, the null character would be stored beyond the bounds of the array, which invokes undefined behavior.


Additionally:

Use a length modifer to ensure there will occur no buffer overflow if the user attempts to enter a number of more than 1000 digits:

scanf("%1000s", a);

or use fgets() which is ensures this by default as it requires the number of characters to be written as second argument.

fgets(a, sizeof(a), stdin);

sizeof(a) is appropriate as sizeof(char) is always 1. It obtains the number of elements, a has.


Result:

#include <stdio.h>

int main (void) {
    char a[1001];
    int i = 0;

    printf("Please enter an integral number up to 1000: ");
    fgets(a, sizeof(a), stdin);
    // scanf("%1000s", a);         Alternative to previous fgets.

    while (a[i] != 0) {
        printf("%c\n", a[i]);
        i++;
    }

    printf("\n");

    return 0;
}
Community
  • 1
  • 1
1
scanf("%s",&a);

Should change to:

scanf("%999s",a);

or you can use fgets from stdin instead:

fgets(a, 1000, stdin);

see Disadvantages of scanf

Hitokiri
  • 3,607
  • 1
  • 9
  • 29
0

There are multiple problems in your code:

  • the size of the array is too small, it can only contain up to 999 digits.
  • the scanf() format should specify the maximum number of characters to store into the destination array to avoid undefined behavior on overlong input.
  • the scanf() argument &a is incorrect, it should be just a.
  • the return value of scanf() should be tested to avoid undefined behavior on an empty input file.

Here is a corrected version:

#include <stdio.h>

int main() {
    char a[1002]; /* sign + 1000 digits + null terminator */
    if (scanf("%1001s", a) == 1) {
        for (int i = 0; a[i] != '\0'; i++) {
            printf("%c\n", a[i]);
        }
        printf("\n");
    } else {
        printf("error: no input\n");
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
-1

Ma'am, String in c always end with a null character i.e, '\0' not just 0. replace this "while(a[i]!=0)" with "while(a[i]!='\0')".. and everything ll be cool..:-)

  • 3
    `'\0'` and `0` are 100% absolutely fully unequivocally **absolutely identical** in C. – pmg May 17 '20 at 14:28
  • Could you please elaborate this?...while(a[i]!=0) and while(a[i]!='\0') are both of these means the same thing? – G V PHANI KUMAR May 17 '20 at 14:34
  • 1
    Yes, in C source text `0` has value zero and type `int`... `'\0'` has value zero and type `int`. For example: `while (*p != 0)`is 100% identical to `while (*p != '\0')` or `for(j=0;j<9;j++)` is 100% identical to `for(j='\0';j<9;j++)` ... – pmg May 17 '20 at 14:43
  • 1
    also, in C, `sizeof '\0'`, `sizeof 0`, and `sizeof (int)` all have the same value (probably `4`) – pmg May 17 '20 at 14:50
-1

Going by your requirement, scanf will take 'a' not '&a'. Also the loop will continue till the end, which is denoted by '\0' character and not zero.

#include <stdio.h>

int main(){
    char a[1000];
    int i=0;
    scanf("%s", a);
    while(a[i]!='\0')
    {
        printf("%c\n",a[i]);
        i++;
    }
    printf("\n");
    return 0;
}
hacker315
  • 1,996
  • 2
  • 13
  • 23