본문 바로가기
컴퓨터/C & C++

[C언어] 002. C 언어 기본 문법 syntax

by HanJoohyun 2017. 10. 17.
반응형



안녕하세요 


한주현 입니다


C 언어 연재 두 번째 시간입니다!!


오늘은 C 언어에서 사용하는 기본 문법에 대해 알아보겠습니다.



소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
 
int main(void){
  /* This is
     multiple line
     comment */
 
  printf("Hello world\n");
 
  // This is single line comment
 
  int num;
  num = 3;
 
  return 0;
}
 
cs



라인8, 12, 13, 15: 세미콜론  ;   , C 프로그램에서 세미콜론은 문장을 마치는 용도로 사용합니다. 각각의 문장은 세미콜론으로 끝맺습니다.


주석(Comment)

라인4-6: /* 여러 줄 주석 */ 은 /* 기호 사이에 집어넣습니다 */

라인10: // 한 줄 주석 은 // 기호를 사용합니다






키워드


다음 표에 정리된 키워드들은 변수 또는 상수로 사용 할 수 없습니다


  auto 

  double 

  int 

  struct 

  break

  else 

  long 

  switch 

  case

  enum 

  register 

  typedef 

  char

  extern 

  return 

  union 

  const

  float 

  short 

  unsigned 

  continue

  for 

  signed 

  void 

  default

  goto 

  sizeof 

  volatile 

  do

  if 

  static 

  while 



1
2
3
4
5
6
7
8
#include <stdio.h>   
 
int main(){
  int auto = 5;
  print("%d\n",auto);
  return 0;
}
 
cs



컴파일 오류 발생

$ gcc keyword.c -o keyword.exe

keyword.c: In function 'main':

keyword.c:4:12: error: expected identifier or '(' before '=' token

   int auto = 5;

            ^

keyword.c:5:16: error: expected expression before 'auto'

   print("%d\n",auto);

                ^







공백


C 언어에서 공백(white space) 라 함은 blank, 탭, 개행문자등을 일컫습니다.


공백을 넣는 경우는 다음과 같습니다



int age;

와 같은 경우는 int 와 age 사이에 공백이 들어가야 합니다


animal = dogs + cats;

shell script 의 경우 = equal 사이에 공백이 있으면 오류가 납니다


반면에 C 언어의 경우는 공백이 있거나 없거나 상관이 없습니다


공백을 소스코드에 적절히 넣게 되면 가독성이 증가하게 됩니다





오늘은 C 언어 기본문법에 대해 알아보았습니다


그럼 다음시간에 만나요~






반응형

댓글