PDA

View Full Version : How to make C program in ubuntu?



1ritesh
December 3rd, 2016, 12:56 PM
Hello,
How to make C program in ubuntu?

howefield
December 3rd, 2016, 12:57 PM
Thread moved to the "Programming Talk" forum.

1ritesh
December 3rd, 2016, 01:24 PM
ok, where to write c programe?

kpatz
December 3rd, 2016, 04:08 PM
Here's a tutorial: http://www.improgrammer.net/how-to-write-c-program-in-ubuntu/

But in a nutshell, you'll need build-essential, which will give you the C compilers and libraries:


sudo apt-get install build-essential

Then you'll use an editor to create your code, such as gedit, or in the terminal, vi or nano.


gedit myprog.c &

...or...

nano myprog.c

...or...

vi myprog.c

Save your code as a .c file (for our example let's call it myprog.c), and to compile it, in a terminal:


gcc -o myprog myprog.c

If it compiles without errors, run it in the terminal with:


./myprog

You can also create a program in C++ by giving the file a .cpp suffix instead of .c, and compiling it using g++ instead of gcc.

1ritesh
December 5th, 2016, 08:20 AM
what to do next for compiling and output?272551

mörgæs
December 5th, 2016, 08:43 AM
From the post above:


...to compile it, in a terminal:


gcc -o myprog myprog.c

1ritesh
December 5th, 2016, 10:54 AM
not working...

lisati
December 5th, 2016, 11:21 AM
not working...

From the display in the screenshot in your previous post, exit the editor with Ctrl+X, then use the gcc command.

1ritesh
December 8th, 2016, 10:40 AM
hello what to do after saving it is not compiling And running?
gcc -o myprog myprog.c

what does pid mean here??

please tell steps

karl96
December 8th, 2016, 10:46 AM
pid means process id.

By the looks of it you've got it the wrong way?


gcc myprog.c -o myprog

spjackson
December 8th, 2016, 12:07 PM
There is no functional difference between these two


gcc -o myprog myprog.c
gcc myprog.c -o myprog

The message in your 3rd screenshot is because you are opening the source file with nano when you already have it open in nano. This has got nothing to do with C or programming; if you edit the same file twice at the same time you may get a warning from your editor.

karl96
December 8th, 2016, 12:19 PM
Agreed, didn't notice that first time looking.

GCC should still find the source file though shouldn't it? As long as it is saved.

1ritesh
December 8th, 2016, 02:05 PM
nothing happen

kpatz
December 8th, 2016, 02:15 PM
When you run gcc -o myprog myprog.c it just compiles and links the program to create a binary called myprog.

To run it, enter this in the terminal:

./myprog

1ritesh
December 9th, 2016, 09:52 AM
hello,
the last code was working by run command
how to write addition program and save it?

karl96
December 9th, 2016, 11:03 AM
Do exactly the same thing, by the looks of it the issue is you haven't actually told your program to print the results to the screen. I would tell you how but i'll leave that to you as a learning exercise :)

1ritesh
December 17th, 2016, 08:51 AM
hello,
this is my new addition program how to run it and compile?272740

lisati
December 17th, 2016, 09:29 AM
hello,
this is my new addition program how to run it and compile?
The same way as described in post 4 of this thread.

1ritesh
December 20th, 2016, 10:06 AM
Hello when i write code for two sum of number it show error here it is272775

lisati
December 20th, 2016, 11:10 AM
You need to use the name of the saved file in place of "myprog.c" in the gcc command, and adapt the name of the output file accordingly.

1ritesh
December 20th, 2016, 01:03 PM
i have done not working showing error

kpatz
December 20th, 2016, 02:32 PM
You named it myprog1.c, not myprog.c. So to compile it use the command:
gcc -o myprog1 myprog1.c
and to run it, use
./myprog1