Δημοσιεύτηκε: 16 Ιουν 2011, 16:07
από Qdata
Αν χρησιμοποιήσεις int η μεταβλητή ισχύει μόνο για εκείνη την συνάρτηση(local variable - τοπική μεταβλητή):

Κώδικας: Επιλογή όλων
#include <stdio.h>

main()
{
int x = 2;
printf("%d\n", x);
fuction1();
}
fuction1()
{
printf("%d\n", x);
}



Κώδικας: Επιλογή όλων
$ gcc test.c -o test
tsst.c: In function ‘fuction1’:
tsst.c:12:17: error: ‘x’ undeclared (first use in this function)
tsst.c:12:17: note: each undeclared identifier is reported only once for each function it appears in


Ενώ με την οδηγία #define αναγνωρίζετε από κάθε συνάρτηση:

Κώδικας: Επιλογή όλων
#include <stdio.h>
#define x 2

main()
{
printf("%d\n", x);
fuction1();
}
fuction1()
{
printf("%d\n", x);
}


Κώδικας: Επιλογή όλων
$ gcc test.c -o test
$ ./test
2
2


Κατάλαβες; :)