C in linux/unix

Status
Not open for further replies.

adi007

Youngling
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...
 
Last edited:
OP
A

adi007

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

mehulved

18 Till I Die............
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

I see right through you.
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

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

Is there any solution for this ???
 

QwertyManiac

Commander in Chief
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.
 
OP
A

adi007

Youngling
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
 
Last edited:
OP
A

adi007

Youngling
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
 
Last edited:

mehulved

18 Till I Die............
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.
 

blackleopard92

In the zone
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

I see right through you.
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
 
OP
A

adi007

Youngling
Saving the output in a text file

consider the following c program

continue here
 
Last edited:
Status
Not open for further replies.
Top Bottom