스레드는 프로그램 내에서 제어의 단일 순차 흐름이다 .

main() 함수에서부터 프로그램이 순차적으로 실행되면서 main() 함수가 종료되면 프로그램도 종료되는것은 단일 스레드 모델이다 

멀티 스레드를 사용하면 프로그램은 main() 함수뿐만 아니라 추가적인 실행 흐름이 있는 것으로 여러 방향에서 순차적으로 프로그램이 실행된다 

1
2
3
4
5
#include <pthread.h>
 
int pthread_create(pthread_t *threadconst pthread_attr_t *attr,
                    void *(*start_routine)(void *), void *arg);
 
cs


첫 번째 인자는 pthread가 성공적으로 리턴이 되면 새로 생성되는 스레드의 스레드ID가 이 포인터가 가리키는 버퍼에 저장될 주소값이다.

두 번째 인자는 스레드의 옵션을 지정할때 사용되는 값인데 기본 옵션으로 스레드를 생성하기 위해서는 NULL 값을 전달하면 된다 .

세 번째 인자는 스레드가 수행할 함수의 포인터를 전달하면 된다

네 번째 인자는 세 번째 인자를 실행할 때 전달해 줄 매개변수이다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
 
void *thread_function(void *param)
{
        while(1) {
                printf("%s\n" , (char*)param);
                sleep(1);
        }
}
 
int main()
{
        pthread_t thread_id;
 
        char *msg = "hi";
 
        pthread_create(&thread_id, NULL, thread_function, msg);
 
        while(1){
                printf("hello!\n");
                sleep(1);
        }
        return 0;
}
 
cs

5행에서 void *을 사용해주는 이유 
어떤 타입을 가진 변수를 전달해야할지 알 수 없으므로 void형으로 통째로 준다

또한 포인터를 이용해서 main에 전달하는 이유는 call by Reference를 참고한다

'c언어' 카테고리의 다른 글

동적 메모리 할당과 해제  (0) 2016.08.10
ANSI C 표준 함수  (0) 2016.08.10
구조체 배열 포인터 중얼-1  (0) 2016.08.10
void형 포인터  (0) 2016.08.09
구조체  (0) 2016.08.08

배열의 크기를 컴파일 하는 시점에서 정하지 않고 프로그램이 실행되는 시점에서 정하려면 메모리 공간을 동적으로 할당받아서 사용해야 한다 .

함수가 호출될 때 사용되는 지역 변수들은 스택 메모리 영역에 할당하지만 동적으로 할당받는 메모리는 힙 메모리 영역에 할당된다.

1
2
#include <stdlib.h>
void *malloc(size_t size);
cs

동적으로 메모리를 할당받기 위해서는 malloc() 함수를 호출한다 .
동적으로 할당받기를 원하는 데이터 크기를 매개변수로 전달하면서 malloc() 함수를 호출하면 동적으로 할당 된 메모리의 시작 주소값이 리턴된다.

할당된 메모리 공간은 직접 해제하지 않으면 프로그램이 종료될 때 할당된 메모리 공간이 해제된다.

할당된 메모리 공간을 해제하지 않고 지속적으로 할당만을 받아서 사용하게 되면 메모리 누스(Memory leak)이 발생하게 된다 .

동적으로 할당된 메모리는 반드시 해제를 해줘야 하며 해제하는 방법은 free() 함수를 호출해야한다 .

1
2
#include <stdlib.h>
void free(void *ptr);
cs

예제 ) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
        char *ptr = (char *)malloc(10);
 
        strcpy(ptr, "Hello");
        printf("%s\n" ,ptr);
 
        free(ptr);
 
        return 0;
}
 
cs
7행에서 메모리를 동적으로 할당을 받았고  9행에서 문자열을 복사 한후 10행에서 출력한다.

12행에서 free 함수를 이용해서 메모리 공간을 해제하였다 .


calloc() 함수 

num * size 바이트의 메모리를 힙 메모리 영역에서 동적으로 할당하고 할당된 메모리의 시작 주소값을 반환한다

1
2
3
#include <stdlib.h>
void *calloc(size_t nmemb, size_t size);
 
cs

예제 )

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int *ptr = (int*)calloc(sizeof(int), 10);
 
        free(ptr);
 
        return 0;
}
 
cs

메모리 영역을 동적으로 할당하는 소스 
자료형의 크기를 * 10 으로 총 40바이트를 할당하고 해제하는 소스이다 .

memeset() 함수 

1
2
3
#include <string.h>
void *memset(void *s, int c, size_t n);
 
cs

메모리 영역을 특정한 값으로 쓰는 함수로 대부분 메모리 영역을 특정한 값으로 초기화 하는데 사용된다.

s부터 n개의 바이트를 c로 채운다 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>
 
int main()
{
        int i;
        char p[5];
        memset(p, 0x11 , 5);
 
        for(i = 0; i < 5; i++) {
                printf("p[%d]:%x\n",i,p[i]);
        }
 
        return 0;
}
 

cs


memcmp() 함수

1
2
#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);

cs


memcmp() 함수는 s1과 s2의 처음 n 바이트를 비교하고 그 차이를 리턴한다 

만약 s1과 s2가 처음부터 n바이트의 데이터가 같을시에 0 을 리턴하고 일치하지 않으면 그 차이를 리턴한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        const char *s1 = "helloworld";
        const char *s2 = "hello";
 
        if(!memcmp(s1,s2,5)) {
                printf("%c %c %c %c %c\n", s1[0],s1[1],s1[2],s1[3],s1[4]);
                printf("%c %c %c %c %c\n", s2[0],s2[1],s2[2],s2[3],s2[4]);
}
        return 0;
}
 
cs

memcpy() 함수

src 주소의 메모리에 있는 데이터를 시작주소에서 n바이트만큼 dest 주소로 복사하는 역할을 한다 


1
2
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);

cs



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
 
int main()
{
    char buf[100];
    char *str = "hello!! wordl!!";
    memcpy(buf, str, strlen(str) +!);
 
    printf("buf :%S\n",buf);
 
    return 0;
        }
cs





'c언어' 카테고리의 다른 글

스레드(Thread) 대충  (0) 2016.08.11
ANSI C 표준 함수  (0) 2016.08.10
구조체 배열 포인터 중얼-1  (0) 2016.08.10
void형 포인터  (0) 2016.08.09
구조체  (0) 2016.08.08

Strlen() 함수 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* #include <string.h>
   size_t strlen(const char *s); */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
        char *str = "hello!! wolrd!!";
 
        printf("str : %lu\n" , (unsigned long)strlen(str));
 
        return 0;
}
 

cs

NULL 문자를 제외한 문자열의 길이를 리턴한다.


strcpy() 함수


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* #include <string.h>
   char *strcpy(char *dest, const char *src) */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
        char buf[100];
        char *str = "hello!! world!!";
 
        strcpy(buf,str);
 
        printf("%s\n" , buf);
 
        return 0;
}
 
cs

NULL 문자를 포함한 문자열을 복사한 후 dest에 저장하는 함수이다     
문자열을 복사한 후에 dest의 주소를 리턴한다.


strcmp() 함수


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* #include <string.h>
   int strcmp(const char *s1 , const char *s2); */
#include <stdio.h>
#include <string.h>
 
        int main()
        {
                char p1[] = "hello";
                char p2[] = "hello";
 
                if(p1 == p2) {
                        printf("p1 == p2\n");
                }else{
                        printf("p1 : %p\n", p1);
                        printf("p2 : %p\n", p2);
                }
 
                if(!strcmp(p1 , p2)) {
                        printf("%s == %s\n", p1, p2);
                }
 
                return 0;
        }
 
cs

두 개의 문자열을 비교한뒤 두 개의 문자열이 일치하면 0을 리턴하고 
문자열 s1이 더 크면 양수를 리턴하고 문자열 s2가 더 크면 음수를 리턴한다 


strcat() 함수 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* #include <string.h>
   char *strcat(char *dest, const char *src); */
 
 
#include <stdio.h>
#include <string.h>
 
int main()
{
        char buf[100= "hello ";
 
        strcat(buf, "world!!");
 
        printf("%s\n",buf);
 
        return 0;
}
 
cs

문자열을 연결하는 함수 dest 문자열의 NULL 문자 위치부터 src 문자열을 합친다.


sprintf() 함수 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* #include <stdio.h>
   int sprintf(char *str, const char *format, ...); */
 
#include <stdio.h>
 
int main()
{
        char buf[1024];
 
        sprintf(buf, "msg : %s""hello!!");
        printf("%s\n", buf);
 
        return 0;
}
 
cs

sptrintf() 와 printf()는 사용법이 동일하다 
다만 그 둘의 차이점은 printf()는 화면으로 출력하는데 반해 sprintf()는 해당 내용을 지정된 버퍼로 출력하는 것이다 .


sscanf() 함수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* #include <stdio.h>
   int sscanf(const char *str, const char *format, ...); */
 
#include <stdio.h>
 
int main()
{
        int i;
        char buf[] = {"11:22:33:44:55:66"};
        int mac[6];
 
        sscanf(buf, "%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
 
        for(i =0; i < 6; i++)
        {
                printf("mac[%d] : %x\n" , i ,mac[i]);
        }
        return 0;
}
 
cs

scanf() 함수는 사용자로부터 데이터를 입력받는 함수지만 sscanf()함수는 지정된 버퍼로부터 내용을 입력받는 함수이다 


isprint()함수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* #include <ctype.h>
   int isprint(int c); */
 
#include <stdio.h>
#include <ctype.h>
 
int main()
{
        int count =0;
        unsigned int i = 0;
        for(i = 0; i<256; i++) {
                if(!isprint(i)) continue;
 
        printf("%d<%c> ",i ,i);
        if(!(++count % 10 )) printf("\n");
 
}
 
printf("\n");
 
return 0;
}
 
cs

isprint() 함수는 화면에 출력 가능한 문자면 0이 아닌 값을 리턴하고, 화면에 출력 불가능한 문자일 경우에는 0을 리턴한다.


exit() 함수 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* #include <stdlib.h>
   void exit(int status); */
 
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int i = 0;
        while(1) {
                i++;
                printf("value : %d\n" , i);
 
                if(i == 5) {
                        exit(0);
                }
        }
        return 0;
}
 
cs

exit() 함수는 호출하는 즉시 프로그램을 종료하는 함수이다 exit() 함수를 호출할 때 전달하는 status 값은 main() 함수가 운영체제(윈도우 명령 프롬프트 또는 리눅스 쉘)에 return 하는 값이다.


'c언어' 카테고리의 다른 글

스레드(Thread) 대충  (0) 2016.08.11
동적 메모리 할당과 해제  (0) 2016.08.10
구조체 배열 포인터 중얼-1  (0) 2016.08.10
void형 포인터  (0) 2016.08.09
구조체  (0) 2016.08.08

struct data

{

char a;

int b;

}


int main()

{

int i;

struct data data_list[] = {

{'A',1},

{'B',2}

};

struct data *pdata = data_list 


이런식으로 pdata에 data_list의 주소를 참조했을때 

pdata[n].멤버 == (pdata + n)->멤버 == (*(pdata +n)).멤버

배열 요소의 위치를 n이라고 했을때 위의 내용은 모두 같은 값을 갖는다 

'c언어' 카테고리의 다른 글

동적 메모리 할당과 해제  (0) 2016.08.10
ANSI C 표준 함수  (0) 2016.08.10
void형 포인터  (0) 2016.08.09
구조체  (0) 2016.08.08
조건부 컴파일  (0) 2016.08.08

Void형 포인터의 특징


1. 모든 데이터의 주소를 가리킬 수 있다.
2. 가리키는 대상이 정해져 있지 않기 때문에 , 사용할 때는 반드시 형변환을 해야 한다.
3. 형변환 없이 직접적으로 역참조 연산('*')을 사용할 수 없다.
4. 형변환 없이 포인터 연산이 불가능하다 . (-- , ++ , +n , -n)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
 
int main()
{
        char a = 'A';
        int b =1;
        float c = 3.14;
        double d = 5.678;
 
        void *p;
 
        printf("void*: %lu\n",(unsigned long)sizeof(p));
 
        p = &a;
        printf("char : %c\n"*(char *)p);
 
        p = &b;
        printf("int : %d\n"*(int *)p);
 
        p = &c;
        printf("float : %f\n"*(float *)p);
 
        p = &d;
        printf("double : %lf\n"*((double *)p));
 
        return 0;
}
 
cs



'c언어' 카테고리의 다른 글

ANSI C 표준 함수  (0) 2016.08.10
구조체 배열 포인터 중얼-1  (0) 2016.08.10
구조체  (0) 2016.08.08
조건부 컴파일  (0) 2016.08.08
정보 올림피아드 - 147  (0) 2016.08.03

struct 구조체명 {

자료형 멤버명;

}

구조체의 기본 구조 

---------------------------------------------------------------------------------------------

struct data {

char a;

int b;

}:

---------------------------------------------------------------------------------------------

구조체를 선언함과 동시에 변수 선언

struct data {

char a;

int b;

}: data_type_1; data_type_2;

---------------------------------------------------------------------------------------------

구조체 타입 data를 정의한 뒤에 data_type_1과 data_Type_2 선언

struct data {

char a;

int b;

}: 

struct data data_type_1; 

struct data data_type_2;

---------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
 
struct data
{
        char a;
        int b;
};
 
int main()
{
        struct data data_type_var;
 
        printf("char : %lu Byte\n", (unsigned long)sizeof(char));
        printf("int : %lu Byte\n", (unsigned long)sizeof(int));
        printf("struct data : %lu Byte\n",(unsigned long)sizeof(struct data));
 
        data_type_var.a = 'A';
        data_type_var.b = 100;
 
        printf("data_type_var.a : %c\n",data_type_var.a);
        printf("data_tyoe_var.b : %d\n",data_type_var.b);
 
        return 0;
}
 
cs

struct 에서는 char를 4byte로 처리하기 때문에 struct data에서 4+4 = 8이 나오게된다.
소스 코드를 컴파일할 때 해당 구조체의 크기를 정렬하여 컴파일하기 떄문이다 
이를 구조체 멤버정렬(struct member alignment) 라고 한다 .
소스 코드를 컴파일 할 때 구조체 멤버 정렬이 발생하는 이유는 CPU에서 메모리에 접근 할 때 특정한 사이즈로 접근을 해야 더욱 속도가 빠르기 때문이다.
멤버 정렬을 하면서 정렬 사이즈 단위에 부족하면 부족한 해당 바이트 값만큼 패딩을 추가하여 정렬 사이즈를 맞춘다 ( 패딩 = 남은 공간을 채우는것)

( %lu = unsigned long 이라는 의미이며 ul로 쓰면 에러가 발생할 수 있다한다.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
#include <stdio.h>
 
struct data
{
        char a;
        int b;
}__attribute__((packed));
 
int main()
{
        struct data data_type_var;
 
        printf("char : %lu Byte\n", (unsigned long)sizeof(char));
        printf("int : %lu Byte\n", (unsigned long)sizeof(int));
        printf("struct data : %lu Byte\n",(unsigned long)sizeof(struct data));
 
        data_type_var.a = 'A';
        data_type_var.b = 100;
 
        printf("data_type_var.a : %c\n",data_type_var.a);
        printf("data_tyoe_var.b : %d\n",data_type_var.b);
 
        return 0;
}
cs

위 소스와 달라진 점은 struct(구조체)가 끝나는 시점에 __attribute__((packed));
를 추가해줬다는 것이다 . 의미는 구조체를 정의할때 패딩 없이 채운다는 의미이다.
리눅스 gcc환경에서 가능하고 윈도우(vc) 환경에서는 
#pragma pack(push, 1)
struct data{
    char a;
    int b;
}:
#pragma pack(pop)
를 추가한다

struct data 에서 char 1 int 4 를 해서 5 Byte가 출력되는것을 확인할 수 있다 

구조체 초기화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
 
struct data
{
        char a;
        int b;
        float c;
};
 
int main()
{
        struct data a ={'B'101 , 1.2};
 
        printf("a.a : %c, a.b: %d, a.c : %f\n" , a.a , a.b , a.c);
 
        return 0;
}
 
cs

---------------------------------------------------------------------------------------------비트 필드 

기본 자료형에서 가장 작은 char 형도 1바이트를 차지한다. 
그보다 더 작게 비트로 어떻게 선언할 수 있을까 ? 

struct 구조체에서 몇 비트를 사용할 것 인지 선언을 한다 
main() 내부에서 얼마나 큰 수 (10진수)를 사용할 것인지 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
 
struct data
{
        unsigned char a:2;
        unsigned char b:2;
        unsigned char c:4;
};
 
int main()
{
        struct data a;
 
        a.a = 2;
        a.b = 1;
        a.c =1;
 
        printf("struct data : %lu\n" , (unsigned long)sizeof(struct data));
 
        printf("a.a : %d\n" , a.a);
        printf("a.b : %d\n" , a.b);
        printf("a.c : %d\n" , a.c);
 
        return 0;
}
 
cs

2^2 혹은 2^4 이상의 수를 주면 오버 플로우가 발생한다 

---------------------------------------------------------------------------------------------공용체 


공용체는 구조체와 비슷한 문법을 갖고 있고 형태도 비슷하지만 구조체와 달리 멤버들이 하나의 메모리 공간을 공유해서 사용한다는 점에서 다른 특징을 갖고 있다.

ex ) 구조체 = 선언되어 있는 자료형의 크기를 더한것이 구조체의 크기 
      공용체 = 선언되어 있는 멤버 중에서 가장 큰 메모리 공간을 차지하는 변수의 크기가 메모리의 크기가 됨.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 #include <stdio.h>
  2 
  3 union data
  4 {
  5         char a;
  6         int b;
  7 };
  8 
  9 int main(void)
 10 {
 11         union data data_type_var;
 12 
 13         printf("union data : %luByte\n",(unsigned long)sizeof(union data));
 14         printf("union data a : %luByte\n",(unsigned long)sizeof(data_type_var.a));
 15         printf("union data b : %luByte\n",(unsigned long)sizeof(data_type_var.b));
 16 
 17         data_type_var.b = 0x11223344;
 18 
 19         printf("data_type_var.a : %x\n" , data_type_var.a);
 20         printf("data_type_var.b : %x\n" , data_type_var.b);
 21 
 22         data_type_var.a = 0x55;
 23 
 24         printf("data_type_var.a : %x\n" , data_type_var.a);
 25         printf("data_type_var.b : %x\n" , data_type_var.b);
 26 
 27         return 0;
 28 }
 29 
 30 
 
cs

 

union data는 크기가 가장 큰 b의 4BYTE를 따라가고 있다 .

17행에서 data_type_var.b = 0x11223344;

를 선언하였고 var.a에서 44를 나타내는걸 보면 같은 메모리를 공유하고 있는 것을 확인 할 수 있다 .

22행에서 data_type_var.a = 0x55;

를 선언하였고 var.a 결과가 55 나왔다. 
var.b의 값이 11223355가 나왔고 같은 메모리를 공유하고 있는 것을 확인 할 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
 
struct data
{
        union {
                struct {
                        char data_a;
                        int data_b;
                };
 
                struct {
                        char a;
                        int b;
                };
        };
};
 
int main(void)
{
        struct data data;
 
        data.a = 1;
        data.data_b = 2;
 
        printf("data.data_a : %d\n", data.data_a);
        printf("data.data_b : %d\n", data.data_b);
 
        printf("data.a : %d\n", data.a);
        printf("data.b : %d\n", data.b);
 
        return 0;
}
 
cs

다른 구조체이지만 한 공용체 안에 들어있기 때문에 같은 메모리 공간을 공유하고있다.

그렇기 떄문에 서로 다른 변수 이름으로 같은 값을 갖게 된다 .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
#include <stdio.h>
 
enum day {MON, TUE, WED, THU, FRI, SAT, SUN};
enum test {A=100,B=103,C};
 
int main()
{
        enum day d;
 
        d = MON;
 
        printf("MON : %d, TUE : %d, WED : %d, THU : %d , FRI : %d , SAT : %d , SUN : %d\n",MON,TUE,WED,THU,FRI,SAT,SUN);
 
        printf("A : %d , B : %d , C : %d\n" , A ,B ,C);
 
        switch(d)
        {
                case MON:
                     printf("Ang\n");
                     printf("\n");
                     break;
                case TUE:
                     break;
                case WED:
                     break;
                case THU:
                     break;
                case FRI:
                     break;
                case SAT:
                     break;
                case SUN:
                     break;
        }
        return 0;
}
 
cs



'c언어' 카테고리의 다른 글

구조체 배열 포인터 중얼-1  (0) 2016.08.10
void형 포인터  (0) 2016.08.09
조건부 컴파일  (0) 2016.08.08
정보 올림피아드 - 147  (0) 2016.08.03
정보 올림피아드 - 146  (0) 2016.08.02

#if              조건

#ifdef           정의되었을 경우

#ifndef         정의되지 않았을 경우

#else           그외의 경우

#elif            그 외의 조건일 경우

#endif          조건문 전처리기의 끝


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#include <stdio.h>
 
#define VERSION 1
 
int main(void)
{
 
#ifdef VERSION
        #if VERSION <2
                printf("VERSION >2\n");
        #else
                printf("VERSION :%d\n",VERSION);
        #endif
#else
        printf("Unknown VERSION\n");
#endif
        return 0;
}
 
cs
<그림 1 >버전확인 



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
int main(void)
{
#ifndef __linux__
        printf("not linux\n");
#else
        printf("linux\n");
#endif
 
        return 0;
}
 
cs

<그림 2> 리눅스인지 확인

__linux__는 linux인지 아닌지 확인하는 매크로임


리눅스임


1
2
3
4
5
6
7
8
#ifndef _18_H
#define _18_H
struct data
{
        int a;
};
#endif
 
cs

<그림.3> ex.5.h

1
2
3
4
5
6
7
8
#include "ex.5.h"
#include "ex.5.h"
 
int main(void)
{
        return 0;
}
 

cs
f


<그림 4> ex.5.c

'c언어' 카테고리의 다른 글

void형 포인터  (0) 2016.08.09
구조체  (0) 2016.08.08
정보 올림피아드 - 147  (0) 2016.08.03
정보 올림피아드 - 146  (0) 2016.08.02
정보 올림피아드 - 145  (0) 2016.08.02


자연수 n을 입력받아 "출력 예"와 같이 공백으로 구분하여 출력되는 프로그램을 작성하시오. 
주의! 숫자는 공백으로 구분하되 줄사이에 빈줄은 없다.



 [Copy]
3
 [Copy]
1 2 3
  4 5
    6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
int main() {
    int a, b, c,d=1;
    scanf("%d"&a);
    for (b = 1; b <= a; b++) {
        for (c = 0; c < b-1; c++) {
            printf(" ");
            printf(" ");
        }
        for (c = a; c >= b; c--) {
            printf("%d ", d);
            d++;
        }printf("\n");
    }
    return 0;
}
cs


'c언어' 카테고리의 다른 글

구조체  (0) 2016.08.08
조건부 컴파일  (0) 2016.08.08
정보 올림피아드 - 146  (0) 2016.08.02
정보 올림피아드 - 145  (0) 2016.08.02
정보 올림피아드 - 144  (0) 2016.08.02


자연수 n(n≤6)을 입력받아 "출력 예"와 같이 공백으로 구분하여 출력되는 프로그램을 작성하시오.
주의! 문자는 공백으로 구분하되 줄사이에 빈줄은 없다.



 [Copy]
3
 [Copy]
A B C
D E 0
F 1 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main() {
    int a, b, c;
    int d = 65;
    int e = 0;
    scanf("%d",&a);
    if (a <= 6) {
        for (b = 0; b < a; b++) {
            for (c = a; c > b; c--) {
                printf("%c ", d);
                d++;
            }
            for (c = 0; c < b; c++) {
                printf("%d ", e);
                e++;
            }printf("\n");
        }
    }
    else return 1;
    return 0;
}
cs


'c언어' 카테고리의 다른 글

조건부 컴파일  (0) 2016.08.08
정보 올림피아드 - 147  (0) 2016.08.03
정보 올림피아드 - 145  (0) 2016.08.02
정보 올림피아드 - 144  (0) 2016.08.02
정보 올림피아드 - 143  (0) 2016.08.02


자연수 n을 입력받아 "출력 예"와 같이 공백으로 구분하여 출력하는 프로그램을 작성하시오.
주의! 숫자를 공백으로 구분하되 줄사이에 빈줄은 없다.



 [Copy]
3
 [Copy]
    1
  1 2
1 2 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main() {
    int a, b, c;
    int d = 1;
    scanf("%d"&a);
    for (b = 1; b <= a; b++) {
        for (c = a; c > b; c--) {
            printf(" ");
            printf(" ");
        }
        for (c = 0; c < b; c++) {
            printf("%d" , d+c);
            printf(" ");
        }printf("\n");
    }
    return 0;
}
cs


'c언어' 카테고리의 다른 글

정보 올림피아드 - 147  (0) 2016.08.03
정보 올림피아드 - 146  (0) 2016.08.02
정보 올림피아드 - 144  (0) 2016.08.02
정보 올림피아드 - 143  (0) 2016.08.02
정보 올림피아드 - 142  (0) 2016.08.02

+ Recent posts