Menu
CoddyTech
コース

C#開発者のためのC

C#からC17へステップアップしましょう。明示的なインターフェース、ポインター、配列、文字列、所有権、構造体を学びます。複数ファイルで構成される在庫台帳を作成し、完成したプログラムを2つ移植します。

C#とCを並べて比較

このコースのレッスンから、同じプログラムをC#とCで並べています。

  1. 出力とコメント

    C#の複合書式指定では、マネージド値を読み取ります。Cのprintfでは可変個引数と変換指定子を使用します。引数の型が一致しない場合、マネージドの書式指定例外ではなく、未定義の動作が発生することがあります。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6
    7static void Main(){8Console.WriteLine("value={0}",7);9}10}
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10printf("value=%d\n",7);11return 0;12}
  2. 真偽値と分岐

    C#では条件にbool型が必要です。Cではスカラー値を真偽値として判定でき、空文字列を指す非nullポインターはtrueです。空かどうかを調べるには、終端文字または長さを確認する必要があります。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6
    7static void Main(){8string s="";Console.WriteLine(s.Length==0?"true":"false");9}10}
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10const char *s=""; printf("%s\n",s?"true":"false");11return 0;12}
  3. 配列には長さが必要

    C#の配列は、Lengthプロパティを持つ参照オブジェクトです。Cのポインタ引数には配列の長さが含まれないため、長さを明示的に渡してください。また、sizeofを要素数の取得に使えるのは、実際の配列型が見えている場合に限られます。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6
    7static void Main(){8int[] a={2,4,6};Console.WriteLine(a.Length);9}10}
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10int a[]={2,4,6};printf("%zu\n",sizeof a/sizeof a[0]);11return 0;12}
  4. 関数の引数は値

    通常のC#のパラメーターとCのパラメーターは値を渡します。C#にはrefモードとoutモードもありますが、Cでは代わりにポインターパラメーターを明示して、このような更新を表現します。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6static void Change(int x){x=9;}7static void Main(){8int x=2;Change(x);Console.WriteLine(x);9}10}
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8static void change(int n){n=9;(void)n;}9int main(void) {10int x=2;change(x);printf("%d\n",x);11return 0;12}
  5. アドレスと間接参照

    C# の ref は、通常のマネージドコードでチェック付きの参照渡しアクセスを可能にします。C ではアドレスの取得と間接参照を直接行いますが、ポインターの有効期間や借用ルールは強制されません。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6static void Set(ref int n){n=5;}7static void Main(){8int n=2;Set(ref n);Console.WriteLine(n);9}10}
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10int n=2;int *p=&n;*p=5;printf("%d\n",n);11return 0;12}
  6. ステータス結果を伴う解析

    C#のTryParseは、成功したかどうかと出力値を分けて扱います。Cのsscanfは代入されたフィールドを報告し、接頭部分を受け付けることがあります。代入に成功しただけでは、入力全体が有効であるとは確認できません。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6
    7static void Main(){8int n;Console.WriteLine(int.TryParse("12x",out n)?"valid":"invalid");9}10}
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10const char *s="12x";int ok=*s!=0;for(size_t i=0;s[i];i++)if(s[i]<'0'||s[i]>'9')ok=0;printf("%d\n",ok);11return 0;12}

シラバス

10 章48 レッスン1 プロジェクト48 チャレンジ99 クイズ問題

条件分岐とループ

5 レッスン

最後のソースコード移植

2 レッスン

C#開発者向けのほかのコース

ほかの言語からCを学ぶ

よくある質問

C#開発者のためのCはどんな人向けですか?

すでにC#を書けて、Cを学びたい人向けです。各レッスンは知っているC#のコードから始まり、Cで同じことをどう書くかを示すので、初心者向けの基礎は飛ばせます。

Cの知識は必要ですか?

いいえ。コースは最初のCプログラムから始まります。必要なのはC#の基礎(変数、条件分岐、ループ、関数、コレクション)です。

C#開発者のためのCにはレッスンがいくつありますか?

C#開発者のためのCには48のインタラクティブなレッスンがあり、自分のペースで進められます。

C#開発者のためのCでは何を学べますか?

コースは次の章で構成されています: コンパイルと計算, 条件分岐とループ, 配列とテキスト, 関数とインターフェース, and more。

C#開発者のためのCは無料ですか?

はい。Coddyのすべてのコースは無料で開始できます。レッスンはブラウザで進められ、ダウンロードも登録も不要です。

Coddyのプログラミング言語のイラスト

C#開発者のためのCを無料で始める

始める