๋”ฐ๋ฐฐ์”จ

๋”ฐ๋ฐฐ์”จ 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์€ ๋นผ๊ณ  ์…ˆ