When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
59 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
!f
#include <stdio.h>
int main() {
for (int i = 0; i < 100; i++) {
printf("Teknik Informatika YES\n");
}
printf("\nAngka kelipatan 5 antara 0-100:\n");
for (int i = 5; i <= 100; i += 5) {
printf("%d ", i);
}
printf("\n\nMasukkan jumlah perulangan: ");
int n;
scanf("%d", &n);
int total = 0;
printf("Masukkan %d nilai:\n", n);
for (int i = 0; i < n; i++) {
int nilai;
scanf("%d", &nilai);
total += nilai;
}
printf("Total penjumlahan: %d\n", total);
return 0;
}
Why do you think something is wrong?
the int?
Does your code produce the result you want it to?
the code say this line is error
What does the error say?
Okay, that's saying that you compile with -std=c89 or -std=c90, which means you're using a standard that's been outdated for multiple decades by now
ah
How do you compile your code?
VS Code doesn't have a compiler. It's just a text editor
ah i see
if you're intending to use C89 then you have to declare all variables at the top of a compound statement or at file scope
;compile -ansi -pedantic-errorsc printf("h\n"); int x;
<source>: In function 'main':
<source>:5:1: error: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
5 | int x;
| ^~~
Build failed
Declaring a variable is basically saying that this object exists. So when you write int x you're declaring that there is an int named x.
A compound statement is basically the stuff inside of the curly brackets.
C89 requires that all your declarations have to go at the top of a compound statement. So ```c
int main() {
printf("h\n");
int x;
return 0;
}
The reason your code isn't working is because you have declarations mixed in your code. You have to move those to the top of the function
Or you can compile with a newer standard, which would allow this mixing
Are you compiling in C89 intentionally?
Is it a requirement for you to use a standard that's 35 years too old?
no
i just follow my instructor
but still the code error
thats why i ask in here
idk iam wrong or my instructor are wrong
You may have different versions of compilers, which would cause the default version that it compiles on to be different
If your instructor writes for loops like this, then yes, specify C99.
okay
Neither of you are wrong. You're just using different standards of the C programming language. You are using the 1989 standard, and your instructor is likely using 1999 or some later standard.
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity