C for Swift Developers
Move from Swift to C17: explicit interfaces, pointers, arrays, strings, ownership and structs. Build a multi-file stock ledger and port two complete programs.
Swift and C side by side
The same program in Swift and in C, from the lessons of this course.
Printing and Comments
Swift string interpolation is checked at compile time: \(value) accepts any value and formats it through its description, so a type mismatch cannot happen. C printf trusts a variadic format string instead, and a conversion that does not match its argument can…
Swift1print("value=\(7)")C1#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}Booleans and Branches
Swift conditions must be Bool: an Int, an optional or a String is never tested for truthiness, so you write != nil, if let or isEmpty. C accepts any scalar condition, so a non-null pointer to an empty string is true; whether a pointer exists and whether its…
Swift1let s = ""2print(s.isEmpty)C1#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}Arrays Need Lengths
A Swift Array is a value that carries its count, and an ArraySlice carries its own bounds, wherever you pass them. A C array becomes a pointer when passed to a function, and that pointer carries neither a length nor a checked lifetime; pass the size…
Swift1let a = [2, 4, 6]2print(a.count)C1#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}Function Arguments Are Values
Swift parameters are constants: assigning to one is a compile error, so a function that needs a scratch copy shadows it with var, and changing the caller's variable takes an inout parameter and &x at the call site. C always copies argument values, lets a…
Swift1func change(_ n: Int) {2 var n = n3 n = 94 _ = n5}6var x = 27change(x)8print(x)C1#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}Addresses and Dereferencing
Swift's &n on an inout argument looks like C's address-of, but it grants exclusive access only for the duration of the call, and the compiler never lets that access escape. Raw pointers exist as UnsafeMutablePointer, marked unsafe by name. A C int * can…
Swift1func setFive(_ p: inout Int) {2 p = 53}4var n = 25setFive(&n)6print(n)C1#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}Parsing with Status Results
Swift's Int("12x") is a failable initializer: it returns nil unless the whole string is a valid integer, so "12x", " 12" and an empty string all fail. C prefix parsers such as strtol, atoi and sscanf can succeed on a leading 12 before trailing bytes…
Swift1print(Int("12x") != nil)C1#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 questionsCompile and Compute
6 lessonsDecisions and Loops
5 lessonsArrays and Text
5 lessonsFunctions and Interfaces
5 lessonsPointers and Structs
5 lessonsValidation and Text Buffers
5 lessonsOwned Dynamic Memory
5 lessonsTagged Data and Composition
6 lessonsProject: Stock Ledger
4 lessonsFinal Source Ports
2 lessonsFrequently asked questions
Who is C for Swift Developers for?
For people who already write Swift and want to learn C. Each lesson starts from Swift 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 Swift basics: variables, conditions, loops, functions and collections.
How many lessons does C for Swift Developers have?
C for Swift Developers contains 48 interactive lessons you can complete at your own pace.
What does C for Swift 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 Swift Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.