/* comments */
<---Program
comments
//comment
<---Single line comment
#include <stdio.h> <-------------Including
header file
#include "myheader.h" <-------------Including user created header file (in src dir)
#define MONTHS 12 <-------------Constant
int num1 =
3; <-------------A
global variable being declared
void
test(); <----letting
the compiler know about the function 'test'
int main() <--------------The
function 'main'
(each program must have this)
{
<--------Enclosing
braces
int num2,
num3; <---Local variables
declared to the 'main' function only
test();
<----calling the function 'test'
}
<------------Enclosing
braces
void
test() <--the function
'test' which returns no values
{
}
files:
c_template.c - basic c program skeleton
Arithmetic Operators
+ addition
- subtraction
* multiplication
/ division
% modulus
++ increment
-- decrement
++/-- may be postfix or prefix
prefix = action is applied before everything else in the line
postfix = action is applied after everything else in the line
Logical Operators
&& and
|| or
! not
Relational Operators
== equal to
> greater than
< less than
!= not equal to
>= greater than or equal to
<= less than or equal to
(true returns a 1, false returns a 0)
Compound Operators
*=
/=
%=
+=
-=
example: myvariable += 2 is the same as myvariable = myvariable + 2
Conversion Characters
%d integer
%c character
%s string
%f float (%.2f will print only 2 digits behind the decimal point, %.0f prints 0 digits, etc)
Conditional/Ternary Operator
condition ? true code : false code;
sum > 20 ? myvariable = 1 : myvariable = 0;
Pointer Operators
& address-of
* dereferencing
Escape Sequences
\n newline
\\ backslash
\" double quote
\t horizontal tab
\a alarm
Typecasting
Temporarily convert the datatype of a variable to another (usually in an equation)
example: float(myvariable) or (float)(myvariable - 4)
If/Else
if (myvariable < 2)
{
code..
}
else
{
code..
}
more examples:
if ((myvariable1 > 2) && (myvariable1 < 50))
if (!(myvalue))
Switch
switch (selection)
{
case (1) : code .. ;
break;
case (2) : code ..;
break;
default : code ..;
break;
}
Loops
while
i = 1;
while (i <= 3)
{
code...
i = i++;
}
do-while (executes at least once)
do
{
code...
}
while (i <= 3)
for
int i;
for (i = 1; i <= 10; i++)
{
code..
}
loops can be stopped prematurely by a break;
to force a new loop cycle use continue;
Pointers
int * pMyint;
int myint = 99;
pMyint = &myint; < point to address of myint
*pMyint = 99; < also sets value of myint to 99
*myarray < points to the first value of array
*(myarray + 1) < points to second value of array
char * pName = "Eric Bouse"
to alter...
pName = "Another Name"
Functions
ceil() - rounds up a float to an integer
header: math.h
cos() - returns the cosine of the angle
header file: math.h
exit() - exits program
header: stdlib.h
examples:
exit(0);
exp()
header file: math.h
fabs() - returns float absolute value
header: math.h
floor() - rounds down a float to an integer
header: math.h
getch - get a single character from keyboard (unbuffered)
header: stdio.h
getchar() - gets single character from keyboard (buffered)
header: stdio.h
examples:
mystring[2] = getchar();
getchar(); can be used to clear the buffer
gets() - gets a stringle value from the keyboard. accepts spaces.
header: stdio.h
examples:
gets(myvariable);
isalpha() - returns true/false (1/0) for alphabetic character test (a-z, A-Z)
header file: ctype.h
examples:
isalpha(mychar);
isdigit() - returns true/false for numeric character test (0-9)
header file: ctype.h
islower() - returns true/false for lowercase character test (a-z)
header file: ctype.h
isupper() - returns true/false for uppercase character test (A-Z)
header file: ctype.h
log()
header file: math.h
log10()
header file: math.h
pow() - raises value to specified power
header file: math.h
examples:
pow(3, 4); < raises 3 to the 4th power
printf() - output to stdout (screen)
header file: stdio.h
examples:
printf("hi\n");
printf("display a string: %s", string_variable);
printf("some numbers: %d %d", 3, 7);
putch() - displays single character on the screen
header: stdio.h
putchar() - displays single character on the screen
header: stdio.h
examples:
putchar(mystring[3]);
puts() - displays a string value on the screen. automatically adds a newline
header: stdio.h
examples:
puts("hi");
rand() - generate a random number from 0-32767. will produce the same sets of random numbers. does not accept arguments
header file: stdlib.h
examples:
myvalue = (rand() % 99) +1; < random number from 1-100
scanf() - get input from stdin (keyboard)
header: stdio.h
examples:
scanf(" %d", &amount);
scanf(" %s", name);
sin() - returns the sine of the angle
header file: math.h
srand() - allows rand() to generate a better random number
header file: stdlib.h, time.h
examples:
time_t t;
srand(time(&t));
myvalue = (rand() % 20);
strcat() - merge two strings
header file: string.h
examples:
strcat(fname, lname); < combines fname + lname to fname
strcpy() - change contents of a character array (string). this is how you assign strings.
header file: string.h
examples:
strcpy(name, "Eric");
strlen()
header file: string.h
sizeof() - determines amount of bytes required to store specified data type
examples:
sizeof(int);
sqrt() - returns square root of value
header file: math.h
tan() - returns the tangent of the angle
header file: math.h
tolower() - converts character to lowercase
header file: ctype.h
toupper() - converts character to uppercase
header file: ctype.h