์ƒˆ์†Œ์‹

๐Ÿ‘ฉ‍๐Ÿ’ป C

C ์–ธ์–ด - ๋ฌธ์ž์—ด ์ด์ •๋ฆฌ | ๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜ , ๋ฒ„ํผ (buffer) ํฌ์ธํ„ฐ ๋ฌธ์ž์—ด (pointer) , ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ (scanf , EOF ,strlen)

  • -

*๋ฌธ์ž์—ด (string)

 

๋ฌธ์ž๋Š” char๋กœ ์„ ์–ธํ•œ๋‹ค. ํ•œ๊ธ€์ž๊ฐ€ ์•„๋‹Œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ๋ฌธ์ž์—ด ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ๋ฌธ์ž์—ด์„ ์„ ์–ธ(์ €์žฅ)ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€์ด๋‹ค.

1) ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.  char *๋ณ€์ˆ˜์ด๋ฆ„ = "๋ฌธ์ž์—ด";

2) ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค.  char ๋ณ€์ˆ˜์ด๋ฆ„[size] = "๋ฌธ์ž์—ด";        (size๋ฅผ ๋น„์›Œ๋‘๋ฉด ์•Œ์•„์„œ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋งŒํผ ํ• ๋‹น๋œ๋‹ค.)

  • 1)์€ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ํฌ์ธํ„ฐ์— ํ• ๋‹นํ•˜์ง€๋งŒ, 2)๋Š” ๋ฐฐ์—ด ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜์— ๋ฌธ์ž๊ฐ€ ์ €์žฅ๋œ๋‹ค.
  • ํฌ์ธํ„ฐ๋กœ ์„ ์–ธํ•˜๋˜, ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•˜๋˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์—๋Š” ๊ณต๋ฐฑ(NULL)์ด ๋“ค์–ด๊ฐ„๋‹ค.
  • ๋ฌธ์ž์—ด ๋ณด๋‹ค , ๋ฐฐ์—ด์„ ๋” ํฐ size๋กœ ์„ ์–ธํ•œ๋‹ค๋ฉด, ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ  ๋‚จ๋Š” ๋‚˜๋จธ์ง€๊ณต๊ฐ„์€ NULL ๋กœ ์ฑ„์›Œ์ง„๋‹ค. 
  • int ๋ฐฐ์—ด๊ณผ๋Š” ๋‹ฌ๋ฆฌ, ๋ฌธ์ž์—ด ๋ฐฐ์—ด์€ ์„ ์–ธ ์ฆ‰์‹œ ๋ฌธ์ž์—ด๋กœ ์ดˆ๊ธฐํ™” ํ•ด์•ผํ•œ๋‹ค.( ๋ฏธ๋ฆฌ ์„ ์–ธํ•ด๋†“๊ณ  ๋‚˜์ค‘์— ํ• ๋‹นํ•˜๋Š”๊ฒƒ์ด ์•ˆ๋œ๋‹ค.) -> ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ธ๋ฑ์Šค๋ฅผ ์„ ํƒํ•˜์—ฌ ์ง‘์–ด๋„ฃ๋Š”๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜๋‹ค. string[1] = "w", string[2] = "a";์ฒ˜๋Ÿผ...
  • ์ปดํ“จํ„ฐ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์ƒ์—์„œ ๋งˆ์ง€๋ง‰์„ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด, ๋งˆ์ง€๋ง‰์—๋Š” Null๊ฐ’์„ ํฌํ•จํ•œ๋‹ค. (null์€ \0์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.) ์˜ˆ๋ฅผ ๋“ค๋ฉด printf()ํ•จ์ˆ˜๋„ null์„ ๋งŒ๋‚˜๋ฉด ์ถœ๋ ฅ์„ ๋ฉˆ์ถ”๋Š” ์›๋ฆฌ์ด๋‹ค.
ํฌ์ธํ„ฐ๋กœ ๋ฌธ์ž์—ด ์„ ์–ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜๊ธฐ
#include <stdio.h>
int main(){
	char *string_1 = "Hello world";
    printf("%s", string_1);
    return 0;
}

์ถœ๋ ฅํ• ๋•Œ ์„œ์‹ ์ง€์ •์ž๋Š” string ์˜ ์•ฝ์ž์ธ s์ด๋‹ค. -> %s

 

ํฌ์ธํ„ฐ๋กœ ๋ฌธ์ž์—ด ํฌ์ธํ„ฐ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๊ธฐ

 

#include <stdio.h>
int main(){
	char *string_1 = "abcd";
    printf("%c\n",string_1[0]);
    printf("%c\n",string_1[4]);
    
    return 0;
}
  • ๋ฌธ์ž์—ด๋„ ๋ฌธ์ž๋“ค์˜ ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. 
  • "abcd"์ด๋ฏ€๋กœ, ๋ฌธ์ž๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ธ๋ฑ์Šค๋Š” string_1[3] ๊นŒ์ง€์ด๋‹ค. string_1[4]์—๋Š” NULL์ด ๋“ค์–ด์žˆ๋‹ค. (ํ•ญ์ƒ ๋ฌธ์ž์—ด์˜ ๋งจ ๋’ค์—๋Š” NULL ์ด ๋“ค์–ด์žˆ๋‹ค.)

 

 

*๋ฌธ์ž์—ด ์ž…๋ ฅ๋ฐ›๊ธฐ (scanf) 

scanf ์—์„œ, ์„œ์‹์ง€์ •์ž๋ฅผ %s๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ๊ฐ’์„ ๋ฐฐ์—ด ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋งŒ, ์ž…๋ ฅํ• ๋•Œ ๋„์–ด์“ฐ๊ธฐ(๊ณต๋ฐฑ)์ด ์žˆ๋‹ค๋ฉด, ๊ณต๋ฐฑ ๋ฐ”๋กœ ์ง์ „๊นŒ์ง€๋งŒ ์ž…๋ ฅ๋˜์–ด ์ €์žฅ๋œ๋‹ค. ๊ณต๋ฐฑ์„ ๋งŒ๋‚˜๋ฉด ๋ฌธ์ž์—ด์˜ ๋์ด๋ผ๊ณ  ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์—..

#include <stdio.h>
int main(){
	char s1[10] ;
    printf("์ž…๋ ฅ ํ•˜์„ธ์š”");
    scanf("%s",s1);
    printf("%s", s1);
    return 0;
}
  • (์ค‘์š”) ํฌ์ธํ„ฐ ๋ฌธ์ž์—ด์€ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์ž…๋ ฅ ๊ฐ’์„ ๋ฌธ์ž์—ด ํฌ์ธํ„ฐ์— ์ €์žฅํ•˜๋ ค๋ฉด, malloc์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ํ•œ ๋’ค์— ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

*EOF (End of File) ์™€ ๊ณต๋ฐฑ 

  • ํŒŒ์ผ์˜ ๋ ) ๋” ์ด์ƒ ์ฝ์„ ๊ฐ’์ด ์—†๋Š” ์ƒํƒœ๋ฅผ ๋งํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ฝ˜์†”์ฐฝ์—์„œ๋Š” Ctrl+Z (window) | Ctrl+D (linux)๋ฅผ EOF๋ผ๊ณ  ์ •ํ•ด๋†“์•˜๋‹ค.
  • stdio.h์— ์ •์˜๋˜์–ด์žˆ๋Š” EOF๋Š” ํŒŒ์ผ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๊ฐ€ ์‹คํŒจํ–ˆ์„๋•Œ -1์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
EOF(end of file)
#include <stdio.h>
int main(){
	int c = getchar(); // ๋ฌธ์ž 1๊ฐœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
    printf("%d\n", c);
    printf("%d\n", EOF);

    return 0;
}


์ž…๋ ฅ >>>>> ctrl+z
์ถœ๋ ฅ >>>>> -1       -1

 

๋”ฐ๋ผ์„œ, ์ž…๋ ฅ์„ ๋ฐ›๋‹ค๊ฐ€ ๊ณต๋ฐฑ์„ ๋งŒ๋‚˜๋ฉด ๋ฌธ์ž๋ฅผ ์ฝ๋Š”๊ฒƒ์„ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค. ์ด๋•Œ ์„œ์‹์ง€์ •์ž %[^\n]s ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ณต๋ฐฑ๊นŒ์ง€ ์ž…๋ ฅ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

#include <stdio.h>
int main(){
	char s1[30];
	printf("what is your name?");
    scanf("%[^\n]s", s1);
    printf("%s\n",s1);
    }

 

 

 

 

 

 

 

 

*๋ฒ„ํผ (buffer)

ํŠน์ •ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์˜๋„ํ•˜์ง€ ์•Š์•„๋„ C์–ธ์–ด ์ž์ฒด์ ์œผ๋กœ ๋ฒ„ํผ๋ฅผ ํ†ตํ•˜์—ฌ

์ž…๋ ฅ ์ถœ๋ ฅ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

 

 

*์ž…๋ ฅ ๋ฒ„ํผ๋กœ ์ธํ•œ ์˜ค๋ฅ˜ ์˜ˆ์ œ

#include <stdio.h>
int main(void){
    int a;
    char c;
    scanf("%d", &a);
    printf("%d\n", a);
    scanf("%c", &c);
    printf("%c\n", c);
    return 0;
}   // ์—”ํ„ฐ๋ฅผ ์ณค์„๋•Œ \n ์ด ๋ฌธ์ž๋กœ ์ธ์‹๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ž…๋ ฅ์„ ํ•œ๋ฒˆ๋ฐ–์— ์•ˆ๋ฐ›๋Š”๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค.

 

*๋‚จ์•„์žˆ๋Š” ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ์ง€์šฐ๊ธฐ ์œ„ํ•œ ์˜ˆ์ œ

#include <stdio.h>

int main(void){
    int a; char c;
    scanf("%d", &a);
    printf("%d\n",a);
    int temp;
   
    while((temp=getchar()) != EOF && temp!= '\n') {}
    scanf("%c", &c),
    printf("%c\n",c);
    return 0;
}

ํ•œ๊ธ€์ž์”ฉ ๋ฐ›์•„์„œ ํŒŒ์ผ์˜ ๋(EOF= end of file)์ด๊ฑฐ๋‚˜ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ๋งŒ๋‚˜๋ฉด ์ž…๋ ฅ์„ ๋ฉˆ์ถ”๋ฏ€๋กœ, ํ•ญ์ƒ ์ž…๋ ฅ๋ฒ„ํผ๋ฅผ ๋น„์›Œ๋†“์„์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

 

 

 

 

*๋ฌธ์ž์—ด๊ณผ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋“ค

 

strlen(<๋ฌธ์ž์—ด>) :๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • string length์˜ ์ค„์ž„๋ง์ด๋‹ค. string.h ํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
  • ๋ฌธ์ž์—ด ๋งˆ์ง€๋ง‰์— NULL์€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์™€๋Š” ๋ฌด๊ด€ํ•˜๊ฒŒ, ์ˆœ์ˆ˜ํ•˜๊ฒŒ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    #include <stdio.h>
    #include <string.h>
    int main(void){
        char a[20] = "Apple banana";
        printf("string's length: %d\n", strlen(a));
        return 0;
        }

 

strcmp(๋ฌธ์ž์—ด1, ๋ฌธ์ž์—ด2) : ๋ฌธ์ž์—ด์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•œ๋‹ค.

  • string compare (๋ฌธ์ž์—ด ๋น„๊ตํ•˜๋‹ค)์˜ ์ค„์ž„๋ง์ด๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ string.h ํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
  • ๋ฌธ์ž์—ด์ด ๊ฐ™๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด1์ด ASCII์ฝ”๋“œ์ƒ์œผ๋กœ ๋” ํฌ๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (์ฆ‰, ๋ฌธ์ž์—ด1์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ๋’ค์— ์žˆ์œผ๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.)
  • ๋ฌธ์ž์—ด2๊ฐ€ ASCII์ฝ”๋“œ์ƒ์œผ๋กœ ๋” ํฌ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (์ฆ‰, ๋ฌธ์ž์—ด2๊ฐ€ ์‚ฌ์ „์ˆœ์œผ๋กœ ๋’ค์— ์žˆ์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.)
#include <stdio.h>
#include <string.h>
int main(void){
    char a[20] = "apple banana";
    char b[20] = "you jun young";
    printf("string a,b์˜ ์‚ฌ์ „์ˆœ ๋น„๊ต :%d",strcmp(a,b));
    return 0;
}

 

 

 strcpy(๋ฌธ์ž์—ด1, ๋ฌธ์ž์—ด2) : ๋ฌธ์ž์—ด2๋ฅผ ๋ฌธ์ž์—ด 1์— ๋ณต์‚ฌํ•ด์„œ ๋„ฃ๋Š”๋‹ค. 

  • string copy์˜ ์ค„์ž„๋ง์ด๋ฉฐ, string.h ํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
  • ์„ฑ๊ณต์ ์œผ๋กœ ๋ณต์‚ฌํ•ด์„œ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์— ๋ถ™์–ด์žˆ๋Š” ๋งˆ์ง€๋ง‰ NULL ๊นŒ์ง€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์—ฌ์•ผํ•œ๋‹ค.
  • ํฌ์ธํ„ฐ ๋ฌธ์ž์—ด์€ ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค. ๋”ฐ๋ผ์„œ strcpyํ•จ์ˆ˜์—๋„ ๋ฏธ๋ฆฌ ์„ ์–ธ๋œ ํฌ์ธํ„ฐ ๋ฐฐ์—ด์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • ๋งŒ์•ฝ, ํฌ์ธํ„ฐ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, malloc์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•œ ๋’ค์— strcpy๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ์–ด์•ผํ•œ๋‹ค.
#include <stdio.h>
#include <string.h>
int main(void){
    char a[20] = "My name";
    char b[20] = "is sam";
    strcpy(b,a);
    printf("๋ณต์‚ฌ๋œ ๋ฌธ์ž์—ด: %s\n",b);
    return 0;
}

 

 

 

strcat(๋ฌธ์ž์—ด1, ๋ฌธ์ž์—ด2) : ๋ฌธ์ž์—ด 1์— ๋ฌธ์ž์—ด 2๋ฅผ ๋”ํ•œ๋‹ค. (์ด์–ด ๋ถ™์ธ๋‹ค)

  • string concatenate ์˜ ์ค„์ž„๋ง. string.h ํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
  • ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ด๋”๋ผ๋„ ๋ชจ์ž๋ผ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
#include <stdio.h>
#include <string.h>
int main(void){
    char a[20] = "my name is";
    char b[20] = " banana";
    strcat(a,b);
    printf("%s\n",a);
    return 0;
}

strchr(๋ฌธ์ž์—ด1, ๋ฌธ์ž1) : ๋ฌธ์ž์—ด1์—์„œ , ๋ฌธ์ž1๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ์œ„์น˜(ํฌ์ธํ„ฐ) ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • string character ์˜ ์ค„์ž„๋ง, string.h ํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
  • strchr์€ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘(์™ผ์ชฝ)๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•˜์ง€๋งŒ, strrchr์€ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
#include <stdio.h>
#include <string.h>
int main(){
    char a1[20] = "my name is jy";
    char *ptr = strchr(a1, 'i');
    printf("%s", ptr);
}

์ถœ๋ ฅ >>>>>> is jy

 

 

strstr(๋ฌธ์ž์—ด1, ๋ฌธ์ž์—ด2) : ๋ฌธ์ž์—ด 1์— ๋ฌธ์ž์—ด 2๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€, ์žˆ๋‹ค๋ฉด ๊ทธ ์ฃผ์†Œ๋ถ€ํ„ฐ ๋’ค๊นŒ์ง€ ์ญ‰ ๋ฐ˜ํ™˜๋œ๋‹ค.

  • string string์˜ ์ค„์ž„๋ง , string.hํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
#include <stdio.h>
#include <string.h>
int main(void){
    char a[20] = "My name is apple";
    char b[20] = "is";
    printf("์ฐพ์€ ๋ฌธ์ž์—ด : %s", strstr(a,b));
    return 0;
}

์ถœ๋ ฅ >>>>>> ์ฐพ์€ ๋ฌธ์ž์—ด : is apple

 

๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ, "n"์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๊ธฐ.
#include <stdio.h>
#include <string.h>

int main()
{
    char str1[30] = "abcdefg abcdef abcd abc ab a";
    char *pointer = strchr(str1, 'b');
    while(pointer != NULL)
    {
        printf("%s\n", pointer);
        pointer = strchr(pointer + 1, 'b');
    }
    
    return 0;
}

 

 

sprintf(๋ฐฐ์—ด , ์„œ์‹, ๊ฐ’):์›ํ•˜๋Š” ์„œ์‹๋Œ€๋กœ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•œ๋‹ค.

  • ์•„๋ž˜ ์ฝ”๋“œ์—์„œ , ๋ฐฐ์—ด s1์€ sprintfํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋นˆ ๋ฐฐ์—ด์ด์—ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•  ๋นˆ ๋ฐฐ์—ด์„ ๋ฒ„ํผ(buffer)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
  • ํฌ์ธํ„ฐ ๋ฐฐ์—ด์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
#include <stdio.h>
int main(){
	char s1[20];
    sprintf(s1, "I'm %d years old", 17);
    
    printf("%s",s1);
    return 0 ;
    }

 

 

strtok(๋Œ€์ƒ๋ฌธ์ž์—ด, ๊ธฐ์ค€๋ฌธ์ž):๊ธฐ์ค€๋ฌธ์ž๋กœ ๋ฌธ์ž์—ด์„ ์ž๋ฅธ๋‹ค.

  • string tokenize ์˜ ์ค„์ž„๋ง, string.h ํ—ค๋”ํŒŒ์ผ์— ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.
  • ์˜ˆ๋ฅผ๋“ค์–ด "the red apple" ์„ " " ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅด๋ฉด, 3๊ฐœ๋กœ ์ชผ๊ฐœ์–ด์ง„๋‹ค.
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.