Writing Your First C Program in Linux

This topic was published by and viewed 652 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Hooman Aminvand
    Hooman Aminvand
    Participant
    • Topics - 11
    • @hooman

    In this article, we will write a sample program and compile it!

    Open your file manager and create a folder (for example create "lab").

    Create-Folder
    Create-Folder

    Then, inside the lab folder, create an empty file and name it "helloworld.c".

    Create helloworld.c file
    Create helloworld.c file

    Next, click and open helloworld.c (it can open with any text editor application; here, I use Gedit)
    and enter the following instructions.

    /*http://dcjtech.info*/
    #include <stdio.h>
    main()
    {
        printf("Hello My Lovely World!");
    }

    Here, you can see my colorful page! The colors help programmers.

    Source-Code
    Source-Code

    Information About C Programming

    C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs, and used to re-implement the Unix operating system. it has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 and subsequently by the International Organization for Standardization (ISO).

    Read more at https://en.wikipedia.org/wiki/C_(programming_language)

    Comments are programmer notes. They just show in your code and not in the compiled program. You must place your notes between /* */ (for several lines). For example,

    /*
    your
    notes
    */
    // for one line and short notes
    /* you can also use for both */

    In C, there is difference between lowercase letters and uppercase letters! So, ABC!=abc. All instructions must begin with a lowercase letter.

    All code in functions are put between "{" and "}". All lines must end in ";" to end each instruction. However, there are some exceptions.

    Here, you can see one example.

    func1
    {
    command 1 ;
    command 2;
       command 3
         continue of command 3 ;
    }

    Instructions and libraries that are pre-written can help programmers. Preprocessor commands and instructions of "include" start with "#", then library names go between "<>". For example, #include . With this instruction, we attach the stdio library to our program. All libraries in C use the ".h" suffix.

    All programs have several functions, but we have just one "main()" function that is used in C.

    How to Compile the Program?

    To compile your program, open terminal and go to the program's file directory (in the past, we created "lab" in home) so, type cd /home/lab (cd = change directory).

    cd shell command
    cd shell command

    Now, we are in "lab". Next, enter ls in the terminal to list the files.

    ls shell command
    ls shell command

    Yeah! You can see the helloword.c file. Now, enter gcc helloworld.c -o lab to compile.

    Compiling with GCC
    Compiling with GCC

    Okay! We successfully compiled it. Now, open the lab folder and you can see the new file.

    Programming Files
    Programming Files

    Okay, go back again to the terminal and type "./lab" and press enter. You will then see the program's output.

    Execute-Program
    Execute-Program

    Welcome, you’re now a programmer!

    Further Reading

    Attachments:
    1. Create-Folder
    2. Create helloworld.c file
    3. Source-Code
    4. cd shell command
    5. ls shell command
    6. Compiling with GCC
    7. Programming Files
    8. Execute-Program
Viewing 1 post (of 1 total)