본문 바로가기
코스웨어/16년 스마트컨트롤러

20160308-이보원 제어언어(그 밖의 기초 학습)

by 알 수 없는 사용자 2016. 3. 9.
728x90
반응형

----------------------------while 사용 구구단 출력 연습-----------------------------

#include<stdio.h>

int main()

{
 int iCnt;
 int iDan;
 iCnt=1;
 iDan=3;
 
  while(9>=iCnt)
  {
   printf("%dx%d=%d\n",iDan,iCnt,iDan*iCnt);
   iCnt=iCnt+1;
  }
 
 
 return 0;
}


Output:

1
2
3
4
5
6
7
8
9
3x1=3
3x2=6
3x3=9
3x4=12
3x5=15
3x6=18
3x7=21
3x8=24
3x9=27

----------------------------------for 사용 구구단 출력------------------------------

#include<stdio.h>

int main()

{
 int iCnt;
 int iDan;
 
 iDan=3;
 
  for(iCnt=1;10>iCnt;iCnt=iCnt+1)
  {
   printf("%dx%d=%d\n",iDan,iCnt,iDan*iCnt);
   
  }
 
 
 return 0;
}


Output:

1
2
3
4
5
6
7
8
9
3x1=3
3x2=6
3x3=9
3x4=12
3x5=15
3x6=18
3x7=21
3x8=24
3x9=27


 

------------------------------------무한 반복문 출력-------------------------------

#include<stdio.h>

int main()
{
 volatile int iCnt;
 for (;;)//while(1)과 같다 무한반복
 {
  for(iCnt=0;100000000>iCnt;iCnt=iCnt+1);//iCint=iCnt+1 == ++iCint,iCnt++과 같다
  {}
  //iCnt=10억;
  printf("test\n");  
 }
 return 0

}

 

--------------------------------union 사용한 출력---------------------------------

#include<stdio.h>


union smart

//struct smart
{
	int A;
	short B;
	int C;
	char D;
};

int main()

{	
	union smart obj;

	obj.A=0x12345678;
	obj.B=0x12345678;
	obj.C=0x12345678;
	obj.D=0x12345678;

	//printf("size of smart:%d\n",sizeof(struct smart));
	printf("size of smart:%d\n",sizeof(union smart));

	printf("%x\n",obj.A);
	printf("%x\n",obj.B);
	printf("%x\n",obj.C);
	printf("%x\n",obj.D);
	return 0;
}


Output:

1
2
3
4
5
size of smart:4
12345678
5678
12345678
78

-union-공용체라한다.

>메모리 절약을 위해 같은 메모리 공간을 여러 변수가 공용으로 사용하는 것이다.

 

---------------------------------함수 포인터(함수에 type)---------------------------

#include<stdio.h>

void smart1()
{
     printf("1번 함수\n");
}

void smart2()
{
     printf("2번 함수\n");
}

int main()
{
 void (*tset)();//smart1을 호출
 tset=smart1;
 tset();
 void (*tset)();//smart2을 호출
 tset=smart2;
 tset();
 return 0;

}

>함수 포인터를 사용하는 이유

①프로그램 코드의 간결
②중복 코드를 줄이기 위해서
③상황에 따라 해당되는 함수를 호출할 수 있음

>함수 포인터(함수에 type)

-type 알기-

1.void smart1();//함수의 원형

2.void (*)

3.void(*)(type)

type 이름;

1.void (*test)();

2.test=smart;-주소

3.test();-주소

-함수의 메모리 주소를 가리키는 포인터

변수를 선언하면 메모리 공간에 할당되면서 메모리 주소를 가지듯이 함수도 마찬가지로

메모리 공간에 할당되면서 메모리 주소를 가진다.

728x90