Menu
CoddyTech

반복 범위와 진행

Coddy의 C# 개발자를 위한 C 코스 레슨. 48개 중 10번째.

C#과 C는 동일한 세 부분으로 구성된 for 형식을 사용합니다. C의 배열 인덱싱에는 관리되는 범위 초과 예외가 없으므로, 경계는 단순히 반복 횟수를 선택하는 문제가 아니라 안전을 위한 의무 사항입니다.

초기화, 경계 및 업데이트를 명시적으로 작성하세요. 마지막 유효 인덱스가 배열 길이보다 작도록 하세요.

C# 예제

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
class Program {

static void Main(){
int total=0;for(int i=1;i<4;i++)total+=i;Console.WriteLine(total);
}
}

C# 출력:

6

C 예제

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>

int main(void) {
int total=0;for(int i=1;i<4;i++)total+=i;printf("%d\n",total);
return 0;
}

C 출력:

6
challenge icon

챌린지

쉬움

입력 n은 0부터 100까지입니다. 1부터 n까지의 정수 합을 반환하세요.

잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 표시된 잠금 상태의 main.c가 함수를 직접 호출하므로 선언된 공개 API와 호환되도록 유지하세요. 다른 main을 추가하지 마세요. 작업에서 범위를 명시적으로 확인하지 않는 한, 모든 숫자 관련 작업 테스트 데이터는 오버플로가 발생하지 않을 만큼 작습니다. 지정된 출력만 출력하세요. 드라이버가 표시된 경우 입력 파싱과 출력을 처리합니다.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){int n=0;if(scanf("%d",&n)!=1)return 1;printf("%d\n",sum_to(n));return 0;}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

C# 개발자를 위한 C의 모든 레슨

직접 연습해 보세요: 온라인 C 컴파일러