Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

닌자고양이

[C#] C#에서 C언어의 printf, scanf, getch 사용하기 본문

C# .NET

[C#] C#에서 C언어의 printf, scanf, getch 사용하기

닌자고양이 2020. 6. 25. 16:00
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) 정의

 

Comments