The C Programming Language in a Linux Environment Tutorial 1 : Getting Started

in hive-154244 •  4 years ago 

Original Page: http://csprogramming.center/2018/03/23/an-introduction-to-c-with-linux/

Requirements:

  • Linux Distribution (obviously)
  • A text editor : Vim, Emacs, gedit, kate, atom
  • A Compiler : gcc or clang
  • A Shell: bash, xterm, sh

My setup for this tutorial:

  • Debian GNU/Linux 9.4
  • Atom (Text editor)
  • gcc version 6.3.0-18+deb9u1 (Debian’s Release version for Debian 9.1)
  • bash

Preface:

First I would like to say, this tutorial series will teach you how to navigate using the shell you are using, as well as writing efficient C code and learning to analyze it. C was designed in 1972 by Dennis Richie, and developed by him and Bell Labs. It was used to develop the Unix Operating System, and since, has become one of the most widely used programming languages of all times. It is an imperative procedural language that is pretty straight forward. It was designed to be compiled using a relatively straight forward compiler, to provide low level access to memory.

First Steps:

Open up your shell and create a project folder.

clim@clim:~/Desktop$ mkdir IntroToC
clim@clim:~/Desktop$ cd IntroToC
clim@clim:~/Desktop/IntroToC$ touch main.c

For those who dont know, mkdir will create a project folder in the specified directory you are currently in. I then use the cd command to change directories and then the touch command to create the file. If you want, you could use the command of a text editor to do the same, for example:

clim@clim:~/Desktop/IntroToC$ gedit main.c

This will open up main.c and you can start from there. Now type the following:

#include <stdio.h>
 
int main(int argc,char * argv[]){
printf("Hello World!\n");
return 0;
}

Lets dissect this line by line:

#include <stdio.h>

This is a preprocessor macro that tells the compiler to “Take this header file and place it at the start of this source code”. When you are using system header files (such as stdio.h, which is in your C Runtime Library’s header folder), you always want to use the less-then and greater-then signs to encapsulate the file you want to include, but when you are including your own header files (ones that you write yourself), you need to use quotations like such:

#include "foo.h"
int main(int argc, char * argv[])

This is a function definition of main. There are certain standards to go by when writing a C Program. The C Standard has a chapter for declaring main:

5.1.2.2.1 Program startup
The function called at program startup is named main. The implementation
declares no prototype for this function. It shall be defined with a
return type of int and with no parameters:
int main(void) { /**/ }
or with two parameters (referred to here as argc and argv , though any names may
be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /**/ }
or equivalent;

You may see other programs and tutorials with the use of void main(), however this is a terrible programming practice and should not be done, unless you are working in a freestanding environment (which i may go over later in this series).

printf("Hello World!\n")

Printf is the most known way of printing formatted data, it takes in a string of characters, and can take in variables that need to be printed. This prints Hello World! on the screen. I use 1 formatting specifier in this example, the ‘\n’ character is the new-line operator and is used to skip to the next line of the terminal. This is to stop the shell from returning on that line, i will show you what i mean later in this tutorial.

return 0;

When your function ends, if it is of a specific type, it must return something. In this case, your just exiting the program normally, so you return 0
Now that you have finished typing that, save it and go back to your folder in your shell. Then run the following :

clim@clim:~/Desktop/IntroToC$ gcc main.c -o test1

What you are doing here is telling gcc to compile main.c into object code, and then link it to the C Runtime as test1. After this you can run the ./test1 program to get:

clim@clim:~/Desktop/IntroToC$ ./test1
Hello World!
clim@clim:~/Desktop/IntroToC$

See, if i didnt include the newline character and just used printf(“Hello World!”) the result would look like this:

clim@clim:~/Desktop/IntroToC$ ./test1
Hello World!clim@clim:~/Desktop/IntroToC$

That ends the Introduction to C .

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!