Use of C program in Linux

Asked By 210 points N/A Posted on -
qa-featured

Hi,
I am Rizwan and i am a student . I want to ask a question about the C programming in Linux.
How can i write and compile a C program in Linux?
I am searching the best answer for this question.
Please if any one have any idea, then share you solution over here.
Thanks.

SHARE
Best Answer by HenryMcCoy
Answered By 0 points N/A #125641

Use of C program in Linux

qa-featured

Hi. 

Farhan here in my point of view the best solution of your question is to write the code in text file and save it with .c extension. Then open the terminal and go to that location 

where you have saved that file(with the help of cd command). After that you can write gcc fileName.c and then press enter. If it runs successfully, it will not show any error otherwise correct the highlighted error. Then you have to type ./a.out, this will run the program. I will suggest you to first compile and run a small program. (just print "Hello World" through printf)

Thanks

Best Answer
Best Answer
Answered By 0 points N/A #125642

Use of C program in Linux

qa-featured

Hello Rizwan.

Here's how you write, compile and execute a C program in Linux.

  1. Write your C program using a text editor.  You can use vi, vim, or gedit as text editors.  Save the file with a *.c extension.  Actually, you don't need the file extension in Linux but since it is already standard practice for C programmers then please do so.
  2. Open a terminal and go to the directory where your C source file is.  Type in ls to see the file that you made.  Let's assume you names your file "myprogram.c", type this in at the prompt:
  • gcc -o myprogram myprogram.c
  • The first "myprogram" will be the name of the executable that will be created.  You may specify a different name if you wish.
  1. Type "ls" again to see the files in your directory.  There should now be a file named "myprogram.out".
  2. To make it executable, type this at the prompt:  
  3. chmod 666 myprogram.out
  4. Execute your program by typing it this way:
  • ./myprogram.out

Hope this helps!

Related Questions