본문 바로가기

자동제어(PLC,PC,로봇) & 전장설계 & CNC/PC 제어

PC제어프로그램 5일차 (20.05.14) - 기본코딩2

반응형

변수 선언 연습

/*파일: year.c  변수 선언 연습*/
#include <stdio.h>
#include <time.h>

int main(void)
{
	time_t t=time(NULL);
	struct tm tm = *localtime(&t);

	int year;
	int month;
	int date;

	year = 2020;
	month = tm.tm_mon+1; // month = 5;
	date = 14;



	printf("오늘 날짜는 %d년 %d월 %d일 입니다.\n", year, month, date);
	printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

	return 0;
}

 

 

 

 

 

문자형 변수 선언 후 값 저장, 출력

이어폰을 착용하고 아래 코드를 실행하면 '비프음소리'가 1번 난다.

 

/* char.c 문자형 변수 선언 후 값 저장, 출력*/
// 인쇄할 수 없는 문자와 특수한 문자를 탈출
#include <stdio.h>

int main(void)
{
  char alarm = '\a'; // 변수를 문자표현식으로 함. 역슬러시는 컴퓨터 안의 것을 그대로 내는 것이다.
  char quote ='\042'; // 8진수 042 이므로 4X8 + 1 = 34
  char uppercase = '\101'; // 8진수 042 이므로 8X8 + 1 = 65

  printf("\n");
  printf("알람(소리 나지요) 출력 > %c\n", alarm); // 비프음 소리1번 울림
  printf("큰 따옴표 \"출력 > %c\n", quote);
  printf("대문자 A 출력 > %c\n", uppercase);
  printf("대문자 C 출력 > %c\n", uppercase+2);
}

 

 

 

 

 

부동 소수형 출력

float : 소수 6자리까지 표현

Long Double : 소수 15자리까지 표현

 

/* char2.c 부동소수형 출력 */
#include <stdio.h>

int main(void)
{
  float c=4.123456;
  double d=4.123456789012345;
  float e=41.234567e-1L;

  //변수의 값 출력(출력 폭, 소수점이하 자리 출력)
  printf("%5.3f\n",c);
  printf("%7.2f\n",d);
  printf("%.3f\n", e);

  return 0;
}

 

 

 

 

 

/* 파일 : define.c */
#include <stdio.h>
#define PHI 3.141592 // printf()의 이용을 위한 헤더파일 포함

int main(void)
{
	double radius = 5;
	double circumference = 2*PHI*radius;
	// long float의 약자 -> lf
	printf("반지름이 %.1lfcm인 원의 둘레는 %.2lfcm입니다.\n", radius, circumference);
	printf("반지름이 %.0lfcm인 원의 둘레는 %.8lfcm입니다.\n", radius, circumference);
}

 

PHI 3.14 -> 지시자로 3.14를 넣었다.

double x; -> 소수 15자리까지 표현하라

 소수 15자리까지 표현하라

.1lf ->  소수 1자리만 표현하라. 앞을 free하다.

X X . 소수

3.1lf -> 전체 자리수는 3자리로 하고 뒤는 위와 같다. 

X . X

 

 

 

/* square1.c */
// 인자가 있는 매크로
#include <stdio.h>
#define SQUARE2 2*2	// 범례를 이렇게 만들어 놓는다.
#define SQUARE3 3*3	// 밑에서 이것을 사용 -> 불편하다

int main(void)
{
  printf("2 * 2 = %d, 3 * 3 = %d,\n", SQUARE2, SQUARE3);

  return 0;
}

 

 

 

/* square2.c */
#include <stdio.h>
#define SQUARE(x) ((x)*(x)) // 함수(범례)를 만들어 놓고 사용한다.

int main(void)
{
  printf("2 * 2 = %d, 3 * 3 = %d.\n",SQUARE(2),SQUARE(3));	

  return 0;
}

 

 

 

 

/* square3.c */
#include <stdio.h>
#define SQUARE(x) ((x)*(x)) // 함수(범례)를 만들어 놓고 사용한다.

int main(void)
{
  printf("4 * 4 = %d, 5 * 5 = %d.\n",SQUARE(4),SQUARE(5));	

  return 0;
}

 

 

 

 

/* square4.c */ 
#include <stdio.h>
#define SQUARE(x) ((x)*(x))
#define PHI 3.14

int main(void)
{
  double radius = 5;
  printf("반지름 %.0lf인 원의 면적은 %.2lf\n", radius, PHI*SQUARE(radius));

  return 0;
}

 

 

 

 



출처: https://okcoding.tistory.com/482?category=899419 [생활코딩]