©
Wageless4action
Team
©
Catmaster
Syntax,
Declaration
&
Implementation
in
C
Good
Rules
For
Great
Legibility
Catmaster
ABSTRACT
This
document
enumerates
the
principal
C
commands,
like
variables,
loops
and
functions,
and
proposes
good
rules
for
declaration
and
implementation
in
this
language,
being
the
main
point
in
using
code
conventions
to
improve
the
legibility
of
the
code.
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
Table
of
Contents
1.
INTRODUCTION
3
2.
COMMENTS
4
2.1.
ONE-LINE
COMMENTS
4
2.2.
BLOCK
COMMENTS
4
3.
VARIABLES
4
4.
DECISION
STRUCTURES
5
5.
LOOPS
5
6.
FUNCTIONS
6
7.
POINTERS
7
8.
STRUCTS
8
9.
TYPEDEFS
8
APPENDIX
A
–
USEFUL
FUNCTIONS
10
DISPLAY
ERROR
MESSAGE
(EC_MESSAGE)
10
CHECKS
INSUFFICIENT
MEMORY
FOR
MALLOC()
(SAFE_MALLOC)
10
RANDOMIZES
NUMBERS
FOR
AN
ARRAY
(GENERATE_ARRAY)
10
APPENDIX
B
–
ABOUT
WAGELESS4CTION
TEAM
11
Syntax,
Declaration
&
Implementation
in
C
2
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
1. Introduction
Imagine
yourself
reading
a
source
code
where
you
have
about
three
statements
in
only
one
line,
one
after
another
without
proper
spacing,
and
variable
names
like
“a“,
“b”
and
“ab”.
Want
to
have
an
example?
Here
it
goes:
#include<stdio.h>
main(){char* a=“hello, world” ;
printf(“%s”, a);return 0;}
It’s
a
little
bit
hard
to
read
this,
isn’t
it?
Besides
being
complicated
and
all
messed
up,
for
those
ones
who
don’t
know
any
programming
principles,
this
will
seem
alien
talk.
That’s
why
I
decided
to
write
the
“Good
rules
for
great
legibility“
series.
Please
pay
attention
that
these
series
of
documents
ARE
NOT
official,
so
the
programming
language
creators
don’t
certify
them
and
neither
programming
companies
do.
In
this
document,
you
will
find
command
syntaxes,
declaration
and
implementation
rules,
and
even
some
explanations.
At
the
end
of
the
document,
there
will
be
information
about
the
Wageless4ction
Team’s
site,
and
information
regarding
to
invitations
to
join
our
good
hacking
cause
;)
Concluding,
this
document
expects
you
to
know
some
C
programming.
If
you
are
not
familiar
with
this
language,
there
are
very
good
books
out
there,
like
Deitel
&
Deitel’s
C
How
To
Program,
Kernighan
&
Ritchie’s
C
Programming
Language,
and
C
In
a
Nutshell.
Syntax,
Declaration
&
Implementation
in
C
3
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
2. Comments
2.1.
One-‐line
comments
Comments
are
the
statements
that
are
not
read
by
the
compiler,
and
their
purpose
is
to
explain
certain
pieces
of
code;
however,
one-‐lined
comments
are
used
to
explain
little
pieces
of
code
–
a
couple
of
lines
at
most.
Here
goes
an
example:
// this is a basic one-lined comment.
// if needed, you can add another line.
2.2.
Block
comments
Block
comments
are
used
to
explain
big
pieces
of
code
–
usually,
three
lines
or
more
–
like
function
purposes.
They
are
generally
used
this
way:
/* this is a nice block comment explaining the
purpose of a big function. This kind of comment
is usually written using three lines or more,
so the reader will be “advised” that many lines
will exist */
3. Variables
Every
program
is
supposed
to
have
variables,
right?
Without,
at
least,
one
variable,
we
can’t
do
much.
And
it
is
even
better
to
have
variables
that
people
can
understand
what
they’re
used
for.
So,
to
declare
and
use
variables,
we
are
expected
to
know
some
specifications:
-‐ Use
significant
variable
names;
-‐ Use
proper
spacing;
-‐ Comment,
if
not
every
one,
a
great
part
of
the
variables;
The
syntax
goes
like
this:
[modifiers] [data_type] variable_name;
And
a
great
declaration
of
a
variable
could
be:
int height; // the height of a triangle
Syntax,
Declaration
&
Implementation
in
C
4
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
So
remember,
having
these
three
rules
in
mind
when
declaring
variables
will
make
people
understand
much
more
easily
what
you’re
trying
to
say.
4. Decision
Structures
What
would
be
life
without
making
decisions,
isn’t
it?
A
great
part
of
your
life
is
composed
by
decisions,
and
our
beautiful
programs
won’t
have
a
different
ending.
So,
a
well
written
decision
structure,
as
every
sentence,
will
make
your
program
a
lot
easier
to
read.
Here
goes
the
syntax:
if (expression) {
various_commands;
} else if (another_expression) {
other_commands;
} else {
some_more_commands;
}
And
a
good
implementation
would
be:
if (n1 > n2) {
puts(“n1 is greater than n2.”);
} else if (n1 < n2) {
puts(“n1 is smaller than n2.”);
} else {
puts(“The numbers are equal.”);
}
5. Loops
Moving
on,
our
next
topic
is
the
loop
structures.
The
loop
structure
is
a
piece
of
code
that
keeps
something
repeating
during
a
certain
number
of
times.
There
are,
in
C,
three
loop
strutures:
for,
while
and
do-‐while.
Their
syntaxes,
respectively,
are:
for (counter_init; repetitions; increment) {
some_commands;
}
while (repetitions) {
some_commands;
}
Syntax,
Declaration
&
Implementation
in
C
5
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
do {
some_commands;
} while (repetitions);
So,
good
examples
of
implementation
would
be:
for (i = 0; i < array_size; i++) {
printf(“%d ”, i);
}
i = 0;
while (i < array_size) {
printf(“%d ”, i);
i++;
}
i = 0;
do {
printf(“%d ”, i);
i++;
} while (i < array_size);
6. Functions
Until
this
point,
all
that
we’ve
seen
helps
us
understanding
the
concepts
and
theories
behind
the
real
programming
stuff
(not
that
variables,
decision
structures
and
loops
aren’t
used,
but
without
functions,
pointers
and
structs,
we
can’t
do
much).
So,
for
our
programs
to
be
well
and
easily
understood,
our
functions
must
be
well
written.
In
the
first
place,
we
need
to
know
well
their
syntaxes:
[return_type] [function_name] (parameters);
Notice
that,
when
declaring
the
functions,
the
parameters
don’t
need
to
be
written;
just
their
data
types
will
be
enough
for
the
compiler
to
understand
what
it
is
dealing
with,
as
seen
in:
int squareRoot(int);
Summing
up,
both
function
declarations
are
correct:
int add(int, int);
int add(int number1, int number2);
Syntax,
Declaration
&
Implementation
in
C
6
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
On
the
other
hand,
we
have
the
function
implementations,
in
whose
structure
the
parameters
must
be
written,
as
seen
in:
int add(int number1, int number2) {
return (number1 + number2);
}
Finishing,
when
declaring
functions,
you
don’t
have
to
specify
the
parameters,
just
their
data
type.
But
when
implementing
these
functions,
the
parameters
must
be
specified
at
the
arguments
parenthesis.
7. Pointers
Pointers
are
the
good
stuff
of
the
C
programming
language.
With
the
pointers,
you
have
powers
you
couldn’t
even
imagine.
But,
in
order
to
use
these
wonderful
structures
correctly,
you,
at
first,
must
know
how
to
declare
and
set
their
values.
The
syntax
goes
like
this:
[data_type] *[pointer_name];
The
pointer
data
type
must
be
equal
to
the
data
type
of
the
value
you’re
trying
to
point
to.
So,
if
you
want
to
have
a
pointer
to
an
integer
or
a
char,
the
variable
that
will
be
pointed
to
must
be
an
integer
or
a
char,
respectively,
as
well.
Here
is
an
example:
char *sPtr; // points to a string
You
may
also
use
pointers
in
functions;
observe:
Stack *newStack(); // creates stack
And
its
implementation:
Stack *newStack() {
Stack *sPtr;
sPtr = (Stack *) malloc(sizeof(Stack));
sPtr->top = -1;
return sPtr;
}
And
you
may
even
set
up
functions
to
prevent,
for
example,
malloc
from
being
unsuccessful,
by
returning
a
NULL
value
if
there’s
not
enough
memory;
at
the
end
of
the
document,
there
will
be
some
useful
functions,
including
this
one,
to
organize
your
programs.
Syntax,
Declaration
&
Implementation
in
C
7
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
8. Structs
A
struct
is
another
structure
that
makes
the
C
a
good
language.
Basically,
the
struct
is
a
group
of
variables
of
the
same
data
type,
or
not.
Its
syntax
is
very
simple:
struct [struct_name] {
variables;
};
For
example,
we
can
have
a
struct
called
point,
containing
two
variables,
xValue
and
yValue,
which
correspond
to
the
point’s
values
in
the
cartesian
coordinate
system
(CCS),
as
seen
in:
struct point {
int xValue; // x value in CCS
int yValue; // y value in CCS
}; // don’t forget the ;
As
you
can
see,
this
struct
has
variables
of
the
same
data
type.
But
we
can
also
have
struct
whose
variables
do
not
share
the
same
data
type.
For
example:
struct stack {
int items[10];
int top;
};
In
this
case,
we
have
two
variables,
an
integer
and
an
array.
The
first
represents
the
top
of
the
stack,
and
the
second
represents
the
elements
themselves.
Finally,
to
access
variables
from
the
struct,
you
may
use
this
arrow
“-‐>”
(without
the
quotes).
So,
if
we
want
to
give
the
top
of
a
stack
named
“s”
a
value,
we
may
write:
s->top = some_value; // for example, -1
9. Typedefs
Our
last
topic,
the
typedef
is
strictly
related
to
the
struct.
In
other
words,
the
typedef
is
a
nickname
–
a
name
you
want
to
call
your
struct.:
typedef struct [struct_name] [typedef_name];
Syntax,
Declaration
&
Implementation
in
C
8
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
For
example,
if
we
have
a
struct
called
person,
and
we
are
coding
a
program
about
soccer,
we
can
call
our
“person”
player,
or
coach,
or
even
spectator.
Observe:
struct person {
char *name;
int shirt_number;
char *position;
};
typedef struct person Player;
You
can
also
write
these
two
commands
as
one:
typedef struct person {
char *name;
int shirt_number;
char *position;
} Player;
Syntax,
Declaration
&
Implementation
in
C
9
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
Appendix
A
–
Useful
Functions
In
this
section,
we
will
write
down
some
useful
functions
to
have
in
your
programs,
such
as
error
treatments,
memory
checkings
and
others.
Display
Error
Message
(ec_message)
// requires stdlib.h
void ec_message(char *message) {
char e_message[100];
strcpy(e_message, “[!!] Error ”);
strncat(e_message, message);
perror(e_message);
exit(EXIT_FAILURE);
}
Checks
Insufficient
Memory
For
malloc()
(safe_malloc)
void *safe_malloc(unsigned int size) {
void *mallocPtr;
mallocPtr = malloc(size);
if (mallocPtr == NULL) {
printf(“Not enough memory!\n”);
ec_message(“in safe_malloc());
}
return mallocPtr;
}
Randomizes
numbers
for
an
array
(generate_array)
// requires an Array typedef
// requires time.h
Array *generate_array(Array *array, int size) {
srand(time(NULL));
int i;
for (i = 0; i < size; i++) {
array[i] = rand() % 10;
}
}
Syntax,
Declaration
&
Implementation
in
C
10
Syntax,
Declaration
&
Implementation
in
C
January
27,
2011
Appendix
B
–
About
Wageless4ction
Team
You
can
find
about
our
group
on
the
internet,
by
accessing
http://www.w4team.cc
(W4
stands
for
Wageless4ction
Team).
We
are
believers
of
the
free
information
cause,
where
useful
stuff
should
be
shared,
in
place
of
being
“hidden”.
We
offer
various
programming
languages’
concepts
and
popular
puzzles,
and
a
news
blog
(within
the
site)
that
is
updated
everyday!
If
you
want
to
learn
how
to
think
outside
the
box,
fastly
and
efficiently,
don’t
hesitate
in
sending
a
request
to
learn
programming
languages
with
us!
If
you
know
how
to
program,
it’s
even
better!
You
can
apply
as
a
coding
volunteer
to
write
programs
in
certain
languages;
you
will
be
told
about
available
spots!
Enter
http://www.w4team.cc
and
help
the
good
hacking
cause!
Syntax,
Declaration
&
Implementation
in
C
11