C for C++ Developers
Move from C++ to C17: explicit interfaces, pointers, arrays, strings, ownership and structs. Build a multi-file stock ledger and port two complete programs.
C++ and C side by side
The same program in C++ and in C, from the lessons of this course.
Printing and Comments
C++ stream insertion resolves typed overloads. C printf relies on a variadic format contract; incorrect conversion specifiers can cause undefined behavior. There is no C ostream abstraction.
C++1#include <iostream>2#include <iomanip>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <memory>8#include <algorithm>9#include <functional>10#include <variant>11#include <sstream>12 13int main(){14std::cout << "value=" << 7 << "\n";15}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
C++ std::string has an emptiness API and no implicit bool conversion. A C string is usually accessed through a pointer; even an empty string has a true condition when that pointer is non-null.
C++1#include <iostream>2#include <iomanip>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <memory>8#include <algorithm>9#include <functional>10#include <variant>11#include <sstream>12 13int main(){14std::string s="";std::cout << std::boolalpha << s.empty() << "\n";15}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
C++ std::array retains its extent in a value type with size(). C raw arrays have an extent in their type but decay to pointers in parameters. C cannot use template deduction to preserve a generic array bound.
C++1#include <iostream>2#include <iomanip>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <memory>8#include <algorithm>9#include <functional>10#include <variant>11#include <sstream>12 13int main(){14std::array<int,3> a{2,4,6};std::cout<<a.size()<<"\n";15}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
C++ supports both value and reference parameters. C has only value parameters; copying a pointer allows access to its pointee but does not create C++ reference binding semantics.
C++1#include <iostream>2#include <iomanip>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <memory>8#include <algorithm>9#include <functional>10#include <variant>11#include <sstream>12void change(int x){x=9;(void)x;}13int main(){14int x=2;change(x);std::cout<<x<<"\n";15}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
C++ reference assignment writes the referred-to object; references are not reseated like pointers. In C, taking an address and dereferencing are explicit. Both raw pointers require a live object behind the address.
C++1#include <iostream>2#include <iomanip>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <memory>8#include <algorithm>9#include <functional>10#include <variant>11#include <sstream>12 13int main(){14int n=2;int& alias=n;alias=5;std::cout<<n<<"\n";15}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
C++ stoi can accept a prefix and exposes consumed length. C strtol similarly exposes an end pointer, while sscanf counts assignments. In both languages, conversion success alone need not mean full-input validity.
C++1#include <iostream>2#include <iomanip>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <memory>8#include <algorithm>9#include <functional>10#include <variant>11#include <sstream>12 13int main(){14std::size_t used=0;int n=std::stoi("12x",&used);std::cout<<n<<":"<<used<<"\n";15}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 C++ Developers for?
For people who already write C++ and want to learn C. Each lesson starts from C++ 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 C++ basics: variables, conditions, loops, functions and collections.
How many lessons does C for C++ Developers have?
C for C++ Developers contains 48 interactive lessons you can complete at your own pace.
What does C for C++ 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 C++ Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.