초기식
while (조건식) // ← 루프 선언문(loop statement)
{
반복할 코드
변화식
}
// ↑ 루프 본체(loop body) 및 변화식

# define _CRT_SECURE_NO_WANINGS
# include<stdio.h>
int main(void)
{
int n;
printf("약수를 출력할 1 이상의 정수 한 개를 입력하세요: ");
scanf("%d", &n);
if (n>=1){
int divisor = 1;
while(divisor <= n){
if (n % divisor == 0){
printf("%d\n", divisor);
}
divisor++;
}
}
else{
printf("1미만의 정수가 입력되었습니다. 프로그램을 종료합니다\n");
}
return 0;
}
// 알파벳 입력받는 코드
# define _CRT_SECURE_NO_WANINGS // 컴파일러 경고 억제
# include<stdio.h> // 표준 입출력 포함된 헫 파일
int main(void){ // 메인함수
char ch; // 입력한 문자 저장 변수
char newlinchar; // 입력 버퍼에 남아 있는 개행 문자 저장 변수
printf("영문 알파벳 문자 한 개를 입력하세요: ");
scanf("%c", &ch);
scanf("%c", &newlinchar);
while((ch<'A'|| ch>'Z')&&(ch<'a'||ch>'z'))
{
printf("알파벳 아닌 문자가 입력되었습니다. 다시 입력하세요: ");
scanf("%c", &ch);
scanf("%c", &newlinchar);
}
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(void) {
char ch;
printf("영문 알파벳 문자 한 개를 입력하세요: ");
scanf("%c", &ch);
while ((ch < 'A' || (ch > 'Z' && ch < 'a') || ch > 'z')) {
while (getchar() != '\n'); // 버퍼에 남아 있는 개행 문자 제거
printf("알파벳 아닌 문자가 입력되었습니다. 다시 입력하세요: ");
scanf("%c", &ch);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h> // srand, rand 함수가 선언된 헤더 파일
#include <time.h> // time 함수가 선언된 헤더 파일
int main()
{
srand(time(NULL)); // 현재 시간값으로 시드 설정
int i = 0;
while (i != 3) // 3이 아닐 때 계속 반복
{
i = rand() % 10; // rand 함수를 사용하여 무작위로 정수를 생성한 뒤 10 미만의 숫자로 만듦
printf("%d\n", i);
}
return 0;
}
초기식
do // ↓ 루프 본체(loop body) 및 변화식
{
반복할 코드
변화식
} while (조건식);
// ↑ 루프 선언문(loop statement)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
char ch;
char newlinchar;
do {
printf("영문 알파벳 문자 한 개를 입력하세요: ");
scanf("%c", &ch);
scanf("%c", &newlinchar);
if ((ch<'A' || ch>'Z') && (ch<'a' || ch>'z')) {
printf("알파벳 아닌 문자가 입력되었습니다. 다시 입력하세요: ");
}
} while ((ch<'A' || ch>'Z') && (ch<'a' || ch>'z'));
printf("ch=%c\n", ch);
return 0;
}
for (초기식; 조건식; 변화식) // ← 루프 선언문(loop statement)
{
반복할 코드
}
// ↑ 루프 본체(loop body)

break문

continue문


# define _CRT_SECURE_NO_WANINGS
# include<stdio.h>
int main(void)
{
int n;
int numOfDivisors = 0;
printf("약수를 출력할 1 이상의 정수 한 개를 입력하세요: ");
scanf("%d", &n);
if (n>=1)
{
for(int divisor =1; divisor <=n; divisor ++)
{
if (n%divisor ==0)
{
if (divisor % 2 ==0){continue;}
printf("%d\n", divisor);
}
}
}
else
{
printf("1미만의 정수가 입력되었습니다. 프로그램 종료\n");
}
return 0;
}

- 캡슐화: 내부동작 원리 및 구조를 밖에 드러내지 않고 사용하는데 필요한 인터페이스만 제공하는 것
- 재사용성: 재사용성을 높이기 위해 함수는 단순화시키고 일반화해서 작성
반환값자료형 함수이름(매개변수_목록)
{
명령문
}
return 표현식;
return ;
//주어진 시간을 1분 증가시켜 변경되는 시간 정보를 출력하는
프로그램을 만들자
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int addOneMinute(int hour, int minute)
{
minute++;
if (minute == 60)
{
minute = 0;
hour ++;
if (hour ==24)
{
hour = 0;
}
}
return hour*100+minute;
}
int main()
{
int hour, minute, time;
printf("시간과 분 입력: ");
scanf("%d %d", &hour, &minute);
time = addOneMinute(hour, minute);
printf("변경된 시간은 %02d:%02d \n", time/100, time%100);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void swap(int num1, int num2)
{
int temp = num1;
num1=num2;
num2=temp;
}
int main()
{
int x = 5;
int y = 9;
swap(x,y);
printf("x=%d, y=%d\n", x,y);
return 0;
}

// 지역변수 사용
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void printQuadEqnSoln(int a, int b, int c)
{
double r = sqrt(b*b-4*a*c);
double x1 = (-b+r)/(2*a);
double x2 = (-b-r)/(2*a);
printf("x1=%f, x2 = %f\n", x1, x2);
}
int main(void) {
printQuadEqnSoln(1, -5, 6);
printQuadEqnSoln(1, 5, -6);
return 0;
}
//전역변수 사용 코드
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<math.h>
double x1;
double x2;
void calcQuadEqn(int a, int b, int c)
{
double r = sqrt(b*b-4*a*c);
x1 = (-b+r)/(2*a);
x2 = (-b-r)/(2*a);
}
int main(void) {
calcQuadEqn(1, -5, 6);
printf("x1 = %f, x2 = %f\n", x1,x2);
calcQuadEqn(1,5,-6);
printf("x1 = %f, x2 = %f\n", x1,x2);
return 0;
}
// 난수 생성
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
unsigned seed = 17;
void setseed(unsigned s)
{
seed = s;
}
unsigned random()
{
seed = (seed*seed)+(13*seed) +19;
//if (seed>RAND_MAX) {seed %= RAND_MAX;}
return seed;
}
int main(void) {
setseed(50);
for (int i = 0; i<=10; i++)
{
printf("%u\n", random());
}
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int x=1; // 전역변수 x를 선언하고 1로 초기화
void func1(int x) // func1 함수에 지역변수 x를 선언
{
printf("func1: x = %d\n", x); // func1()의 지역변수 x의 값을 화면에 출력
}
void func2(int n)
{
x=n; // 아직 func2의 지역변수 x가 선언되기 전이므로 전역변수 x에 n을 저장
printf("func2-1: x = %d\n", x); // 전역변수 출력
int x = 5; // func2의 지역변수 x를 선언 및 초기화
printf("func2-2: x = %d\n", x); // func2의 지역변수 x값을 추력
}
int main(void)
{
printf("main: = x =%d\n", x); // 전역변수 x의 값을 출력
func1(3);
func2(7);
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int x = 5;
int main(void) {
printf("x = %d\n", x); // 5
int x = 7;
printf("x = %d\n", x); // 7
{
printf("x = %d\n", x); // 7
int x = 9;
printf("x = %d\n", x); // 9
}
for (int x = 0; x <2; x++)
{
printf("x = %d\n", x); // 0, 1
}
printf("x = %d\n", x); //7
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void printCallCount()
{
static int count = 0; //정적 지역변수 count를 선언한고 0으로 초기화
int localCount = 0; // 지역변수 localCount를 선언하고 0으로 초기화, 일반 지역변수는 함수가 호출될때마다 초기화됨
count++;
localCount++;
printf("count = %d\n", count); // 이전 호출때 사용한 값이 저장
printf("localCount = %d\n", localCount); // 함수 호출과 함께 이전 값은 사라짐
}
int main()
{
printCallCount(); //count = 1, localcount = 1
printCallCount(); // count =2, localcount = 1
printCallCount(); // count =3, localcount = 1
}
월을 나타내는 enum 자료형을 만들고 사용자로부터 정수를 입력받아 eunm 값으로 반환하는 함수 코드
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// enum 자료형을 선언
typedef enum {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC} MONTH;
MONTH getMonth()
{
MONTH month; // int month; 도 가능
printf("1~12 사이의 정수를 입력하세요: ");
scanf("%d", &month); // enum도 int와 동일하게 %d로 입력받음
return month;
}
int main(void) {
MONTH m = getMonth();
printf("month = %d\n", m);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void func(int n)
{
const int m = n;
const int v = 3;
printf("m = %d\n", m);
}
int main(void)
{
func(50);
return 0;
}