Lesson 2. Writing The First Program

Overview

  • Write your first C/C++ Program

Before we start

I will be using Xcode 12.4 on macOS 11.2.3, running on intel-based MacBook Pro 13 inch 2019. Due to the odd nature of visual studio’s C compiler, some codes might not work on them. I highly recommend using online IDE using gcc compiler.

Start to code!

Launch Xcode

When you launch Xcode, you will see the screen like this: Xcode 1

Create a new Xcode project

When you press on “Create a new Xcode project”, you will be asked to choose the type of the project. There is an option called “Command Line Tool” under macOS tab. Xcode 2 After that, name your new project and put Organization Identifier you want (I have it as “Personal”). Also you should select the programming language as C.

Start coding

On the side bar, there will be a file called main.c. If you click on it, you will be able to start coding! Xcode 3

Coding

What are we going to make our program to do?

Since this would be the very first writing the C code, we will begin with printing something to the screen.

Procedure

  1. Erase everything from the screen
  2. Type
  3. Press Build and Run button (It will look like a youtube logo but only with triangular part)
  4. You will see text Very first program I wrote in C! at the bottom of the screen

What just happened?

You have wrote your very first program! Lets go line by line.

First Line

1
#include<stdio.h>

This line is importing the header called stdio, which is containing the functions for STandarD Input Ooutput.

Second Line

1
int main(){

This is where you define the main function. It is the function that the compiler(computer) will be running the program from. This function will be ran initially from your code in every C program.

Third Line

1
printf("Very first program I wrote in C!\n");

printf is the function included in stdio header. This function prints the text inputted to it on the screen. In this case, inputted text will be “Very first program I wrote in C!”. You should remember to put the semicolon(;) at the end of each operation, since it is indicating that each command is finished. Lastly, \n will change line in the text.

Fourth line

1
return 0;

When we stated the main function, we said int main() specifically. int in the front is telling that the main function’s type is integer, meaning there should be an integer return value. In this line of code, we are returning the integer 0 to end the program. return statement in the main function will immediately terminate the program. When you see the output, it will say Program ended with exit code: 0 at the end of it. This means the return value of the main function was 0.

Last line

1
}

This line only means that main function’s content is over.

Task

Try to write a program which prints your name and today’s date in separated lines. Also, you are asked to make the exit code as 8. For example, mine would look like:

1
2
3
andylang8445
11 March, 2021
Program ended with exit code: 8

The sample code of mine would be included below. I recommend trying this on your own and check my answer of mine.

Next Lesson

Next Lesson (Lesson 3) will be about input at handling data.