나가을
나가을

Reputation: 37

I want to make program in C printing each number of full number

#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

Upvotes: 2

Views: 127

Answers (5)

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;
}

Upvotes: 3

chqrlie
chqrlie

Reputation: 145307

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;
}

Upvotes: 0

Hitokiri
Hitokiri

Reputation: 3699

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

Upvotes: 1

Rajat Jain
Rajat Jain

Reputation: 2032

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;
}

Upvotes: -1

G V PHANI KUMAR
G V PHANI KUMAR

Reputation: 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..:-)

Upvotes: -1

Related Questions