How to use C language variables - global variables?

Asked 1 years ago, Updated 1 years ago, 132 views

I am posting because I have a question about using the C language variable. If I use the code that I'm trying to make as an easy example, (It's not a real code, it's a simple example made with only a part that's similar to the way I'm trying to use it!)

#include <stdio.h>
void people(int n);
void amount(int n);

external int; declare as global variable to use the same n value in //amount and people function

int main(void) {
    people(n);
    amount(n); // recall two functions

    return 0;
}
void people(intn) { // the number of people function
    printf("How many people in total? >> ");
    scanf_s("%d", &n);
}
void amount(intn) { // an amount function that outputs the number of necessary preparations according to the number of people
    printf("Pencils require a total of %d."), n
}

I'm trying to code two functions to use the same variable, but I don't know how to declare n to use itCan't we just declare it as a discharge variable? fatal error LNK1120: 1 unverifiable external reference.The error appears as .

c variable global-variable

2022-09-22 12:35

1 Answers

The outside specifies that the declared content is already defined elsewhere.

If you declare a variable as external int;, n is already defined (generated), and the compiler does not generate that variable.

The link error you mentioned occurs because the main uses a variable that is only declared and does not exist.

Replace external int; with intn;.


2022-09-22 12:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.