How to see output of C program in Linux or UNIX

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.

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 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 execuable 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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
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>, <code>...</code> and <kbd>...</kbd> for code samples.