Problem with local and global variable exercise in C [closed]

0

I did the following exercise as it appears in a book:

/*4_3.c en prueba*/
#include<stdio.h>
int k=5; /*variable global*/

void main(void){
void f1(void); /*prototipo de función*/
int i;
 for(i=1;i<=3;i++){
    f1();
 }
}

void f1(void){
int k=2 /*variable local*/
k+=k;
printf("\n\nEl valor de la variable local es: %d", k);
::k = ::k + k;
printf("\nEl valor de la variable global es: %d", ::k);
}

and I was supposed to get a program like this more or less:

but when compiling I get an error, I'm using in cmd:

gcc -o test 4_3.c

Does anyone know what can happen? because I copied the example just like in the book.

    
asked by Robby 21.10.2016 в 11:37
source

3 answers

2

You are missing a semicolon ( ; ) at the end of line int k=2 .

/*4_3.c en prueba*/
#include<stdio.h>
int k=5; /*variable global*/

int main(void){ // <--- cambio void por int
void f1(void); /*prototipo de función*/
int i;
 for(i=1;i<=3;i++){
    f1();
 }
 return 0; // <---- agregado
}

void f1(void){
int k=2; /*variable local*/ // <--- aquí
k+=k;
printf("\n\nEl valor de la variable local es: %d", k);
::k = ::k + k;
printf("\nEl valor de la variable global es: %d", ::k);
}

Anyway :: is no operator in C so you should compile with g++ or put the extension .cpp or .C or .c++ to the file to tell gcc to use the C ++ compiler.

Additionally, the correct thing is that main returns int .

    
answered by 21.10.2016 / 11:54
source
1
int k=2 /*variable local*/

That line has to end with a semicolon.

Greetings.

    
answered by 21.10.2016 в 11:41
1

You need a;; always remember to put; C is a bit tedious language sometimes but after you learn this language correctly none will resist you.

    
answered by 21.10.2016 в 12:08