Quick Reference for C language
------------------------------------------------------------------------------
STRUCTURE OF A FILE:
global var declarations;
1 or more function declarations;
There must be a function named "main".
STRUCTURE OF A FUNCTION
returntype functionname ( list of parameters )
declaration of types of parameters;
{
local variable declarations;
statements;
}
Example function: find gcd (greatest common denominator) of 2 ints and
return it:
int gcd (a, b)
int a, b;
{
while (a != b) {
if (a < b)
b -= a;
else
a -= b;
}
}
DECLARATIONS AND BUILT-IN TYPES:
int a, b; a and b are integers
float x, y; x and y are single precision real numbers
double z; z is a double precision real
char ch; ch is a single character variable
char name[100]; name is a character array
int grades[50]; grades is an array of 50 integers
All arrays are 0 based. Thus, given "int grades[50];" here are the elements:
grades[0], grades[1], grades[2], ..., grades[49]
OPERATORS:
+, -, /, * usual meanings
% remainder of integer division
<< left shift (bits)
>> right shift (bits)
++, -- add 1, substract 1 (either post or prefix)
COMPARISON AND BOOLEAN OPERATORS:
==, != equality, inequality
<, >, <=, >= less than, greater than, etc.
! negates sense
&& logical AND (short-circuit)
|| logical OR (short-circuit)
NOTE: 0 is considered FALSE, and anything other than 0 is true, even negative
or floating point numbers. There is no "boolean" type in C.
COMBINATION OPERATORS
+=, -=, /=, etc Ex. a += 5; is a = a + 5;
STATEMENTS
assignment: = a = 5; c = sqrt(t + 4) - 12;
if: if (a < 10) j = 15;
if (a < 10) { (compound statement requires
j = 18; braces)
k = 19;
}
while: while (a < 5) {
c = 13 * c;
a--; /* subtract 1 from a */
}
COMMENTS:
/* anything except another C comment (cannot nest comments) */
INPUT/OUTPUT:
gets(line); Read in 1 line, line must be a character
string, declared as "char line[100];"
puts(line); Write out 1 line
printf ("Average of %d grades is %8.2f\n", numgrades, average);
Printf is general formatted print. First is a char
string with literal output and formatting codes.
Then the variables to be printed, one per code.
%d = print integer, %f = print float
%s = print string, %c = print one character
\n = new line \t = tab character
x = atoi(line); atoi() "ascii to integer", converts from char
string to integer
STRING MANIPULATION:
char name[100], town[50]; declare char string
strcpy (name, "Mark Meyer"); assign a string to variable
strcat (name, ", Jr."); append a string to pre-existing
string value
strcpy (town, name); copy one value into another var.
strlen (name) find out length, returns int
name[i] pick apart string by character
(usual array operations)
strcpy (name, ""); assign null string
STRUCTURES:
struct s1 { s1 is called "struct tag", kind of a type name
int x; x and y are fields.
float y;
} m,n; m, n are struct variables
struct s1 k; k is another variable of type s1
m.x = 15; field assignment
PARAMETERS:
All parameters are call by value in C. To simulate call by reference,
you must use pointers.
void dosomething (k)
int *k; k is call by ref, so it must be a pointer
{
*k = (*k + 47) / 2; modify k, changes in caller, too
} return to caller
void main()
{
int z = 15; declare int z, initialized to value 15
dosomething(&z); call dosomething, passing in pointer to z so
that it can be changed.
printf ("Z is now %d\n", z);
} you will see 31