Go · C 코드 비교
이 코스 레슨에 나오는 같은 프로그램을 두 언어로 나란히 보여 줍니다: Go, C.
출력과 주석
Go의 fmt는 형식이 지정된 인수를 검사하고 출력 형식의 불일치를 보고합니다. C의 printf는 가변 인수 형식 계약을 신뢰하므로, 형식이 일치하지 않으면 정의되지 않은 동작이 발생할 수 있습니다.
Go1package main2import ("fmt")3 4func main(){5fmt.Printf("value=%d\n",7)6}C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10printf("value=%d\n",7);11return 0;12}불리언과 분기
Go에서는 조건에 bool이 필요합니다. C에서는 스칼라 값의 참 여부를 검사하므로, 빈 문자열을 가리키는 포인터는 null이 아니면 참입니다. Go의 len 의미를 포인터 검사로 옮기지 말고 명시적으로 비어 있는지 확인하세요.
Go1package main2import ("fmt")3 4func main(){5s:="";fmt.Println(len(s)==0)6}C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10const char *s=""; printf("%s\n",s?"true":"false");11return 0;12}배열에는 길이가 필요하다
Go 슬라이스는 설명자에 길이와 용량을 담습니다. C 포인터에는 둘 다 없습니다. C의 포인터/길이 쌍은 빌린 뷰와 비슷하지만, C는 경계나 수명을 강제하지 않습니다.
Go1package main2import ("fmt")3 4func main(){5a:=[]int{2,4,6};fmt.Println(len(a))6}C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int a[]={2,4,6};printf("%zu\n",sizeof a/sizeof a[0]);11return 0;12}함수 인자는 값이다
Go는 포인터와 슬라이스 디스크립터를 포함한 값을 값으로 전달합니다. C도 마찬가지로 인수를 복사합니다. 복사된 포인터를 통해 값을 변경하면 가리키는 대상이 영향을 받지만, 지역 포인터를 재할당해도 호출자의 포인터가 바뀌지는 않습니다.
Go1package main2import ("fmt")3func change(x int){x=9;_ = x}4func main(){5x:=2;change(x);fmt.Println(x)6}C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8static void change(int n){n=9;(void)n;}9int main(void) {10int x=2;change(x);printf("%d\n",x);11return 0;12}주소와 역참조
Go와 C는 모두 주소 지정과 역참조에 &와 *를 사용합니다. Go는 가비지 컬렉션을 통해 이스케이프된 객체를 계속 살아 있게 하지만, C에서는 자동 객체가 범위를 벗어나면 포인터가 댕글링 포인터가 될 수 있습니다.
Go1package main2import ("fmt")3 4func main(){5n:=2;p:=&n;*p=5;fmt.Println(n)6}C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int n=2;int *p=&n;*p=5;printf("%d\n",n);11return 0;12}상태 결과를 반환하는 파싱
Go의 Atoi는 형식이 잘못된 입력에 대해 오류를 반환합니다. C의 scanf 스타일 변환은 숫자 접두부를 받아들일 수 있으므로, 뒤에 바이트가 남아 있으면 입력 전체를 무효로 처리할지 호출자가 결정하고 적용해야 합니다.
Go1package main2import ("fmt";"strconv")3 4func main(){5_,err:=strconv.Atoi("12x");fmt.Println(err==nil)6}C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10const char *s="12x";int ok=*s!=0;for(size_t i=0;s[i];i++)if(s[i]<'0'||s[i]>'9')ok=0;printf("%d\n",ok);11return 0;12}
강의계획서
10 챕터48 강의1 프로젝트48 도전과제99 퀴즈 문항컴파일과 계산
6 강의조건문과 반복문
5 강의배열과 텍스트
5 강의함수와 인터페이스
5 강의포인터와 구조체
5 강의검증과 텍스트 버퍼
5 강의소유 동적 메모리
5 강의태그된 데이터와 합성
6 강의프로젝트: 재고 원장
4 강의최종 소스 포팅
2 강의자주 묻는 질문
Go 개발자를 위한 C은(는) 누구를 위한 코스인가요?
이미 Go 코드를 쓸 줄 알고 C 언어를 배우려는 분을 위한 코스입니다. 각 레슨은 익숙한 Go 코드에서 시작해 C 언어에서는 같은 일을 어떻게 하는지 보여 주므로, 초보자용 기초는 건너뛸 수 있습니다.
C 언어를 미리 알아야 하나요?
아니요. 코스는 첫 C 프로그램부터 시작합니다. 필요한 것은 Go 기초입니다: 변수, 조건문, 반복문, 함수, 컬렉션.
Go 개발자를 위한 C에는 강의가 몇 개 있나요?
Go 개발자를 위한 C에는 자기 페이스에 맞게 진행할 수 있는 48개의 인터랙티브 강의가 포함되어 있습니다.
Go 개발자를 위한 C은(는) 무엇을 다루나요?
코스는 다음 챕터로 구성됩니다: 컴파일과 계산, 조건문과 반복문, 배열과 텍스트, 함수와 인터페이스, and more.
Go 개발자를 위한 C은(는) 무료인가요?
네. Coddy의 모든 코스는 무료로 시작할 수 있습니다. 다운로드나 가입 없이 브라우저에서 강의를 진행할 수 있습니다.