# C in linux/unix



## adi007 (Oct 29, 2007)

Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka

                                 This thread is all about running c programs in linux/unix



> _
> Don't forget to visit these other threads started by me
> 1.Solve the given C puzzle:mindcracking C puzzles.Updated atleast once in a week
> *www.thinkdigit.com/forum/showthread.php?t=70697
> ...



Read it in my blog

I hope this is enough for today.
I am awaiting for your responses.

*
Be sure to check this thread often because the following areas will be dealt by me shortly
1.Precautions while running c in linix/unix
2.Advantages of running c in linux/unix
3.Drawbacks
and many more...
*


----------



## bibjee (Oct 29, 2007)

# include <stdio.h>


----------



## adi007 (Oct 29, 2007)

^^corrected, i was just modifying the post and you viewed it while i was doing so..
Now the post is updated!


----------



## mehulved (Oct 30, 2007)

I have put this up elsewhere in the forums too, but let me put it up again
*people.ubuntu-in.org/~ghoseb/tc-to-gcc.pdf
It will be useful for people starting with gcc.


----------



## Sykora (Oct 30, 2007)

If you're going to use main() (which is basically int main() ), instead of void main(), then it is recommended to give a return 0; statement at the end.

Also, if you want to give your executable file a different name than a.out, use


```
$> gcc -o <name of output file> <name of source file>
```

Lastly, most linux distros alias vi to vim by default. If you're still using vi, don't. Use vim instead.


----------



## Garbage (Nov 1, 2007)

I had problem with conio.h in Linux. So I couldn't use getch()

Is there any solution for this ???


----------



## QwertyManiac (Nov 1, 2007)

Use *getchar(); *(Available in *stdio.h* itself)


----------



## Garbage (Nov 1, 2007)

but what about OTHER FUNCTIONS available in conio.h ??


----------



## QwertyManiac (Nov 1, 2007)

curses / ncurses replace conio (It is only for DOS) on UNIX/LINUX, not exactly but they are too TUI driving headers like the latter, *man* them to know more.


----------



## Garbage (Nov 1, 2007)

okay....

~~ Thanks QM ~~


----------



## adi007 (Nov 5, 2007)

*Advantages of running c in linux/unix*
C is strongly linked with linux/unix .The kernel of Unix/Linux is written in C.Running C in linux/unit has several advantages .The most important among it is *full word length* utilization.
Continue in my blog


----------



## praka123 (Nov 5, 2007)

2147483647
^that much no program needs i suppose


----------



## adi007 (Nov 6, 2007)

*Important instructions for programs using math.h library function*

if your program uses math.h header file then sometimes the compilation will lead to errors.In such cases you have to suffix *-lm* to the compilation file command 

Read it in my blog 


			
				praka123 said:
			
		

> 2147483647
> ^that much no program needs i suppose


^^
_
praka123 here is the program which uses long values


		Code:
	

#include<stdio.h>
#include<math.h>
main()
{
    long double pi=3.14159265,a,r;
    int pre;
    printf("Enter precision of pi between 0 and 8\n");
    scanf("%d",&pre);
    printf("Enter radius\n");
    scanf("%Lf",&r);
    pi=(int)(pi*pow(10, pre));
    pi=pi/pow(10, pre);
    a = pi * r * r;
    printf("The Value of pi taken is %.*Lf and the area of the circle is %Lf\n", pre, pi, a); 
}

^^ the above program will not work in TC because when pre is some 7 or 8 then
the statement


		Code:
	

pi=(int)(pi*pow(10, pre));

will results in pi=(int)(3.14159265*10^7)=(int)(31415926.7) which cannot be converted into
31415926 because the maximum value a int datatype variable that can store is 32767 in windows.Hence pi is assigned to some garbage value.

Anyways, you have change (int) to (long) if you want to run it in TC.But, imagine if you want the accuarcy to some 12 or 13 then you must rely upon linux because the max value of long data type in linux is some 9.22337e+18(32 bit) where as in case of windows it is 2147483647(Equal to what int is in linux)
_


----------



## praka123 (Nov 6, 2007)

thanks and informative,i am yet a newbie to C


----------



## mehulved (Nov 6, 2007)

adi007 said:
			
		

> _
> 
> 
> Code:
> ...


Is it? I thought depended on whether it was a 16bit or a 32bit compiler.


----------



## Faun (Nov 6, 2007)

must i say....informative


----------



## Sykora (Nov 6, 2007)

@mehulved : It does depend on the compiler, not the operating system.


----------



## blackleopard92 (Nov 7, 2007)

mehulved said:
			
		

> Is it? I thought depended on whether it was a 16bit or a 32bit compiler.


of course, it depends on the compiler.. tc is 16 bit.. visual studio/gcc are system native ones( 32/64 depending on the system)

and generally.. when a value greater than the size of int is stored in a variable.. then it's higher bits are cropped.. not same as garbage values.


----------



## Sykora (Nov 7, 2007)

When a value greater than the size is stored in a variable, the value is wrapped around from the lower end. ie if the maximum value is 32767, and you try to store 32768, it'll wrap-around to -32768


----------



## adi007 (Nov 7, 2007)

Saving the output in a text file

consider the following c program

continue here


----------



## adi007 (Nov 9, 2007)

^^hmmm no responses



			
				Sykora said:
			
		

> When a value greater than the size is stored in a variable, the value is wrapped around from the lower end. ie if the maximum value is 32767, and you try to store 32768, it'll wrap-around to -32768



How C compiler decides Garbage value in case of dataoverflow
FInd this out in my lesser known facts in c thread.
*www.thinkdigit.com/forum/showthread.php?p=655539#post655539


----------



## adi007 (Nov 12, 2007)

No contributions and responses  .Looks like no one has liked this thread.

Compilation or linking of mutiple files to a single file

Another advantage of running c in linux is compliation or linking of multiple c files into a single file.Here multiple files means not multiple programs but the files which contains the part or functions of the main program.

consider the following  C program

```
#include<stdio.h>
int main()
{
void func1(void);
void func2(void);
printf("Main function start\n");
func1();
func2();
printf("Main function end\n");
}

void func1()
{
printf("First function\n");
}

void func2()
{
printf("Second function\n");
}
```

When you run this program, the output will be


```
Main function start
First function
Second function
Main function end
```

With linux, there is no restriction that the functions func1 and func2 must be written in the same file itself.
i.e,.
save this as func.c

```
#include<stdio.h>
int main()
{
void func1(void);
void func2(void);
printf("Main function start\n");
func1();
func2();
printf("Main function end\n");
}
```

this as func1.c

```
void func1()
{
printf("First function\n");
}
```

and finally this as func2.c


```
void func2()
{
printf("Second function\n");
}
```

Now as u can see the above program is split into 3 parts.one the main program and the remaining 2 functions.
now in order to compile the above 3 programs as a single file you have to issue command

```
bash-2.05b$ cc -o output.out func.c func1.c func2.c
```
In general the syntax is

```
cc -o <outputfilename>  <input files>
```



> _
> Also, if you want to give your executable file a different name than a.out, use
> 
> 
> ...




The 3 files will be compiled as one.Now to run the program type *./output.out*.The same output you will get as given above



> _
> You can divide the main program only at the points of the end of the function.That means u cannot split the program as
> 
> 
> ...





> Some of the uses of linking are:
> _
> 1.Improves redability of the main program
> 2.file containing the function can be used by other programs.That means once you create a separate file for a function,u can reuse it if other program requires the function.all you have do it is specify the function declaration in the main program and link this function during compilation.
> ...


----------



## praka123 (Nov 12, 2007)

isnt cc symlinked to gcc.cc is for original unix systems may be.


----------



## QwertyManiac (Nov 12, 2007)

praka123 - Yes it is, but why the doubt?


----------



## mehulved (Nov 12, 2007)

praka123 said:
			
		

> isnt cc symlinked to gcc.cc is for original unix systems may be.


 I would avoid such assumptions after the dash fiasco


----------



## QwertyManiac (Nov 12, 2007)

How would dash be related to a symlink? I don't understand, do explain if you get the time. 

Also, why does *cat /usr/bin/cc* makes one's terminal go fscky?


----------



## mehulved (Nov 13, 2007)

QwertyManiac said:
			
		

> How would dash be related to a symlink? I don't understand, do explain if you get the time.


dash won't but what if cc was linked to another compiler and not gcc. You can't guarantee your program to build then. It may just work fine or maybe not at all.


			
				QwertyManiac said:
			
		

> Also, why does *cat /usr/bin/cc* makes one's terminal go fscky?


 using cat on any binary would do that.


----------



## QwertyManiac (Nov 13, 2007)

Well, *man cc* gives a gcc document page, same as what *man gcc* gives. So its symlinked


----------



## praka123 (Nov 13, 2007)

^in BSD's they may not be using gcc.hence cc


----------



## Sykora (Nov 13, 2007)

@qwerty : Not exactly...

```
$> which cc
/usr/lib/ccache/cc
$> ls /usr/lib/ccache/cc
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 /usr/lib/ccache/cc -> /usr/bin/ccache
$> ls /usr/lib/ccache
total 0
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 c++ -> /usr/bin/ccache
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 cc -> /usr/bin/ccache
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 g++ -> /usr/bin/ccache
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 gcc -> /usr/bin/ccache
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 i386-redhat-linux-c++ -> /usr/bin/ccache
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 i386-redhat-linux-g++ -> /usr/bin/ccache
lrwxrwxrwx 1 root root 15 2007-10-31 21:12 i386-redhat-linux-gcc -> /usr/bin/ccache
$>
```


----------

