How to run C program in linux?

MadhaniHarsh
  7 years ago
  13

Install C and C++ Compilers in Mint

sudo aptitude install build-essential
This will install all the required packages for C and C++ compilers
 

Compiling Your first C Programs

Now you need to open first.c file
sudo gedit first.c
add the following lines save and exit the file

#include
int main()
{
printf(“Hello world\n”);
return 0;
}

Firstly compile the code using the following command

cc -c first.c

that would produce an object file you may need to add to the library.

then create an executable using the following command

cc -o first first.c

Now run this executable using the following command
./first

Output should show as follows
Hello, world

Comments
dagon 7 years ago

The tags are removed when you press edit, but you can fix the tutorial and save it and it will work. If you then choose to edit it once more you must make sure to also re-add whatever the editor takes away... :(
I don't know of any other way.


MadhaniHarsh 7 years ago

@dagon:exactly....


dagon 7 years ago

Ok. The code probably went missing after editing the text.
A bug in CKEditor:
http://drupal.org/node/1258998


dagon 7 years ago

1. #include
should be:
#include <stdio.h> // parenthesis won't render (html/xml???)
This explains the "undeclaired" part of the errors...

2. printf(“Hello world\n”)
The quotation marks is not plain ascii so that would
explain the "stray `\342'... etc.
We need a "Code component" thing for tutorials!

3. I'm with @frankh:
gcc -o name name.c


MadhaniHarsh 7 years ago

@parashargunjan:have you used conio.h header file?


parashargunjan 7 years ago

first.c: In function ‘main’:
first.c:4: error: stray ‘\342’ in program
first.c:4: error: stray ‘\200’ in program
first.c:4: error: stray ‘\234’ in program
i m getting the below error plz help me out

first.c:4: error: ‘Hello’ undeclared (first use in this function)
first.c:4: error: (Each undeclared identifier is reported only once
first.c:4: error: for each function it appears in.)
first.c:4: error: expected ‘)’ before ‘world’
first.c:4: error: stray ‘\’ in program
first.c:4: error: stray ‘\342’ in program
first.c:4: error: stray ‘\200’ in program
first.c:4: error: stray ‘\235’ in program


Vishal 7 years ago

very nice..


efthialex 7 years ago

@MadhaniHarsh:

Nice job!


MadhaniHarsh 7 years ago

@trollboy:thankx,spread it if u like it


trollboy 7 years ago

Nice simple introduction


kazztan0325 7 years ago

@MadhaniHarsh:

Needless to say, I clicked on Promote Button when I added my first comments.

( And then I added you as my friend. I would be glad if you would also add me as your friend.)


MadhaniHarsh 7 years ago

@kazztan0325:thankx,i will keep this way of explaining.If u like it then please promote it.


frankh 7 years ago

Comment:

For the simplest compilation and use of C i Linux, it's not necessary to create an object file first. You can do gcc -o name name.c, and you'll immediately have an executable file.


kazztan0325 7 years ago

I think this tutorial is very useful and easy to follow for beginners who are interested in C and C++ to start learning them.