닌자고양이
[C#] C#에서 C언어의 printf, scanf, getch 사용하기 본문
using System.Runtime.InteropServices;
class Program
{
[DllImport("msvcrt.dll")]
public static extern int printf(string format, __arglist);
public static int printf(string format) => printf(format, __arglist());
[DllImport("msvcrt.dll")]
public static extern int scanf(string format, __arglist);
public static int scanf(string format) => scanf(format, __arglist());
[DllImport("msvcrt.dll")]
static extern int _getch();
static void Main()
{
int a = 0, b = 0, n;
var s = new StringBuilder();
n = printf("정수 정수 문자열 입력:\n");
n = scanf("%d %d %s", __arglist(ref a, ref b, s));
n = printf("(%d 개 입력됨)\n%d %d %s\n", __arglist(n, a, b, s));
_getch();
}
}
1. System.Runtime.InteropServices 네임스페이스 사용
2. msvcrt.dll 에서 c런타임 함수 참조
3. __arglist 로 관리되지 않은 가변 인수 리스트를 전달
printf 의 __arglist(...) 에 출력할 변수들을 입력
scanf 의 __arglist(...) 에 문자열은 StringBuilder 변수를, 그외에는 ref 변수를 입력
* 추가 인수 없는 int printf(string format), int scanf(string format) 정의
'C# .NET' 카테고리의 다른 글
[.NET Core/.NET 5] 단일 실행 파일로 배포하기 (0) | 2021.03.01 |
---|---|
[C#] 문자열을 특정 길이(chunk size)로 쪼개기 (0) | 2020.09.18 |
[C#] 시간 범위 겹침 여부 체크 (0) | 2020.02.19 |
[C#] LINQ 를 사용한 DataTable 의 Outter Join (0) | 2019.10.18 |
Comments