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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 55 comments so far... add one

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
  • ajay May 25, 2007 @ 4:38

    yaa
    you help me a lot
    thnx budy

    • firoz Jul 1, 2010 @ 5:38

      wt is the process to get output in c program

      • Shaik.moulali Jun 26, 2014 @ 9:26

        After written the program in c language
        To Compile that program in turboc click the ALT+F9
        To Run that program in turboc click the CTRL+F9

  • vivek Jun 4, 2007 @ 18:18

    hey man thanks for the instruction…… i can now compile and run c programs……

  • manisha Sep 15, 2007 @ 11:05

    this is realy very nice.thanks to help me.

  • umair Jan 22, 2008 @ 17:50

    i have installed MinGW now pls tell how to write a program and excuate it

  • pinku Jun 2, 2008 @ 10:19

    Hi,
    from your site i came to know how to compile and see out put of program, but how to write c program in linux????I m a window user,so if anyone give me that ans i will be obliged.

    • Harish Kadam Jan 20, 2017 @ 10:23

      Use just have to do write ur program in gedit and save it with c extension…..
      U have to go In file right click then document then click empty document….
      Now u can write ur program this is gedit then save it filename.c extension…..

      Have…fun…..

  • rakesh kumar thakur Aug 15, 2008 @ 5:11

    i search this site through google and find my answer on the topic … How can i run and compile program on gcc compiler?

  • tintin Oct 23, 2008 @ 0:18

    Nice post. It helps newbie a lot.
    :)

  • ashutosh Feb 1, 2009 @ 10:21

    The given commands is work properly…………
    it is very helpful

  • Stefaan Apr 21, 2009 @ 17:19

    Thanx man!!!

  • Thanks for this tip May 8, 2009 @ 10:01

    Thanks for this tip
    Plz also tell me how to enter the out put of one c program to another one plz send this at my email address.
    Thanks for this tip

  • Avinash T M May 21, 2009 @ 12:52

    Superb information , i liked this website very much.

  • bhargav Jul 19, 2009 @ 19:23

    i am executing c program in fedora as gcc temp.c but it says that
    bash: gcc: command not found
    please tell how to run

  • neeraj sinha Jul 28, 2009 @ 14:30

    @bhargav:
    Update your YUM repo (in Fedora/ RHEL), thereafter
    run the following command as root:
    yum install gcc
    or simply write
    sudo yum install gcc
    have fun!

  • Ivneet Aug 17, 2009 @ 6:35

    I am successfully executing a C prg in Turbo C but i am getting a runtime error in gcc. Please help

  • neeraj sinha Aug 18, 2009 @ 16:30

    @Ivneet:
    Please be specific about your program. Paste it anywhere and give us the link.

  • mahavir Sep 15, 2009 @ 1:04

    Awesome man!!!!!\
    this really helped………..

  • Mobashir Sep 26, 2009 @ 19:27

    i m a new user for linux platform,may any body plz let me know how to code,compile etc a graphics application for c++ under linux platform ,,,,,( explain with example plz)

  • vaibhav Oct 7, 2009 @ 13:50

    it is showing that gcc command not found . so what to do help me body.

  • neeraj sinha Oct 7, 2009 @ 20:19

    @vaibhav!
    probably gcc is not installed. Install it first depending on your linux.
    and try the commands. !
    http://susenj.wordpress.com

  • yash Oct 8, 2009 @ 3:14

    i hav c++ program in linux how i ll run it in windows c++ because it gives 25 errors

  • Shivam Oct 21, 2009 @ 14:59

    thx buddy it really helps me….

    their is some IDE for C++ environment…

  • neeraj sinha Oct 22, 2009 @ 3:02

    @shivam:
    yes there is: like KDevelop, Anjuta, Kate etc.

  • soumendu ganguly Dec 6, 2009 @ 3:19

    thanx…….
    u help me lot……….

  • sagnik mukherjee Feb 11, 2010 @ 10:20

    THANK U….:)

  • Hk Apr 27, 2010 @ 9:50

    I use Fedora 12 (live Cd).
    i created a c program in it and tried to execute.
    it says that no such command..
    how to eliminate this problem.
    Thanks

  • kiran May 27, 2010 @ 12:37

    Thanks…

    • firoz Jul 1, 2010 @ 5:40

      how to get o/p in c program pls explain me na

  • www Sep 25, 2010 @ 19:45

    the output command does not do on fedora 13
    #./test

  • hemant Mar 3, 2011 @ 7:20

    i hav c program in linux how i ll run it in windows c because it gives 25 errors

  • mohammed fereja Apr 12, 2011 @ 9:50

    thanks..alot it just helped me out

  • sreedhar.M Apr 19, 2011 @ 10:26

    I am sreedhar,i am doing MCA.I have interest to learn programming languages in linux operating system.so,what should i do.

  • murali Jun 29, 2011 @ 3:52

    thank u

  • David Jul 23, 2011 @ 16:26

    ha, of course. Thanks for posting this. Saved me an embarrassing question on some IRC channel or a forum.

  • Hubert Vijay Sep 13, 2011 @ 22:29

    Good work dude….proves to be very useful for the begginers

  • akshata kittur Oct 2, 2011 @ 16:01

    I have typed a hello world program in fedora.. But how should i get back to the terminal.what are the keys on keyboard that i hav t press and want commands to type so that my file is saved.

    • Harish Kadam Jan 20, 2017 @ 10:26

      Create ur file in edit n save the file with .c extension…..
      Then compile……then run….through commands

  • vandana Dec 13, 2011 @ 5:29

    thnx…..for providin this nice solution…

  • vaishali Mar 30, 2012 @ 4:17

    it really helped me thanks

  • ankit senger Aug 6, 2012 @ 9:34

    thanks it is very helpful for me

  • kishore vignesh Aug 10, 2012 @ 7:57

    thank you :) !

  • lata Sep 11, 2012 @ 4:51

    Is gcc compiler necessary for C++ programming under Linux????
    Can we do this without any compiler????

    • neeraj Sep 11, 2012 @ 10:00

      Any language needs a compiler/interpreter.
      I can even run Turbo C++ on Linux.
      so, answer to your first question = no, but yes, it’s the most preferred compiler for any *nix Operating system.
      for second question = No.

      • lata Sep 11, 2012 @ 11:24

        Neeraj,
        thank you so much for ur help and consideration

  • sra1 Sep 28, 2012 @ 6:55

    thanq nd what abt html programmes

    • neeraj Sep 28, 2012 @ 13:05

      Sorry, There is no programming language called HTML.
      What exactly you want to do with HTML under gcc?

  • 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.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz