C and C++ are general-purpose programming languages. They are old but mastering them is still great to have. They are hard to replace. We can replace Java, Python, PHP, and NodeJS to Go for back-ends. That was what most startups did. But in the operating system and game development industries, C/C++ is a preferable programming language. Especially C++ is the favorite programming language for competitive programming.
C/C++ is the first programming language that I learned. At that moment, the resources to learn those languages are still limited. I thought they were hard to learn. In this article, I hope we can learn the basics of C and C++. Welcome to the C/C++ crash course.
Prerequisites
Text Editor
I used VS Code here along with the C/C++ extension from Microsoft. You can use another text editor such as Code::Blocks.
Compiler
We need to install compilers like g++ and gcc because C/C++ is a compiled programming language. They are pre-installed for Linux and macOS. For Windows, you can use MinGW. You can use g++ to compile C files. But you will a warning. So, that's why I compile C files with gcc.
Hello World!
Let's get started with Hello World! Write the code below, let's say hello.cpp, and compile it with the command g++ hello.cpp -o hello. The -o is to name the executable file to be hello.
They include different header libraries even if the syntax is similar. iostream for C++ and stdio.h for C, they bring standard input and output functions.
We put using namespace std;, standard namespace library, at the top in C++ files. We need to put std:: in every function like std::cout if we don't.
The main difference between C++ and C is how we printed the string. We use escape sequences to print new lines like \n.
We can see those main functions always return int 0. That means our program runs successfully. To flag our program contains an error return a number that is greater than 0.
#include<iostream>#include<ctime>usingnamespacestd;voidisEven(intnum){if(num%2==0){cout<<num<<" is even."<<endl;}else{cout<<num<<" is odd."<<endl;}}voidgreetings(){time_tnow=time(0);tm*ltm=localtime(&now);switch(ltm->tm_hour){case1...11:cout<<"Good morning!"<<endl;break;case12...16:cout<<"Good afternoon."<<endl;break;default:cout<<"Good evening."<<endl;}}intmain(){for(inti=0;i<5;i++){for(intj=0;j<=i;j++){cout<<"*";}cout<<endl;}intsum=1;while(sum<1000){sum+=sum;}cout<<sum<<endl;intnum;cout<<"Put a number: ";cin>>num;isEven(num);greetings();return0;}
#include<stdio.h>#include<time.h>voidisEven(intnum){if(num%2==0){printf("%d is even.\n",num);}else{printf("%d is odd.\n",num);}}voidgreetings(){time_tnow=time(0);structtm*ltm=localtime(&now);switch(ltm->tm_hour){case1...11:printf("Good morning!\n");break;case12...16:printf("Good afternoon.\n");break;default:printf("Good evening.\n");}}intmain(){for(inti=0;i<5;i++){for(intj=0;j<=i;j++){printf("*");}printf("\n");}intsum=1;while(sum<1000){sum+=sum;}printf("%d\n",sum);intnum;printf("Put a number: ");scanf("%d",&num);isEven(num);greetings();return0;}
#include<iostream>usingnamespacestd;structperson{stringname;intage;};intmain(){personp1;p1.name="John Doe";p1.age=1e1;personp2={"",999};p1.age+=8;cout<<p1.name<<" "<<p1.age<<endl;// John Doe 18
cout<<p2.name<<" is "<<p2.age<<" years old."<<endl;// is 999 years old.
return0;}
#include<stdio.h>structperson{char*name;intage;};intmain(){structpersonp1;p1.name="John Doe";p1.age=1e1;structpersonp2={"",999};p1.age+=8;printf("%s %d\n",p1.name,p1.age);// John Doe 18
printf("%s is %d years old.\n",p2.name,p2.age);// is 999 years old.
return0;}
In C and Go, we can create an object from the struct. But in C++, there is a syntax for that, class. We will cover that later.
#include<iostream>usingnamespacestd;unionperson{charinitial;intage;};intmain(){personp1;p1.initial='J';p1.age=1e1;personp2={.age=999};p1.age+=8;cout<<p1.initial<<" "<<p1.age<<endl;// 18
cout<<p2.initial<<" is "<<p2.age<<" years old."<<endl;// � is 999 years old.
return0;}
#include<stdio.h>unionperson{charinitial;intage;};intmain(){unionpersonp1;p1.initial='J';p1.age=1e1;unionpersonp2={.age=999};p1.age+=8;printf("%c %d\n",p1.initial,p1.age);// 18
printf("%c is %d years old.\n",p2.initial,p2.age);// � is 999 years old.
return0;}
A union is a syntax that holds only one property or member because all members share the same memory location.
In C++, we need to use a pointer of a string as a member of a union because a real string can cause an error.
#include<iostream>usingnamespacestd;enumColor{RED,GREEN,BLUE};intmain(){intnumber;cout<<"Input a number between 0-2: ";cin>>number;Colorcolor=static_cast<Color>(number);switch(color){caseRED:cout<<"Red"<<endl;break;caseGREEN:cout<<"Green"<<endl;break;caseBLUE:cout<<"Blue"<<endl;break;default:cout<<"Undefined"<<endl;}return0;}
#include<stdio.h>enumColor{RED,GREEN,BLUE};intmain(){intnumber;printf("Input a number between 0-2: ");scanf("%d",&number);switch(number){caseRED:printf("Red\n");break;caseGREEN:printf("Green\n");break;caseBLUE:printf("Blue\n");break;default:printf("Undefined\n");break;}return0;}
Enumeration basically is a number with a defined variable.
In C++, we can use static_cast to convert data type.
Take a look at our example in C. number is int but cases are Color, they are compatible.
A vector is just like an array in C++. Most C++ developers are more convenient with vectors rather than real arrays.
You can find the completed source code of these examples on https://github.com/aristorinjuang/c-cpp-crash-course. There is a Makefile. That is a shortcut to compile and run those programs. We can run it by typing make <target> like make hello.