// 변수와 자료형

// 변수 : 데이터를 저장하는 메모리 공간
// 자료형 : 변수에 저장되는 값의 종류와 크기를 정하는 것

#include <stdio.h>

int main() {
     // 변수 만들기
     // 자료형 변수이름 = 값;
     int a = 2025;

     // 자료형 변수이름;
     // 변수이름 = 값;
     int b, c;
     b = 9;
     c = 13;

     printf("%d\n", a);

     /*자료형과 서식 지정자
     정수 int %d
     실수 float (4바이트) %f
          double (8바이트) %lf
     문자형 char
          문자 %c (1바이트) ''
          문자열 %s ""
     */

     float d = 12.36;
     printf("%f\n", d);
     printf("%.1f\n", d);

     double e = 7.123456789123456;
     printf("%.15lf\n", e);

     char f = 'A';
     printf("문자 : %c\n", f);

     char g[] = "abcd";
     printf("문자열 : %s\n", g);
     //문자열의 가장 마지막에는 널 null문자 \0이 들어있다
     printf("%d\n", sizeof(g));


     short num1 = 200;
     int num2 = 100000;
     long long num3 = 1234567890123;
     printf("short: %hd\n", num1);
     printf("int(long): %d\n", num2);
     printf("long long: %lld\n", num3);

     //변수 : 변하는 수
     int age = 14;
     printf("나이는 %d\n", age);
     age = 15;
     printf("나이는 %d\n", age);

     //상수 : 변하지 않는 수
     //const 자료형 변수이름 = 값;
     const int YEAR = 2010;
     printf("상수: %d\n", YEAR);
     // YEAR = 2015; 수정이 불가능하다

     printf("----문제-----\n");
     // 1. char 타입 변수에 문자 'C'를 저장해서 출력하세요.
     char ch = 'C';
     printf("1 : %c\n", ch);

     // 2. 문자열 "Hello C"를 저장한 뒤, 그 길이를 sizeof 연산자를 이용해 출력하세요.
     char str[] = "Hello C";
     printf("2 : 문자열:%s, 문자열 길이:%d\n", str, sizeof(str));

     // 3. float 타입 변수에 소수 둘째 자리까지 출력할 수 있는 값을 저장하고 출력하세요.
     float f1 = 3.14159f;
     printf("3 : %.2f\n", f1);

     // 4. double 타입 변수에 긴 소수를 저장하고 소수점 15자리까지 출력하세요.
     double d1 = 1.123456789;
     printf("4 : %.15lf\n", d1);
     
     return 0;
}

Embed on website

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