#include <stdio.h>

int main() {
     // 배열 : 같은 자료형의 값을 하나로 묶어서 저장하는 공간

     // 배열 선언
     // 자료형 배열명[배열크기];
     int score[5];

     score[0] = 100;
     score[1] = 90;
     score[2] = 80;
     score[3] = 70;
     score[4] = 60; 

     for (int i = 0; i < 5; i++) {
          printf("%d ", score[i]);
     }
     printf("\n");

     // 배열 선언과 동시에 초기화
     // 배열 크기를 적지 않아도 자동으로 개수를 세어서 설정한다
     int score2[] = {1, 2, 3, 4 ,5};

     for (int i = 0; i < 5; i++) {
          printf("%d ", score2[i]);
     }
     printf("\n");

     // 배열 선언 O, 초기화 X -> 더미 값(쓰레기 값)
     // 배열 선언 O, 일부 초기화 -> 초기화 한 값은 저장, 나머지는 0으로 자동 초기화 
     int arr[10]; // = {1};

     for (int i = 0; i < 10; i++) {
          printf("%d ", arr[i]);
     }
     printf("\n");

     //실수 배열
     float arr_f[5] = {1.1f, 2.2f, 3.3f};
     for (int i = 0; i < 5; i++){
          printf("%.2f ", arr_f[i]);
     }
     printf("\n");

     //문자열
     char str1[] = "coding";
     printf("%s\n", str1);
     printf("%c\n", str1[0]);
     // 문자열의 마지막에는 널null문자 \0가 들어있다
     // 널null문자 : 문자열의 끝을 알려주는 문자
     printf("%d\n", sizeof(str1)); // sizeof() : 메모리 공간을 얼마나 차지하는지 바이트 단위로 알려준다

     for (int i = 0; i < sizeof(str1); i++) {
          printf("%d ", str1[i]); //아스키 코드
     }
     printf("\n");

     char kor[10] = "박주현";
     printf("%s\n", kor);
     printf("%d\n", sizeof(kor));
     //한글은 한 글자 당 3바이트 + \0

     //2차원 배열
     // 자료형 배열명[세로 크기][가로 크기];
     int arr2[2][5];
     arr2[0][4] = 100;
     arr2[1][0] = 200;

     int arr3[2][3] = {
          {10, 20, 30}, 
          {40, 50, 60}
     };
     // {{10, 20, 30}, {40, 50, 60}};

     for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 3; j++){
               printf("arr3[%d][%d] = %d\n", i, j, arr3[i][j]);
          }
     }

     printf("----문제----\n");
     int arr4[4][2] = {
          {1, 2},
          {3, 4},
          {5, 6},
          {7, 8}
     };

     printf("%d\n", arr4[2][1]);


// 문자열 "hello"를 담을 배열 eng를 선언
     char eng[] = "hello";
// 문자열을 출력
     printf("%s\n", eng);
// 배열의 크기를 출력
     printf("%d\n", sizeof(eng));
// 문자 e의 아스키 코드를 출력
     printf("%d\n", eng[1]);

     return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: