자료형과 연산자
오픈 플랫폼 BSP/C언어 / 2010. 5. 17. 17:51
자료형과 연산자
문자와 해당 아스키 코드를 출력하는 프로그램 |
소스코드 |
#include <stdio.h> int main() { char ch; printf("Please enter any character: "); scanf("%c", &ch); printf("the ASCII code for character %1c is %3d.\n", ch, ch); return 0; } |
실행 |
|
8진수, 10진수, 16진수를 이용한 문자의 출력과 escape sequence를 이용한 문자 출력 |
소스코드 |
#include <stdio.h> int main() { printf("10진수 65: %c\n", 65); printf("16진수 40: \x40\n"); // \x00 = 16진수 printf("8진수 73: \073\n"); // \000 = 8진수 printf("백슬래쉬 \\, 작은 따옴표 \', 큰따옴표 \"\n"); printf("2 bell sound \a\a\n"); return 0; } |
실행 |
문자열을 화면에 출력하는 프로그램 |
소스코드 |
#include <stdio.h> int main() { char word[30]; printf("Please enter anything: "); scanf("%s", word); printf("%s, this is what you typed.\n", word); return 0; } |
실행 |
scanf 함수는 공백 및 엔터까지의 입력을 지원한다. |
비교 연산자 사용의 예 |
소스코드 |
#include <stdio.h> int main() { int number; scanf("%d", &number); printf("Is number less than 10? %3d\n", number < 10); // 참이면 1 거짓이면 0 printf("Is number not equal to 10? %3d\n", number != 10); return 0; } |
실행 |
미성년자를 구분하는 프로그램, 논리값으로 출력 |
소스코드 |
#include <stdio.h> int main() { int age; printf("Please enter Tom's age: "); scanf("%d", &age); printf("Is Tom a teen age? %d\n", (age >=13) && (age <=19)); // 둘다 참이면 1 하나라도 거짓이면 0 return 0; } |
코드 |
증감 연산자의 전치형과 후치형을 비교하는 프로그램 |
소스코드 |
#include <stdio.h> int main() { int a1, a2, b1, b2; a1 = 30; // a1 = 30 a2 = ++a1; // ++a1 = 31 -> a2 = 31 printf("a1 = %5d a2 = %5d\n", a1, a2); b1 = 30; // b1 = 30 b2 = b1++; // b1 = 30 -> b2 = 30 -> b1++ = 31 printf("b1 = %5d b2 = %5d\n", b1, b2); return 0; } |
실행 |
'오픈 플랫폼 BSP > C언어' 카테고리의 다른 글
리눅스에서 GDB를 이용한 디버깅 하기 (0) | 2010.05.31 |
---|---|
20100526 - 과제 (0) | 2010.05.27 |
int형 변수가 범위를 벗어나는 값을 가질 경우 발생하는 오류 (0) | 2010.05.13 |
C언어 컴파일 과정 (VC++ 기준) (0) | 2010.05.12 |
C언어의 기초 컴파일 실습 (0) | 2010.05.11 |