auto auto auto int number 2 auto include
내부변수 [auto] auto는 자동으로 지정되는 내부변수 로, auto int number = 2; 와 같은 것입니다. 이 auto 변수는 {}밖을 나가면 생존할 수 없습니다. 예를 들어, #include <iostream> using namespace std; void main(){ int num = 2; { cout<<“num : “<<num<<endl; num = 5; cout<<“num : “<<num<<endl; { num+=7; cout<<“num : “<<num<<endl; } } } • #include <iostream> using namespace std; void main(){ int num = 2; { cout<<"num : "<<num<<endl; int num = 5; cout<<"num : "<<num<<endl; { num +=7; cout<<"num : "<<num<<endl; }
#include <iostream> using namespace std; void act(); int a= 20; void main(){ cout<<"a: "<<a<<endl; a = 100; cout<<"a : "<<a<<endl; int a = 50; cout<<"a : "<<a<<endl; act(); cout<<"out a : "<<a<<endl; } 외부변수 - 간단한것 void act(){ cout<<"act a : "<<a<<endl; int a= 200; cout<<"act midish a : "<<a<<endl; }
소스파일을 다음과 같이 만들고 extern 예제 [mainfile. cpp] #include <iostream> using namespace std; void file 1(); void file 2(); int a=100; //외부변수 int b=200; void main(){ cout<<"a : "<<a<<endl; file 1(); file 2(); cout<<"a : "<<a<<endl; cout<<"b : "<<b<<endl; } [file 1. cpp] #include<iostream> using namespace std; extern int a; //정적변수로 선언 void file 1(){ a += 100; cout<<"file 1's a : "<<a<<endl; } [file 2. cpp] #include<iostream> using namespace std; static int b = 400; //정적변수로 선언 void file 2(){ cout<<"file 2's b : "<<b<<endl; }
레지스터 변수 예제 #include<iostream> using namespace std; long int power(register int x, register int n); void main(){ int a = 2, b = 5; cout<<a<<"^"<<b<<" = "<<power(a, b)<<endl; } long int power(register int x, register int n){ register int k; long int p = 1; for(k=1; k<=n; k++) p = p*x; return p; }
- Slides: 18