How to write linux programs

How To Write, Compile and Execute C Program on Unix OS [With Hello World Example]

Question: I would like to understand the basics of how to write, compile and execute a C program on Linux OS. Can you explain it with a simple example? Answer: In this article, let us review very quickly how to write a basic Hello World C program and how to compile *.c program on Linux or Unix OS.

1. Write a Hello World C Program

$ vim helloworld.c /* Hello World C Program */ #include main()

2. Make sure C Compiler (gcc) is installed on your system

$ whereis cc cc: /usr/bin/cc /usr/share/man/man1/cc.1.gz $ which cc /usr/bin/cc $ dpkg -l | grep gcc ii gcc 4:4.3.3-1ubuntu1 The GNU C compiler ii gcc-4.3 4.3.3-5ubuntu4 The GNU C compiler ii gcc-4.3-base 4.3.3-5ubuntu4 The GNU Compiler Collection (base package) ii gcc-4.3-doc 4.3.3-5ubuntu4 Documentation for the GNU compilers (gcc, go ii gcc-4.3-locales 4.3.3-5ubuntu4 The GNU C compiler (native language support ii gcc-4.3-multilib 4.3.3-5ubuntu4 The GNU C compiler (multilib files) ii lib64gcc1 1:4.3.3-5ubuntu4 GCC support library (64bit) ii libgcc1 1:4.3.3-5ubuntu4 GCC support library

3. Compile the helloworld.c Program

$ cc helloworld.c $ ls -l -rw-r--r-- 1 ramesh ramesh 71 2009-08-28 14:06 helloworld.c -rwxr-xr-x 1 ramesh ramesh 9152 2009-08-28 14:07 a.out

4. Execute the C Program (a.out)

You can either execute the a.out to see the output (or) rename it to some other meaningful name and execute it as shown below.

$ ./a.out Hello World! $ mv a.out helloworld $ ./helloworld Hello World!

If you enjoyed this article, you might also like..

Comments on this entry are closed.

Do you know why this appear:
$ cc helloworld.c
helloworld.c:3:10: error: #include expects “FILENAME” or
helloworld.c: In function ‘main’:
helloworld.c:7: warning: incompatible implicit declaration of built-in function ‘printf’ ?

I guess the compile-error is kind of easter egg from you =). $ cc helloworld.c
helloworld.c:3:9: error: #include expects “FILENAME” or
helloworld.c: In function ‘main’:
helloworld.c:7: warning: incompatible implicit declaration of built-in function ‘printf Just to explain the first error (on line 3 of the file): We didn’t tell the #include directive “What” to include. It expects some header file. The one we probably want is stdio.h, for it includes the declaration of the printf function, which we use on line 7 and which is the one the compiler is complaining about in the second error (crying the declaration can’t be found in other words =).
As it is System header file we add it in (don’t know how they’re called in eng =). I call this “great time to patch your first program” =))
This is the patch file helloworld.patch:
3c3
#include To apply this simply type:
$ patch helloworld.c < helloworld.patch Then compile the file again and enjoy =)

Читайте также:  L2tp ipsec linux client gui

I’m not a programmer. I tested your c program which did not compile. I searched google and found this line of yours may be incorrect:
#include
which should be
#include
With “#include” the program compiled. Rich

Hello again. I don’t think I mistyped that second #include. I think your web site removes Here it is in parenthesis (studio.h) just in case it is removed in the above sentence. Rich

Confirmed. Your web site removes angle brackets, aka diamond brackets, aka cone brackets or aka chevrons. I had to look that up too.

By the way, I am using Firefox 3.0.12 on a newly installed Centos 5.3. Perhaps other web browsers will show the angle brackets.

@All, I’ve corrected it now. Thanks a lot for pointing out the issue. You all are right. It’s because of less-than and greater-than symbol in stdio.h did not get displayed properly in the blog post.

when I comepile a sourcefile, the error message keeps appearing and says that there is an implicit declaration of function printf. I don’t know how to fix it. Please help!

Источник

How to Write and Run a C Program in Linux

Linux C Programming

C is one of the world’s oldest, most widely-used programming languages. It has been used to develop countless applications, from operating systems to embedded devices. Even today, many developers still rely on C for its flexibility and reliability as a programming language.

The C Programming Language

C is a highly portable language. This means that programs written in C are easily transferable from one computer system to another without having to be rewritten from scratch. This allows developers to quickly port their applications across different platforms without too much extra effort or hassle.

Also, C offers an outstanding balance between high-level abstractions and low-level programming techniques. In other words, you can use pre-defined functions and libraries that handle more complex tasks like memory management or string manipulation while still being able to access the machine’s hardware directly if needed. This makes it suitable for almost any application ranging from large commercial projects to smaller hobby projects!

Читайте также:  Узнать вес файла linux

C has been around for over 40 years, so plenty of resources are available online and offline that provide helpful tutorials and detailed documentation on how best to use the language. Whether you’re just starting with programming or already an experienced programmer looking for some tips – you should have no trouble finding what you need when looking into C development!

Finally, unlike many other modern programming languages, C isn’t tied to just one platform or software framework – it can be used with almost every major Operating System (OS), including Windows, macOS, Linux, and even Android! This universality makes it an extremely versatile option compared to other languages, which may only be ideal for certain OSes or environments.
C is truly one of the most significant programming languages ever created, and its popularity doesn’t seem like it will diminish anytime soon! With its flexibility, portability, and ease of use – it’s no wonder why so many people choose this powerful language when building their software projects!

Install C Compiler (GCC) on Linux

We have run the steps and commands mentioned in this article on an Ubuntu 22.04 LTS system, but it works the same on other versions like Ubuntu 20.04 or Debian 11.

To compile a simple C program, we use the Linux command-line tool, the terminal. To open the terminal, you can use the Ubuntu Dash or the key combination Ctrl+Alt+T.

Install the build-essential packages

In order to compile and execute a C program, you need to have the essential packages installed on your system. Enter the following command as root in your Linux Terminal:

$ sudo apt install build-essential

Install gcc compiler that is part of build-essential package with apt

You will be asked to enter the root password; the installation will begin after that. Please make sure that you are connected to the internet.

Write a simple C program

After installing the essential packages, let us write a simple C program.

Open Ubuntu’s graphical Text Editor and write or copy the following sample program into it:

Then save the file with .c extension. In this example, I am naming my C program as sampleProgram.c

Example C program

Alternatively, you can write the C program through the Terminal in gedit as follows:

This will create a .c file to write and save a program.

Compile the C program with GCC Compiler

In your Terminal, enter the following command to make an executable version of the program you have written:

$ gcc [programName].c -o programName
$ gcc sampleProgram.c -o sampleProgram

Compile source code with gcc

Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate paths in this command.

Run the program

The final step is to run the compiled C program. Use the following syntax to do so:

Читайте также:  Создать свой хостинг linux

Start our compiled C program

You can see how the program is executed in the above example, displaying the text we wrote to print through it.

This article taught you how to write, compile and run a simple C program in Linux. All you need is the essential packages and skills to make you a programming guru in Linux!

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

How to Write and Run a C Program on Ubuntu

C language is one of the earliest programming languages. It is simple and easy to learn. Formerly C programming was performed on Turbo C, a discontinued integrated development environment. But nowadays it can be easily executed on different operating systems.

In this guide, you will learn how to write your first C program using the Linux operating system which requires just the GNU C compiler and a text editor and not a full blown integrated development environment to get started. The following steps will show to install a GNU C compiler on Linux, how to write the source code, compile and execute the C program.

Requirements

To execute a C program on Linux, you need to install the following :

  1. GNU C and C++ compiler collection
  2. Development tools
  3. Development libraries
  4. IDE or text editor to write programs

Step 1: Installation of the compiler on Linux

For C programming it is essential to install a compiler that will compile the program before executing. In Linux operating system type the following command in terminal to install the GNU C compiler which is one of the most famous compilers for Linux:

$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

Install C Compiler and Toolchain

Step 2: Verification of installation

To ensure that your compiler is installed type the given command to find its location and version number:

Check GCC C-Compiler version

Which GCC

GCC

Step 3: Open text editor on Linux

To start writing your source code, open a text editor by typing

Step 4: Write your first C program

Write the C source code given below in text editor:

#include

main()
printf("This is my first C program on ubuntu\n");
>

My First C Program

Step 5: Compile your program using GNU C compiler

The following command will invoke the GNU C compiler to compile the file program.c and output (-o) the result to an executable called program.

Compile sourcecode with GCC

Step 6: Execute your program

Type the given command to display output:

Run binary program

Wrapping Up

The above guide demonstrates how to write your first C program in Linux by installing GNU C compiler. To know more about C programming in Linux explore different books and practice more source code related to different topics in C programming.

About

OSNote covers tutorials and stories about Linux and OpenSource Software.

Источник

Оцените статью
Adblock
detector