DIGIT-ARC read-only archive · snapshot: July 23, 2026

C in linux/unix

30 posts · originally from https://geek.digit.in/community/threads/c-in-linux-unix.71889/

A
adi007 Original post #1
Oct 29, 2007 at 12:37 PM
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
1.Lesser known facts in C:facts that you never knew about C
*www.thinkdigit.com/forum/showthread.php?t=71047

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
bibjee #2
Oct 29, 2007 at 12:59 PM
# include <stdio.h>
:)
A
adi007 #3
Oct 29, 2007 at 1:05 PM
^^corrected, i was just modifying the post and you viewed it while i was doing so..
Now the post is updated!:D
M
Oct 30, 2007 at 1:26 AM
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
Sykora #5
Oct 30, 2007 at 10:18 AM
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

Code:
$> 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
Garbage #6
Nov 1, 2007 at 6:55 PM
I had problem with conio.h in Linux. So I couldn't use getch()

Is there any solution for this ???
QwertyManiac
Nov 1, 2007 at 7:00 PM
Use getchar(); (Available in stdio.h itself)
Garbage
Garbage #8
Nov 1, 2007 at 7:04 PM
but what about OTHER FUNCTIONS available in conio.h ??
QwertyManiac
Nov 1, 2007 at 7:17 PM
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
Garbage #10
Nov 1, 2007 at 7:21 PM
okay....

~~ Thanks QM ~~
A
adi007 #11
Nov 5, 2007 at 10:49 AM
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
praka123 #12
Nov 5, 2007 at 1:55 PM
2147483647
^that much no program needs i suppose
A
adi007 #13
Nov 6, 2007 at 3:21 PM
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):D:D:D:D
praka123
praka123 #14
Nov 6, 2007 at 4:12 PM
thanks and informative,i am yet a newbie to C :)
M
mehulved #15
Nov 6, 2007 at 5:15 PM
adi007 said:

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):D:D:D:D
Is it? I thought depended on whether it was a 16bit or a 32bit compiler.
Faun
Faun #16
Nov 6, 2007 at 9:28 PM
must i say....informative
Sykora
Sykora #17
Nov 6, 2007 at 10:44 PM
@mehulved : It does depend on the compiler, not the operating system.
blackleopard92
Nov 7, 2007 at 12:37 AM
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
Sykora #19
Nov 7, 2007 at 9:29 AM
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
A
adi007 #20
Nov 7, 2007 at 12:55 PM
Saving the output in a text file

consider the following c program

continue here
A
adi007 #21
Nov 9, 2007 at 4:34 PM
^^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
A
adi007 #22
Nov 12, 2007 at 5:03 PM
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
Code:
#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

Code:
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
Code:
#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
Code:
void func1()
{
printf("First function\n");
}

and finally this as func2.c

Code:
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
Code:
bash-2.05b$ cc -o output.out func.c func1.c func2.c
In general the syntax is
Code:
cc -o <outputfilename>  <input files>


Also, if you want to give your executable file a different name than a.out, use
Code:
cc -o <output file> <source file>


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
Code:
#include<stdio.h>
int main()
{
void func1(void);
void func2(void);
printf("Main function start\n");
func1();
func2();
and
Code:
printf("Main function ends\n");
}

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.
3.makes the main program look small and hence debugging is easy.
praka123
praka123 #23
Nov 12, 2007 at 7:01 PM
isnt cc symlinked to gcc.cc is for original unix systems may be.
QwertyManiac
Nov 12, 2007 at 7:19 PM
praka123 - Yes it is, but why the doubt? :)
M
mehulved #25
Nov 12, 2007 at 9:04 PM
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 at 9:14 PM
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?
M
mehulved #27
Nov 13, 2007 at 4:58 AM
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 at 6:05 AM
Well, man cc gives a gcc document page, same as what man gcc gives. So its symlinked :)
praka123
praka123 #29
Nov 13, 2007 at 8:27 AM
^in BSD's they may not be using gcc.hence cc
Sykora
Sykora #30
Nov 13, 2007 at 8:29 AM
@qwerty : Not exactly...
Code:
$> 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
$>