Menu
CoddyTech
Course

C for Kotlin Developers

Move from Kotlin to C17: explicit interfaces, pointers, arrays, strings, ownership and structs. Build a multi-file stock ledger and port two complete programs.

Kotlin and C side by side

The same program in Kotlin and in C, from the lessons of this course.

  1. Printing and Comments

    Kotlin string templates call toString() on any value, and String.format checks each argument at runtime, throwing IllegalFormatConversionException when %d receives a String. C printf trusts the variadic format contract: a conversion that does not match the…

    Kotlin
    1fun main() {2    val n = 73    // A string template also works: "value=$n"4    println("value=%d".format(n))5}
    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) {10    int n = 7;11    /* %d must receive an int */12    printf("value=%d\n", n);13    return 0;14}
  2. Booleans and Branches

    Kotlin conditions must be Boolean: if (s) does not compile for a String?, so you write s != null, s.isEmpty() or s.isNullOrEmpty(). C accepts any scalar as a condition, and a pointer to an empty string is non-null and therefore true.

    Kotlin
    1fun main() {2    val s: String? = ""3    println(s != null)4    println(s.isNullOrEmpty())5}
    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) {10    const char *s = "";11    printf("%s\n", s ? "true" : "false");12    printf("%s\n", s[0] == '\0' ? "true" : "false");13    return 0;14}
  3. Arrays Need Lengths

    Kotlin IntArray and Array<T> are objects that carry their size, and passing one to a function passes that size along. A C array passed to a function becomes a pointer to its first element and loses the bound, so pass a size_t length explicitly; there is no…

    Kotlin
    1fun main() {2    val a = intArrayOf(2, 4, 6)3    println(a.size)4}
    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) {10    int a[] = {2, 4, 6};11    printf("%zu\n", sizeof a / sizeof a[0]);12    return 0;13}
  4. Function Arguments Are Values

    Kotlin passes every argument by value: an Int's value, or a copy of a reference to an object. Its parameters are read-only, so x = 9 inside a function does not even compile, while the function can still change a MutableList through the copied reference. C…

    Kotlin
    1fun change(items: MutableList<Int>, x: Int) {2    // x = 9 would not compile: parameters are read-only3    items[0] = x + 74}5
    6fun main() {7    val items = mutableListOf(1)8    val x = 29    change(items, x)10    println("$x ${items[0]}")11}
    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. Addresses and Dereferencing

    Kotlin has no address-of operator: objects are reached through references, two variables can alias one object, and a local Int cannot be referenced at all, so sharing a mutable number means wrapping it in an object. The garbage collector keeps an object alive…

    Kotlin
    1class Cell(var value: Int)2
    3fun main() {4    val cell = Cell(2)5    val alias = cell6    alias.value = 57    println(cell.value)8}
    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. Parsing with Status Results

    Kotlin's toIntOrNull and matches(Regex(...)) both judge the whole string: "12x" gives null and false. C sscanf with %d may successfully consume only a numeric prefix and report success; full-input validation must be a separate, explicit part of the parser…

    Kotlin
    1fun main() {2    val s = "12x"3    println("${s.toIntOrNull()} ${s.matches(Regex("[0-9]+"))}")4}
    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}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

Final Source Ports

2 lessons

More courses for Kotlin developers

Learn C from another language

Frequently asked questions

Who is C for Kotlin Developers for?

For people who already write Kotlin and want to learn C. Each lesson starts from Kotlin you know and shows how C does the same thing, so you skip the beginner basics.

Do I need to know C already?

No. The course starts with your first C program. You need Kotlin basics: variables, conditions, loops, functions and collections.

How many lessons does C for Kotlin Developers have?

C for Kotlin Developers contains 48 interactive lessons you can complete at your own pace.

What does C for Kotlin Developers cover?

The course is organized into chapters covering: Compile and Compute, Decisions and Loops, Arrays and Text, Functions and Interfaces, and more.

Is C for Kotlin Developers free?

Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.

Coddy programming languages illustration

Start C for Kotlin Developers for free

Get started