How to see output of C program in Linux or UNIX

See all UNIX related articles/faq
I am a new user of Linux operating system but not to C programming language. At my university we are using Turbo C compiler under DOS/Windows XP to write and compile C programs. Under Linux I wrote a small program called test.c. How do I compile and see the output of C program in a Linux when using Fedora Linux?

Under Linux you need to use the cc/gcc (GNU project C and C++ compiler) command to compile a program written in C or C++. When you compile a program it generates an executable file called a.out.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Category Software Development
OS compatibility AIX AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora FreeBSD HP-UX Linux macOS Mint Mint NetBSD OpenBSD openSUSE Pop!_OS RHEL Rocky Slackware Stream SUSE Ubuntu Unix WSL
Est. reading time 2 minutes
Advertisement

Syntax

The syntax is:
$ gcc -o output-file program.c
OR
$ cc -o output-file program.c
OR
$ make program.c

Sample code

Here is a sample C code called test.c that I am going to compile using GNU C compiler:

/* Purpose: A simple program to get name and information about current kernel
 * using uname(2) on a Linux.
 * Author: Vivek Gite <https://www.cyberciti.biz>
 */
#include <sys/utsname.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/utsname.h>
 
int main(void){
	int i;
	struct utsname myname;
	i = uname(&myname); /* hold the structure */
	if ( i == 0 ){
		printf("Operating system name : %s\n",myname.sysname);
		printf("Node name : %s\n",myname.nodename);
		printf("Operating system release : %s\n",myname.release);
		printf("Operating system name : %s\n",myname.version);
		printf("Hardware identifier : %s\n",myname.machine);
	}
	else {
		 fprintf(stderr,"Oh no. uname(2) failed with %s\n", strerror(errno));
		 exit(1);
	}
	return 0;
}

Task: Compile C program

To compile type the command:
$ gcc test.c
OR
$ cc test.c

Task: Execute program to see output

Above command will create a file called a.out. To see output of test.c program type:
$ ./a.out

Task: Compile to specific executable file

You can specify an executable file name while compiling program itself:
$ gcc -o test test.c
OR
$ cc test.c -o test
OR
$ make test
Now execute the test program to see output of test.c on screen:
$ ./test
Sample outputs:

Fig.01: My test program in action

Fig.01: My test program in action

Creating Makefile

You can write Makefile for compiling your C code. This is useful for building large and complex code. Assume that your have one source code file named test.c and make a new file called Makefile using a text editor:
$ vim Makefile
# OR #
$ nano Makefile

Append the following

# Compiler to use (gcc)
CC = gcc
 
# Flags for the compiler
CFLAGS = -Wall -Wextra
 
# Source files one.c two.c and so on
SRCS = test.c
 
# Executable file name
TARGET = test
 
all: $(TARGET)
 
$(TARGET): $(SRCS)
	$(CC) $(CFLAGS) -o $@ $^
 
clean:
	rm -f $(TARGET)

Save this as Makefile in the same directory as your C code. You can now compile it as follows:
$ make all
Run it:
$ ls -l
$ ./test

If you want to clean up the compiled files, you can run the following command:
$ make clean

How to see output of C program in Linux or UNIX and write Makefile

Click to enlarge

This entry is 13 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

55 comments… add one
  • sanju Nov 6, 2012 @ 6:56

    hey there, can you tell me how to see the output of a c++ program in unix environment page by page ???

  • Dibya Nov 26, 2012 @ 7:27

    Thank Man…

  • nagababu Nov 28, 2012 @ 6:31

    thanks to this web site

  • George Maithya Jul 8, 2014 @ 14:12

    Please show me how to write a program in c that prints a file a screenfull at a time waiting for response from user.

  • hs Nov 7, 2015 @ 15:50

    thank u ….

  • Precious May 30, 2016 @ 15:47

    How about if i am making use of code block how do i get to see the output on the screen\

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.