Mac Installation

Lesson 3
Author : Afrixi
Last Updated : December, 2022
C - Programming Language
This course covers the basics of programming in C.

Installing C on a Mac is relatively straightforward, and there are several ways to do it. Here is a more detailed explanation of the steps to install C on a Mac for beginners:

Install Xcode: Xcode is Apple’s integrated development environment (IDE) that includes a C compiler and other tools for developing software on Mac. To install Xcode, go to the App Store on your Mac and search for “Xcode”. Once you have found it, click on the “Get” button and follow the prompts to install Xcode. Note that Xcode is a large download, so it may take some time to complete.

Install the command line tools: After installing Xcode, you need to install the command line tools, which includes the C compiler and other command line utilities. To do this, open the Terminal app on your Mac and enter the command “xcode-select –install”. This will prompt you to install the command line tools. Follow the prompts to complete the installation.

Write a C program: Open a text editor such as TextEdit or Sublime Text and write a simple C program, such as the “Hello, World!” program. This program simply displays the text “Hello, World!” on the screen. Here’s an example of the program:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Save the C program: Save the C program with a .c extension, such as “hello.c”. Be sure to save it in a location that you can easily find later.

Compile the C program: Open the Terminal app on your Mac and navigate to the folder where the C program is saved using the “cd” command. For example, if your program file is saved in the “Documents” folder, you would enter “cd Documents” at the command prompt. Then, enter the command to compile the program, which typically involves running the “gcc” command with the name of the program file as an argument. For example, if your program file is called “hello.c”, you would enter the command “gcc hello.c -o hello” to compile the program and create an executable file. The “-o” flag tells GCC to name the output file “hello”.

Run the C program: After compiling the program, you can run it by entering the name of the executable file at the command prompt. For example, if your executable file is called “hello”, you would enter the command “./hello” to run the program. The program should display the text “Hello, World!” on the screen.

With these steps, beginners should be able to successfully install and run C on their Mac. Note that there are other C compilers available for Mac, such as Clang, that can also be installed and used in a similar manner.