C Quick Reference

(Last Mod: 27 November 2010 21:38:36 )

ECE-1021 Home



Precedence of C Operators

LEVEL Description Operator Associativity
1 Function Expression ( ) left
Array Expression [ ]
struct indirection ->
struct member .
2 Increment / Decrement ++ -- right
Bitwise invert (1's complement) ~
Logical NOT (unary NOT) !
Address &
Dereference *
Cast ( type )
Unary plus (positive sign) +
Unary minus (negative sign) -
Size in bytes sizeof
3 Multiplication * left
Division /
Modulus %
4 Addition +
Subtraction -
5 Shift Left <<
Shift Right >>
6 Less than <
Less than or equal <=
Greater than >
Greater than or equal >=
7 Equal ==
Not equal !=
8 Bitwise AND &
9 Bitwise XOR ^
10 Bitwise OR |
11 Logical AND &&
12 Logical OR ||
13 Conditional ? : right
14 Assignment

=

*=  /=  %=

+=  -=

>>=  <<=

&=  |=  ^=

15 Comma , left

Format codes for printf() function family

Format Specifier: %[flags][width][.precision][modifier][type]

Required type and optional modifiers

Modifier Type Format Corresponding argument must be
INTEGER TYPES
  c character char, short, or int (automatically promoted to int)
  s string address of char (first character of null-terminated string)
  d, i decimal char, short, or int

(automatically promoted by to int)

o octal
x hex (a..f)
X hex (A..F)
l d, i decimal long
o octal
x hex (a..f)
X hex (A..F)
  u decimal

unsigned char, unsigned short, unsigned int

 (automatically promoted to unsigned int)

l u decimal unsigned long
FLOATING POINT TYPES
  f m.nnnnnn float or double (automatically promoted to double)

g and G use which ever format is shortest

e m.nnnnnne?xx
E m.nnnnnnE?xx
g d, e, or f
G d, E, or f
L f m.nnnnnn long double

g and G use which ever format is shortest

e m.nnnnnne?xx
E m.nnnnnnE?xx
g d, Le, or Lf
G d, LE, or Lf
POINTER TYPES
  p address pointer (i.e., an address)
REPORT BACK
  n

characters

written

so far

address of variable of type int
h n address of variable of type short
l n address of variable of type long
SPECIAL CHARACTER
  % % no argument required/allowed

Optional Flags

Flag Meaning
- Left justify the result
+ Begin number with appropriate sign (+ or -)
space Add space in place of an unused sign
# type {o} : begin number with 0 (zero) to indicate octal
type {x} : begin number with 0x to indicate hex
type {X} : begin number with 0X to indicate hex
type {e, E, f}: use a decimal point
type {g, G}: use a decimal point and do not remove trailing zeros.
0 (zero) Pad the number to the left with zeros

Format codes for scanf() function family

Conversion Code format:  %[flags][width][modifier][type]

Required type and optional modifiers

Modifier Type Interpretation Corresponding argument must be
INTEGER TYPES
  c character address of char
  s string address of char
h d decimal integer address of short
  address of int
l address of long
h o octal integer address of short
  address of int
l address of long
h x or X hexadecimal integer address of short
  address of int
l address of long
h i

nnn - decimal integer

0nnn - octal integer

0xnnn - hex integer

address of short
  address of int
l address of long
h u unsigned decimal integer address of short
  address of int
l address of long
FLOATING POINT TYPES
 

f

e, E

g, G

floating point value -

decimal or exponential format acceptable

address of float
l address of double
L address of long double
POINTER TYPES
  p address address of a pointer
REPORT BACK
  n

characters

read

so far

address of variable of type int
h address of variable of type short
l address of variable of type long
SPECIAL CHARACTER
  % match the % in the input no argument required/allowed
MATCHING/EXCLUSION
  [chars]

string that only has chars

address of char
[^chars] string that excludes chars address of char

Optional Flags

Flag Meaning
* Suppress storage of scanned result (hence no corresponding address in list)

Width

Characters not in a conversion code:


Abbreviated reference of commonly used functions

prototype Comments
<stdio.h>
int printf(const char *fs [, arg, ...] ); fs = format string
int fprintf(FILE *fp,const char *fs [, arg, ...] );
int sprintf(char *dest, const char *fs [, arg, ...] );
int scanf(const char *fs [, arg, ...] ); fs = format string
int fscanf(FILE *fp,const char *fs [, arg, ...] );
int sscanf(const char *src, const char *fs [, arg, ...] );
FILE *fopen(const char *fn, const char *mode); fn = filename, mode ex: "wt" "rb"
int fclose(FILE *fp); returns 0 on success, EOF otherwise
<stdlib.h>
int rand(void); random integer [0 ,,, RAND_MAX]
void srand(unsigned int); seed the random number generator
double sin(double radians); argument in radians
double cos(double radians);
double tan(double radians);
double arcsin(double x); return value in radians
double arccos(double x);
double arctan(double x);
double atan2(double y, double x);
double exp(double x); returns ex
double log(double x); returns natural logarithm of x
double log10(double radians); returns base ten logarithm of x
double fabs(double x); absolute value of a floating point value
<math.h>
double sin(double radians); argument in radians
double cos(double radians);
double tan(double radians);
double arcsin(double x); returns -(PI/2) < radians < +(PI/2)
double arccos(double x);
double arctan(double x);
double atan2(double y, double x); returns  -PI < radians < +PI
double exp(double x); returns ex
double log(double x); returns natural logarithm of x
double log10(double radians); returns base ten logarithm of x
double pow(double x, double y); returns x to the power of y
double fabs(double x); absolute value of a floating point value
double ceil(double x); smallest integer not less than x
double floor(double x); largest integer not larger than x

In-Class Developed Output Functions (using only putc())

To print a character to the screen:

#define PutC(c)  (putc((char) (c), stdout))

To print a string to the screen:

int PutS(const char *s)

{

   int i;

   for (i = 0; '\0' != s[i]; i++ )

        PutC(s[i]);

   return i;

}

To print a single decimal digit to the screen:

#define PutD(d) (PutC('0' + (d)))

To print an unsigned integer to the screen:

int PutV_u(unsigned int n)

{

   unsigned int m;

   int printed;

 

   printed = 0;

   for (m = 1; n/m >= 10; m *= 10 )

        /* EMPTY LOOP */ ;

   do

   {

        for (d = 0; n >= m; d++)

            n -= m;

        PutD(d);

        printed++;

        m /= 10;

   } while (m > 0);

  

   return printed;

}

To print a signed integer to the screen:

int PutV_i(int n)

{

   if (n < 0)

   {

        PutC('-');

        return 1 + PutV_u(-n);

   }

 

   return PutV_u(n);

}

To print a floating-point value to the screen (to six decimal digits):

int PutV_lf(double x)

{

    double weight;

    int printed;

 

    printed = 0;

    if (x < 0)

    {

        PutC('-');

        printed++;

    }

 

    for (weight = 1; x/weight >= 10.0; weight *= 10 )

        /* EMPTY LOOP */ ;

 

    do

    {

        for (d = 0; x >= weight; d++)

            x -= weight;

        PutD(d);

        printed++;

        weight /= 10.0;

    } while (weight > 0.5);

 

    PutC('.');

    printed++;

   

        do

    {

        for (d = 0; x >= weight; d++)

            x -= weight;

        PutD(d);

        printed++;

        weight /= 10.0;

    } while (weight > 0.5e-6);

   

    return printed;

}