Beruflich Dokumente
Kultur Dokumente
Intro to C
/∗ H e l l o , w o r l d ! Program ∗/
#i n c l u d e ” s t d i o . h”
i n t main ( )
{
p r i n t f ( ” H e l l o , w o r l d ! \ n” ) ;
}
> gcc h e l l o . c
> a . out
hello , world
>
/∗ p r i n t F a h r e n h e i t t o C e l s i u s t a b l e
f o r f a h r = 0 , 2 0 , . . . , 300 ∗/
main ( )
{
i n t fahr , c e l s i u s ;
i n t l o w e r , upper , s t e p ;
lower = 0; /∗ low l i m i t o f t a b l e ∗/
u p p e r = 3 0 0 ; /∗ h i g h l i m i t o f t a b l e ∗/
step = 20; /∗ s t e p s i z e ∗/
fahr = lower ;
w h i l e ( f a h r <= u p p e r ) {
c e l s i u s = 8 ∗ ( f a h r −32) / 9 ;
p r i n t f ( ”%d\ t%d\n” , f a h r , c e l s i u s ) ;
fahr = fahr + step ;
}
}
f e l i x : ˜ / c s 4 2 9 / c> g c c −O2 t e m p e r a t u r e . c
f e l i x : ˜ / c s 4 2 9 / c> a . o u t
0 −28
20 −10
40 7
60 24
80 42
100 60
120 78
140 96
160 113
180 131
200 149
220 167
240 184
260 202
280 220
300 238
f e l i x : ˜ / c s 4 2 9 / c> g c c −O2 −o t e m p C o n v e r t t e m p e r a t u r e . c
f e l i x : ˜ / c s 4 2 9 / c> t e m p C o n v e r t
0 −28
20 −10
40 7
60 24
80 42
100 60
120 78
140 96
160 113
180 131
200 149
220 167
240 184
260 202
280 220
300 238
#i n c l u d e < s t d i o . h>
#d e f i n e LOWER 0 /∗ low l i m i t o f t a b l e ∗/
#d e f i n e UPPER 300 /∗ h i g h l i m i t o f t a b l e ∗/
#d e f i n e STEP 20 /∗ s t e p s i z e ∗/
/∗ p r i n t F a h r e n h e i t t o C e l s i u s t a b l e
f o r f a h r = 0 , 2 0 , . . . , 300 ∗/
main ( )
{
int fahr ;
double c e l s i u s ;
#i n c l u d e ” s t d i o . h” // f o r t h e p r i n t f command
main ( i n t a r g c , c h a r ∗ a r g v [ ] , c h a r ∗ env [ ] )
{
p r i n t f ( ” S t a t u s : number o f command l i n e a r g s . \ n” ) ;
}
#i n c l u d e ” s t d i o . h”
main ( i n t a r g c , c h a r ∗ a r g v [ ] , c h a r ∗ env [ ] )
{
int i ;
i f ( a r g c == 1 )
p r i n t f ( ”The command l i n e argument i s : \ n” ) ;
else
p r i n t f ( ”The %d command l i n e a r g u m e n t s a r e : \ n” ,
argc ) ;
f o r ( i = 0 ; i < a r g c ; i++ )
p r i n t f ( ” Arg %3d : %s \n” , i , a r g v [ i ] ) ;
}
main ( i n t a r g c , c h a r ∗ a r g v [ ] , c h a r ∗ env [ ] )
{
int i ;
p r i n t f ( ”The e n v i r o n m e n t s t r i n g s a r e : \ n” ) ;
i = 0;
w h i l e ( env [ i ] != NULL )
{
p r i n t f ( ” Arg %3d : %s \n” , i , env [ i ] ) ;
i ++;
}
}
> g c c −o e n v a r g s e n v a r g s . c
> envargs
The e n v i r o n m e n t s t r i n g s a r e :
Arg 0 : PWD=/u/ byoung / c s 4 2 9 / c
Arg 1 : TERM=dumb
Arg 2 : TERMCAP=
Arg 3 : COLUMNS=80
Arg 4 : EMACS=t
Arg 5 : INSIDE EMACS = 2 3 . 3 . 1 , c o m i n t
Arg 6 : SHELL=/ l u s r / b i n / t c s h
Arg 7 : GROUP=p r o f
Arg 8 : GPG AGENT INFO=/tmp/ k e y r i n g −hZHfuV/ gpg : 0 : 1
# < l o t s more , 49 i n a l l >
. f i l e ” s u m .c ”
.text
. p 2 a l i g n 4 , ,15
. g l o b l sum
.type sum , @ f u n c t i o n
sum :
pushl %ebp
movl %esp , %ebp
movl 12(% ebp ) , %e a x
addl 8(% ebp ) , %e a x
popl %ebp
ret
v o i d main ( i n t a r g c , c h a r ∗ a r g v [ ] , c h a r ∗ env [ ] ) {
i n t i = 15213;
f l o a t f = 15213.0;
double d = 15213.0;
i n t ∗p = & i ;