C Quick Reference
%
%% %
%-n.m_ width n with m decimal points (floating point only)
the optional '-' is for left-justification
(.6 is default)
%n pointer to integer for # of chars written so far.
Strings
%c character
%s string
Decimal
%d integer
%u unsigned integer
%o unsigned octal
%x unsigned hexidecimal (abcdef)
%X unsigned hexidecimal (ABCDEF)
Floating Point
%f float
%e [-]d.ddde+dd (scientific notation)
%E [-]d.dddE+dd
%g/G uses %e/%E if exponent is +/-4. %f otherwise.
%lf (for scanf only, reading in double-precision numbers)
\
\b backspace \f formfeed
\n newline \r carriage return
\t horizontal tab \v vertical tab
\\ backslash \? question mark
\' single quote \" double quote
\ooo octal \xhh hex number
/* comment line(everything between ignored) */
#
#include <headerfile.h> {include a standard library header}
#include "headerfile.h" {include a personal header}
arithmetic
integer division truncates decimal
modulus equals the remainder of the integer division
assignment
= ex: a=0; a=b=c=0; x=5.0;
commandline
main(int argc,char *argv[],char *eargs[])
argc = number of command line arguments
argv[0] = first command line argument (program pathname)
argv[1] = second command line argument
eargs is a collection of pointers to characters strings containing
the session environment variables
extern char **environ; /* can also do the 'eargs', possibly. */
constants
#define var 123.4 double
#define var 123.4L long double
#define var 123.4F float
#define var 123 int
#define var 123UL unsigned long integer
#define chr 'a' character
enum constant_expression {item1,item2,etc...};
ex: enum boolean {NO,YES};
const double e = 2.71828;
const char msg[] = "warning: ";
EOF {is} -1
Data Types
integers
char character: (1 byte vax) {ie: 'a' equals 97}
one character in the local character set
int integer (4 bytes vax)
short short integer (2 bytes vax)
long long integer (4 bytes vax)
unsigned char ex: [0,255] (+for getting whole ascii table)
signed char ex: [-128,127]
floating points
float single precision floating point (4 bytes vax)
double double-precision floating point (8 bytes vax)
arrays
structures
unions
declarations
float eps=1.0e5; int i=5; char c;
char line[10]; char esc='\\'; int limit=MAX+1;
char str1[] = " abc";
float v[3]; //array of 3 floats v[0],v[1],v[2]
int a[2][5]; //two arrays of five ints
when passing multi-dimensional arrays, acept A[][size].
c needs to know the size of the second one.
char* vpc[32]; //array of 32 character pointers
deliminators
((c = getchar()) != EOF)
{puts characters into c until EOF(-1) found}
errors
segmentation fault
1) pointer variable not accounted for {basically}
2) unlocked file access
3) accessed non-accessible memory
files
FILE *ifp; {ifp is a file pointer}
ifp=fopen("file-spec","mode"); {opens a file for (read,write,or
append) and ifp now points to that file.}
fclose(ifp); {closes the file pointed to by ifp}
functions
use popen to execute a unix command and wait for completion.
return-type function-name(parameter declarations, if any);{top of file}
return-type function-name(parameter declarations, if any)
{declarations;statements;}
if
if (expr) {stmts;} else {stmts;}
if (expr) {stmts;} else if (expr) {stmts;} else {stmts;}
switch(expr) {case const-expr: {stmts;break;}
case const-expr: {stmts;break;}
default: {stmts;break}}
io
gets(char *s) /* read in s ending in \0, disregarding \n */
fgets(char *s,MAX,FILE *stream) /* keeps \n */
char buf[max]; int l; /* this set reads in an integer from
fgets(buf,max,stdin); * standard input, and puts it flawlessly
sscanf(buf,"%d",&l); * into l */
incrementing/decrementing
++ -- pre or post decrements/increments
+= etc. change value(others: -=,*=,%=,/=,
jumping
break breaks out of the current loop
continue jump to the next value, repeat loop
goto {goto and syn nomore, o ye of little bithacking}
logical operators
&& (and) || (or)
loops
for (i=_;until this =0{or untrue};change i) {execute block;}
while (this is true{or non-zero}) {execute block;}
do {stmts;} while (this is true{or non-zero});
odd header functions
<signal.h> sleep(unsigned int seconds) pauses for specified
seconds
passwords
to input a password (ie: not type on the screen), use getpass().
precedence
Operator Associativity
---------- -------------
() [] -> . left to right
! ~ ++ -- + - * & (type) sizeof right to left
* / % left to right
+ - ""
<< >> ""
< <= > >= ""
== != ""
& ""
(?) ""
| ""
&& ""
|| ""
?: ""
= += -= *= /= %= &= 94= |= <<= >>= ""
, (discards value of left expression) ""
printf
printf("text%dtext%ftext%c",integer,float,char);
variables
first 31 characters significant; no reserved words; starts with letter
or _, all others have to be either letter, digit, or _.
vt100
general format: (put inside quotes in a printf)
\033{stuff} {stuff}: (need \033 before each of these.)
(0_ - print graphics characters:
_: prints: _: prints:
~~ ~~~~~~~ ~~ ~~~~~~~
a horizontal bar f degree sign
g +/- sign j box low right
k box up right l box up left
m box low left n big cross-hairs
o-s line top->bottom t bar right
u bar left v perpendicular
w 1/perpendicular x bar
y less than/equal to z greater than/equal to
_ blank | not equal to
~ dot ` diamond
{ pi } script L
(B - turns graphics characters off
[#B - vertical tab(move down # lines)
[#E - line feed(# linefeeds)
[#F [#L - move whole screen down # lines, home cursor
[5;3H - screen position. row 5, column 3.{[0;0H == [H == home}
[J - clear screen from positioning down
[#M - moves whole screen up # lines
[#P - delete # characters under the cursor
[U - home
[U & [J - home, clear all.
[#X - overstrike 4 spaces starting from cursor