유니코드 문자열 처리 - C언어

홍성우·2023년 4월 21일

자료구조 (C언어)

목록 보기
4/15

유니코드 wchar_t 2Byte

#include <stdio.h>
#include <string.h>

int main() {
	// 유니코드
	wchar_t* pwszData = L"String";
	wchar_t wszData[32];

	wcscpy(wszData,pwszData);
	wprintf(L"%s\n", wszData);
}

유니코드 wcstombs()

int main() {
	// 유니코드 문자열 상수로 초기화
	wchar_t* pwszData = L"String";

	// MBCS문자열을 담을 수 있는 배열
	char szData[32];
	size_t nConverted = 0;

	// 변환된 문자열의 길이를 알아낸다.
	nConverted = wcstombs(NULL, pwszData, 32);
	printf("%d\n", nConverted);

	// 유니코드 문자열을 MBCS 문자열로 변경해 szData 저장
	nConverted = wcstombs(szData, pwszData, 32);

	printf("%s (%d)\n", szData, nConverted);

}

형 변환

int main() {
	// atoi() : ASCII to Integer
	// atol() : ASCII to long
	// atof() : ASCII to double

	char szBuffer[32];
	int nResult = 0;

	printf("Input String:");
	gets(szBuffer); 

	nResult = atoi(szBuffer);
	printf("%d\n", nResult);

	printf("%d\n", atoi("2147483647"));
	printf("%d\n", atoi("2147483648"));
	printf("%e\n", atof("1.7e+308"));
	printf("%e\n", atof("1.7e+309"));
}

시간 출력 함수

#include <stdio.h>
#include <string.h>
#include <time.h>

int main() {
	// atoi() : ASCII to Integer
	struct tm* ptime = { 0 };
	time_t t = 0;

	t = time(NULL);
	ptime = localtime(&t);

	printf("%d\n", t);
	printf("%s", ctime(&t));

	printf("%04d-%02d-%02d\n",
		ptime->tm_year + 1900,
		ptime->tm_mon + 1, ptime->tm_mday);

}

난수 생성

  #include <stdio.h>
  #include <string.h>
  #include <time.h>

  int main() {
      int i = 0;
      srand((unsigned)time(NULL)); // 난수 초기값 세팅

      for (int i = 0; i<10; i++) {
          printf("%4d\n", rand());
      }

      for (int i = 0; i < 10; i++) {
          printf("%6d\n", rand() % 3); // 가위바위보 세팅 0~2난수 생성
      }


  }

명령 프롬트창

System 명령 콘솔에 실행할 문자열이 저장된 메모리 주소입력
Input창에 notepad라고 입력시 notepad.exe가 실행되어 메모장이 열린다.

#include <stdio.h>
#include <string.h>
#include <time.h>

int main() {
	char szCommand[512] = { 0 };
	printf("Input command:");
	gets(szCommand);

	system(szCommand); // input으로 notpad를 입력했다-> notepad.exe가 실행된다.

}	

Exit함수

#include <stdio.h>
#include <string.h>
#include <time.h>

int main() {
	char ch;
	printf("Do you want to Exit? (Y/N)\n");
	ch = _getch(); // _getch()함수는 콘솔창에 사용자 입력값이 보이지 않는다.

	if (ch == 'y' || ch == 'Y') {
		puts("EXIT");
		exit(1);


	}
	puts("End of main()");

}
profile
제 블로그를 방문해 주셔서 감사합니다

0개의 댓글