๋ฐ๋ฐฐ์จ
๋ฐ๋ฐฐ์จ 4.1 ~
์์ณ์ด
2021. 1. 7. 20:12
4.1 ๋ฌธ์์ด ์ ์ถ๋ ฅํ๊ธฐ
#define_CRT_SECURE_NO_WARNINGS
#inclide <stdio.h>
int main()
{
char fruit_name[40];
printf("What is your favorate fruit?\n");
scanf("%s", fruit_name); // be careful with &
printf("you like %s!\n", fruit_name);
return 0;
}
char fruit_name; ์ด
char fruit_name[40]; ์ฆ ๋ฌธ์์ ๋ฐฐ์ด๋ก ๋ฐ๋๋ฉด์
scanf("%c", &fruit_name); ์์
scanf("%s", fruit_name); ๋ก ๋ฐ๋๋ค. &๊ฐ ์ฌ๋ผ์ง ์ด์ ๋ fruit_name ์ด ์ฃผ์๊ฐ ๋๊ธฐ ๋๋ฌธ
4.2 sizeof ์ฐ์ฐ์
์๋ฃํ์ ํฌ๊ธฐ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#inclide <stdlib.h>
struct MyStruct
{
int i;
float f;
}
int main()
{
/* 1. sizeof basic types */
int a = 0;
unsigned int int_size1 = sizeio a;
unsigned int int_size2 = sizeof(int);
unsigned int int_size3 = sizeof(a);
size_t int_size4 = sizeof(a);
size_t float_size = sizeof(float);
printf("Size of int type is %u bytes.\n", int_size1);
printf("Size of int type is %zu bytes.\n", int_size4);
printf("Size of float type is %zu bytes.\n", float_size);
/* 2. sizeof arrays */
int int_arr[30]; // int_arr[0] = 1024; ....
int *int_ptr = NULL;
int_ptr = (int*)malloc(sizeof(int) * 30); // int_ptr[0] = 1024; ...
printf("Size of array = %zu bytes\n", sizeof(int_arr)); // 120 bytes
printf("Size of pointer = %zu bytes\n", sizeof(int_ptr)); // 4 bytes
/* 3. sizeof character array */
char c = 'a';
char string[10]; // maximally 9 character + '/0' (nullcharacter)
size_t char_size = sizeof(char);
size_t str_size = sizeof(string);
printf("Size of char type is %zu bytes.\n", char_size); // 1 bytes
printf("Size of string type is %zu bytes.\n", str_size); // 10 bytes
/* 4. sizeof structure */
printf("%zu\n", sizeof(struct MyStruct)); // 8 bytes = int 4 + float 4
return 0;
}
4.3 ๋ฌธ์์ด์ด ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋๋ ๊ตฌ์กฐ
์ซ์ ํ๋ 1
์ซ์์ ๋ฐฐ์ด 0 1 2 3 4 5 6 7 8 9
๋ฌธ์ ํ๋ 'a'
๋ฌธ์์ ๋ฐฐ์ด 'h' 'e' 'l' 'l' o' '\0' ~ ~ ~ ~ ๋ฌธ์์ด์ ๋์ ํํํ๊ธฐ ์ํด \0 ์ฌ์ฉ
#include <stdio.h>
int main()
{
int a = 1;
int int_arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printf("%i %i %i\n", int_arr[0], int_arr[1], int_arr[9]);
//printf("%i\n",int_arr[10]); ๋ฌธ์ ์๊น
/*
char c = 'a';
char str1[10] = "Hello"; // null charactor
char str2[10] = { 'h', 'i'};
printf("%c\n", c);
printf("%s\n", str1);
printf("%s\n", str2);
printf("%hhi %hhi %hhi %hhi %hhi\n",
str2[0], str2[1], str2[2], str2[3], str2[4]); // 72 105 0 0 0
*/
/*
char str3[10] = "Hello, world"; // array size is not enough
char str4[20] = "Hello, \0World";
printf("%s\n", str4); // Hello, ๋ง ์ถ๋ ฅ๋จ \0 ๋ค๋ ๊ฐ์ ์กด์ฌํ์ง๋ง ์ถ๋ ฅํ์ง ์์
*/
return 0;
}
4.4 strlen() ํจ์
#include <stdio.h>
#include <string.h> // strlen and more
int main()
{
char str1[100] = "Hello";
char str2[] = "Hello";
char str3[100] = "\0";
char str4[100] = "\n";
printf("%zu %zu\n", sizeof(str1), strlen(str1)); // 100 5 strlen์ \0๋ ๋นผ๊ณ ์ผ๋ค
printf("%zu %zu\n", sizeof(str2), strlen(str2)); // 6 5
printf("%zu %zu\n", sizeof(str3), strlen(str3)); // 100 0
printf("%zu %zu\n", sizeof(str4), strlen(str4)); // 100 1
/* Extra */
/*
char* str5 = (char*)malloc(sizeof(char) * 100);
str5[0] = 'H'; str5[1] = 'e'; str5[2] = 'l'; str5[3] = 'l'; str5[4] = 'o';
str5[5] = '\0';
printf("%zu %zu\n", sizeof(str5), strlen(str5)); // 4 5
*/
return 0;
}
๋ฌธ์์ด์ ๊ฐ์๋ฅผ ์ธ์ด์ค๋ค
\0์ ๋นผ๊ณ ์