Hello I am new to coding and was just wondering why the below code:
#include <stdio.h>
int main() {
for(int i = 0; 1 ; i++) {
char x;
char z[1+i];
x=getchar();
if (x == '\n'){
*(z+i) = '\0';
printf("%s",z);
break;}
*(z+i) = x;
printf("%s, %d = %c, i = %d\n",z, (z+i),*(z+i),i);
}
return 0;
}
of C does not work for inputs that are more than 15 characters? (I don't think it is beacuse of the current state of my pc since I tried it with an online compiler and it still breaks at 16 chars, but then again I am new to coding.) Would appreciate any help. Thank you. (If the answer is something like: "Thats not possible in C" or something like that please don't leave it at that and explain why it is not possible).
PS: I cheked some of the similar questions to mine but I don't get what any of them are doing, if this post is a duplicate, I would really appreciate if you could link me to the post that solves my issue, I will delete this post shortly after if that is the case.
This is the input and output of my program for 16 chars,
1234567890123456
1, 6421952 = 1, i = 0
12, 6421953 = 2, i = 1
123, 6421954 = 3, i = 2
1234, 6421955 = 4, i = 3
12345, 6421956 = 5, i = 4
123456, 6421957 = 6, i = 5
1234567, 6421958 = 7, i = 6
12345678, 6421959 = 8, i = 7
123456789, 6421960 = 9, i = 8
1234567890, 6421961 = 0, i = 9
12345678901, 6421962 = 1, i = 10
123456789012, 6421963 = 2, i = 11
1234567890123, 6421964 = 3, i = 12
12345678901234, 6421965 = 4, i = 13
123456789012345, 6421966 = 5, i = 14
1234567890123456p@, 6421967 = 6, i = 15
Process returned 0 (0x0) execution time : 4.076 s
Press any key to continue.
And this is the input and output for 15 chars,
123456789012345
1, 6421952 = 1, i = 0
12, 6421953 = 2, i = 1
123, 6421954 = 3, i = 2
1234, 6421955 = 4, i = 3
12345, 6421956 = 5, i = 4
123456, 6421957 = 6, i = 5
1234567, 6421958 = 7, i = 6
12345678, 6421959 = 8, i = 7
123456789, 6421960 = 9, i = 8
1234567890, 6421961 = 0, i = 9
12345678901, 6421962 = 1, i = 10
123456789012, 6421963 = 2, i = 11
1234567890123, 6421964 = 3, i = 12
12345678901234, 6421965 = 4, i = 13
123456789012345, 6421966 = 5, i = 14
123456789012345
Process returned 0 (0x0) execution time : 4.150 s
Press any key to continue.
As you can see it works fine for 15 chars of input, but breaks at 16, this is the problem that I want to be solved.
zon each iteration. The fact that it "keeps" the old values from the previous iteration is just an accident (undefined behavior). Second, you try to usezas a C-style string, but you don't terminate it with\0. The fact that it's de-facto terminated in your case and does not crash on first iterations is another accident. Thirdly, local arrays with dynamic variables is compiler extension, not standard C, but that is irrelevant here. Try compiling with-fsanitize=address.intto be 16 bits or 32 bits or some other size is a compiler extension: It is specified by the C standard but some choice is permitted by the implementation. Variable-length arrays are specified in the C standard. They were a mandatory feature in C 1999 but are optional now.fgets.