You are on page 1of 25

m 



 
   
 

 
    
  
   
 
  
  

 
    

    


www.cim.mcgill.ca/~jer/C/
m 


 
 

  

   
  !"
  # $

 
 % " &' "(!")   (


  *+  ,$ *
’

  
  
 -      
 )   
  .'/0
 ***

       


 
 1   
 -
  2  
 13   4   "  " 5
 ***
G 
! 
 (!" 3   3   
3     (6%&'7
/* A simple program that prints something */

#include <stdio.h>

main ()
{
printf ("Hello, world!\n");
}
^   

" 
float x;
double d = 5;
int *p, i, a[100];
char s[21];

 
type variable_name, ... [= value];




      
8      
 int   

longshortunsigned
  #  # 

" 
int x, y, z;
x = 2;
x = x + 1;

$  


y = z = 4 + 5;
x += 1;
++x;
x++;
y = x--;


          3" "    
 
9        
 %
" 
int i = 10;
float f = 2.5;
char s [] = "hi";
printf ("Jack\'s integer is %d\n", i);
printf ("Jill\0x27s float is %f\n", f);
printf ("My string s = %s\n", s);

 
printf (string_with_formatting, var1, var2, ...);
 :  %d  %f  %c " %s   ;
  ( 2 (\n 3 \' 2 \0x27 *
 #include <stdio.h>   9    
 &
" 
#include <stdio.h>
int i;
float f;
scanf ("%d %f\n", &i, &f);
/* inputs an integer and a float */

 
scanf (string_with_formatting, &var1, &var2,...);


 !" 
4&5   scanf m  "
 
  "   9   
printf   " 3 "  
  " *<
  " 
&'%" 
’ 

 
#include <stdio.h>

main ()
{
int n;
float x;
char mark;
scanf ("%d %f %c", &n, &x, &mark);
printf ("Of %d %s,\n%f got %c\¶s\n",
n, "students", x, mark);
}

 !  "3   a a5.999 A


&'%" 
#include <stdio.h>

main ()
{
int n;
float x;
char mark;
scanf ("%d %f %c", &n, &x, &mark);
printf ("Of %d %s,\n%f got %c\¶s\n",
n, "students", x, mark);
}
   
a a5.999 A
 =  
Of a students,
a5.999001 got A's
    

" 
if (age < 0)
{
printf ("warning: negative age\n");
age = -age;
}

 
if (?  ) 
if (?  )  else 



 " 
   int ‰4  5
  "2

 
  8
     >  
   
ù   

" 
if (x < 0)
printf ("x is less than 0\n");
else if (x == 0)
printf ("x is equal to 0\n");
else
printf ("x is greater than 0\n");

’ (
 

if (x < 0)
if (y < z)
printf ("y is less than z\n");
else
printf ("x not less than 0\n");
’ )

" 
/* print "hi" forever */
while (1)
printf ("hi");

 
while (?  )



  
 " 
   int ‰4  5
  "2

 
  8
     >  
   
)

" 
/* print "hi" three times */
int i; /* i continues to exist when loop ends */
for (i = 0; i < 3 ; i++)
printf ("hi");

 
for ( ; ?  ; 
)
 ;

"* 
 ;
while (?  ) {
 ;

;
}
)" 
/* print squares up to 100 */
main ( )
{
int j, up = 100;
for (j = 0; j * j <= up; j++)
printf ("%d \n", j * j);
}


  ? 
for (int j = 0; ...
 3         
   >    @
" (
/* print squares up to 100 */
void main ( )
{
int j, up = 100, sq;
for (j = 0; (sq = j * j) <= up; j++)
printf ("%d \n", sq);
}


  2   while  
    

 "

m 

int years[45];
float temperatures [11];
void main () {
years[0] = 2000;
temperatures[11] = -45. 7;
}



 
    A
 8   

8 "A "  
  ) 
 "
 


 temperatures[11] 
  

  

char a, b, c1, c2;


a = '0'; b = '\037'; c1 = 'K'; c2 = c1 + 1;
 /  4a, 31, 75, 7
 !"2 '0',...,'9', 'a',...,'z', 'A',...,'Z'
   "   
     


 
printf ("%c %d\n", c1, (int) c1);

 =  K 75
 

 
 '\0'+    
 char 
char s[3] = "hi"; /* invisible '\0' */
char t[3] = {'h', 'i', '\0'};

   

#include <string.h>
strlen ("there"); /* returns 5 */
strcpy (s, t); /* copy t to s */
strcmp (s, t) /* alphabetical comparison */
"
 
,

m

  
 $  
 

   2    
 =    "2      

"   "   " 2  "   "
" * "  3
*
 B8 %BB0CD,EE,FG
 

/* Increment; takes an integer argument and


* returns the argument plus one.
*/
int incr (int i)
{
int j;
j = i + 1;
return j;
}

main ()
{
int k, m = 4;
k = incr(m);
printf ("k = %d, m = %d\n", k, m);
}
   k = 5, m = 4
ù   

 " "     


 return     
void printhi ()
{
printf ("hi\n");
}
    

   


int incr (int i)


{
i++;
return i;
}

 4  
5   int
#  
    

-

.
void no_incr (int i)
{
i++;
}

void main () {
int x = 5;
no_incr(x);
printf ("%d\n", x);
}
 3 "  
        "
  *
  void main()  main
     
"

’     .
 


, 
,
 /      
 /  "      
 > *
 '    "3
  "    3 "
  
   3 "3  *