차리토 실험실

C언어 예제 #1(프린트, 사용자 입력, 반복문, 조건문)

수차리토 2023. 1. 20. 20:55

1. "Hello, World!" 출력: 가장 기초적인 프로그램입니다. 콘솔창에 "Hello, World!"를 출력합니다


#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

 

2. 사용자 입력: 키보드로부터 사용자 입력을 받아 처리하는 예제입니다.

#include <stdio.h>

int main() {
    int age;
    printf("What is your age? ");
    scanf("%d", &age);
    printf("Your age is: %d\n", age);
    return 0;
}
 
3. 반복문: for, while, do-while 반복문을 사용하는 예제입니다.
 
#include <stdio.h>

int main() {
    int i;
    printf("for loop:\n");
    for (i = 0; i < 10; i++) {
        printf("%d ", i);
    }
    printf("\nwhile loop:\n");
    i = 0;
    while (i < 10) {
        printf("%d ", i);
        i++;
    }
    printf("\ndo-while loop:\n");
    i = 0;
    do {
        printf("%d ", i);
        i++;
    } while (i < 10);
    return 0;
}
4. 조건문: if, if-else, switch-case를 사용하는 예제입니다.
#include <stdio.h>

int main() {
    int a = 10, b = 20;
    if (a < b) {
        printf("a is less than b\n");
    }
    if (a == b) {
        printf("a is equal to b\n");
    } else {
        printf("a is not equal to b\n");
    }
    int x = 3;
    switch (x) {
        case 1:
            printf("x is 1\