Syntax, Declaration & Implementation in C
Syntax, Declaration & Implementation in C
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
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
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.
3. Variables
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;
}
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).
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;
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
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];
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)