Reputation: 37
#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
Reputation: 15082
Your code has two issues:
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 *
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
Reputation: 145307
There are multiple problems in your code:
scanf()
format should specify the maximum number of characters to store into the destination array to avoid undefined behavior on overlong input.scanf()
argument &a
is incorrect, it should be just a
.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
Reputation: 3699
scanf("%s",&a);
Should change to:
scanf("%999s",a);
or you can use fgets
from stdin
instead:
fgets(a, 1000, stdin);
Upvotes: 1
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
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