티스토리 뷰
for문을 배워보자
/*
C 마지막 날
2016-03-08
09:01
do while문
do:(정리 필요)
while:(정리 필요)
회사에서 많이 쓰지 않는다.
*/
/*
#include <stdio.h>
int main()
{
do
{
}while();
return 0;
}
*/ |
while문으로 구구단 3단 작성
/*
2016-03-08
09:08
volatile
while문으로 구구단 3단 만들기
*/
#include <stdio.h>
int main()
{
int iCnt;
iCnt = 1;
while(9 >= iCnt)
{
printf("%d * %d = %d\n",3,iCnt,3*iCnt);
iCnt = iCnt + 1; //++iCnt 나 iCnt++ 로도 쓸수 있다.
}
return 0;
}
|
while을 지우고 for문을 대입
#include <stdio.h>
int main()
{
int iCnt;
iCnt = 1;
for(9 >= iCnt)
{
printf(" %d * %d = %d\n",3,iCnt,3*iCnt);
iCnt = iCnt + 1;
}
return 0;
} |
#include <stdio.h>
int main()
{
int iCnt;
for(iCnt = 1; 9 >= iCnt; iCnt = iCnt + 1)//주의: 마지막에는 ;안 붙임
{
printf(" %d * %d = %d\n",3,iCnt,3*iCnt);
}
return 0;
} |
결과는 똑같이 출력된다.
칠판에 있는 1,2,3번을 for문의 ()안에 집어 넣은것
웹컴파일러 : 이전에 작성했던 코드를 붙여넣기하면 실행결과가 같이 나온다.
for문은 안에 내용을 안적으면 while(1)과 같이 무한 반복한다.
#include <stdio.h>
int main()
{
int iCnt;
for(;;)//while(1)과 같다.-무한반복
{
}
return 0;
} |
#include <stdio.h>
int main()
{
volatile int iCnt; //volatile : 휘발성의(임베디드에서 매우 중요)
for(;;)//while(1)과 같다.-무한반복
{
for(iCnt = 0; 100000000 > iCnt; iCnt = iCnt + 1)
{}
printf("test\n");
}
return 0;
} |
안쪽의 for문에서 10000000을 cpu에서 세고난 후에 printf로 test를 출력한다.
volatile을 쓰면 메커니즘의 변화가 있다.
- 0을 초기화
- 참 거짓 비교
- 1 증가
- 2부터 반복
cpu와 memory 간에 데이터의 이동을 의도적으로 지연시킬때 사용(지연용 반복문)
임베디드에선 for문 뒤에 ;를 붙임
for(iCnt=0; 10000000> iCnt; iCnt++);
구조체와 공용체
/*
2016-03-08
11:23
main3.c
union
*/
#include <stdio.h>
/*struct smart
{
int A;
short B;
int C;
char D;
};*/
union smart //union : 공용체(같이 사용)
{
int A;
short B;
int C;
char D;
};
int main()
{
/*struct smart;*/
union smart obj;
obj.A = 0x12345678;
/*printf("size of %d Byte",sizeof(struct smart));*/
printf("size of %d Byte\n",sizeof(union smart));
printf("size of %d \n",sizeof(obj.A));
return 0;
}
/*
2016-03-08
11:23
main3.c
union
*/
#include <stdio.h>
/*struct smart
{
int A;
short B;
int C;
char D;
};*/
union smart //union : 공용체(같이 사용)
{
int A;
short B;
int C;
char D;
};
int main()
{
/*struct smart;*/
union smart obj;
obj.A = 0x12345678;
obj.B = 0x12345678;
obj.C = 0x12345678;
obj.D = 0x12345678;
/*printf("size of %d Byte",sizeof(struct smart));*/
printf("size of %d Byte\n",sizeof(union smart));
printf("size of A %x Byte\n",sizeof(obj.A));
printf("size of B %x Byte\n",sizeof(obj.B));
printf("size of C %X Byte\n",sizeof(obj.C));
printf("size of D %x Byte\n",sizeof(obj.D));
printf("%x\n",obj.A);
printf("%x\n",obj.B);
printf("%x\n",obj.C);
printf("%x\n",obj.D);
return 0;
} |
함수포인터
함수 포인터를 사용하는 이유
①프로그램 코드의 간결
②중복 코드를 줄이기 위해서
③상황에 따라 해당되는 함수를 호출할 수 있음
함수 포인터
① int (*ifunc)(int a);
return값 : int 형
인자 : int a
함수 포인터 명 : ifunc
② char *(*cfunc)(char *p[]);
return값 : char 포인터형
인자 : char형의 포인터 배열
함수 포인터 명 : cfunc
③ void (*vfunc)(void);
return값 : 없음, 혹은 void
인자 : 없음, 혹은 void
함수 포인터 명 : vfunc
④ int (*fptr) (int a, int b);
return값 : int 형
인자 : int a과 int b
함수 포인터 명 : fptr
/*
날짜 : 2016-03-08
작성 시간 : 12:06
함수의 타입을 쉽게 추출하는 방법 - 외울것!
1.함수의 원형을 추출한다.(void smart();)
2.이름있는 곳(smart)을 (*)로 대체한다.
3.대체한 곳을 다시 쓴다.(void (*) (Type);)
+--printf()의 타입------------------------+
|원형 ---> int printf(const char *, ...); |
|--------> int ( * ) (const char *, ...); |
+-----------------------------------------+
+--fopen()의 타입---------------------------------+
|원형 ---> int fopen(const char *,const char *); |
|--------> int ( * ) (const char *,const char *); |
+-------------------------------------------------+
type을 추출하는이유: type을 추출해야 포인터를 쓰기때문임
int A;
int *P;
포인터 선언
Type 이름;
타입을 알아야 함수포인터를 만들수 있다.
*/
#include <stdio.h>
void smart1();
void smart2(); //함수의 원형
int main()
{
void (*test) (); //test는 4바이트 짜리 변수
test = smart1;
test(); //함수주소(); 하면 함수 콜
test = smart2;
test();
return 0;
}
void smart1()
{
printf("1번 함수\n");
}
void smart2()
{
printf("2번 함수\n");
} |
장점 : 같은 포인터로 다른 함수를 사용할수있다.