简体   繁体   中英

VS Code C program not run

#include<stdio.h>
#include<conio.h>
void main(){
    system("cls");
    int k;
    // printf("Enter a Number: ");
    // scanf("%d",&n);

    for(int i = 1; i<=5;i++){
        k = 0;
        for(int j = 1 , k+=i; j<=5; j++){
            
            printf("%2d",k);
            k += 5;
            
        }
        printf("\n");
    }
    getch();
}

this program in vs code not run but in turboo c this program run.In vs code give error:

error: expected '=', ',', ';', 'asm' or '__attribute__' before '+=' token
         for(int j = 1 , k+=i; j<=5; j++){

This program in vs code not run, but in turbo c this program runs. In vs code it gives error:

error: expected '=', ',', ';', 'asm' or '__attribute__' before '+=' token
         for(int j = 1 , k+=i; j<=5; j++){

GCC fails with this:

foo.c:12:26: error: invalid '+=' at end of declaration; did you mean '='?
        for(int j = 1 , k+=i; j<=5; j++){

You can't use += in the first part of the for statement.

It's expecting a variable declaration, and you've given it two:

int j = 1, k += i;

This declaration of k hides the previous one, and you're attempting to initialize it in terms of itself.

It's unclear from your question what you're expecting to do with k , but try k = k + i or just k = i instead. Or just initialize k on the line above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM