C Programming Structures
C Programming Structures
LAB Programming in C
What is programming?
Series of instructions to a computer to accomplish
a task
Revision
Instructions must be written in a way the
computer can understand
A few reminders
Programming languages are used to write
programs
1
10/12/2022
C C
1972: 1983-1988:
2
10/12/2022
C C
Source file
3
10/12/2022
C Programming C Programming
A function consists of a list of statements, either: functions that you write yourself or
single instructions that are part of the C functions that are provided with the C
language itself, or
language.
calls to functions
C Programming C Programming
4
10/12/2022
1. Edit
a program which combines libraries and
2. Preprocess
the machine code generated from a
program into a single executable. 3. Compile
4. Link
Normally a single programme does both
5. Load
compiling and linking.
6. Execute
1. Edit
4. Link
2. Preprocess
3. Compile
4. Link
5. Load 5. Load
6. Execute
6. Execute
Source: www.tutorialsspace.com Source: www.tutorialsspace.com
5
10/12/2022
Flow of C
Program
Execution
A C Program’s
Structure
Objective
6
10/12/2022
Hello World!
Hello World! Explained
#include <stdio.h>
#include
main()
aka a preprocessor directive.
{ It tells the compiler to "include" text from
another file into your source code.
printf("Hello World!\n");
Else you will have errors.
}
These functions are required by most C programs. … before the source code is compiled.
7
10/12/2022
Hello World!
A C Program
Explained
int main
/*tells the compiler that we are using The microprocessor starts at a specific
the stdio library, and that this library starting point.
should be linked to our executable. */
In all C programs, the starting point is the
main() function.
8
10/12/2022
Hello World!
A C Program
Explained
int main ()
9
10/12/2022
Since it’s a function, write it as printf(). This library consists of input and output
routines.
It displays information on the screen.
Used to print strings…
10
10/12/2022
\n is interpreted as a single special character… Causes the print routine to output an end-of-
line character to the monitor
the end-of-line character…
(or whatever the standard output of the
the one produced when you press the Enter key.
computer is).
It’s called a newline in C.
The next piece of output will start on the next
line.
11
10/12/2022
Fortunately, compilers easily detect them This command sends the value 0 (zero)
and warn you about them. back to the operating system when the
main() function is done.
Thus they’re usually easy to catch.
12
10/12/2022
13
10/12/2022
Hello World!
Explained
The new command prompt will be on the line
END OF LAB
below.
Local Declarations
Statements
14
10/12/2022
The C Program
Structure
i.e.:
i. Documentation i. Documentation
Section Section
Comments
e.g. program name, Comments are not
the author,
compiled:
date,
program version, their syntax is NOT
algorithms, methods used …
checked.
/* This program displays natural
numbers from 1 to 10 */
15
10/12/2022
i. Documentation i. Documentation
Section Section
If not compiled, what’s the point of having them? Comments at the top of the
Several reasons - we use them to: program
describe what a particular variable is for,
provide information about the
describe the purpose of a function and how it program.
works,
document the name, version number, purpose, and The rest
programmer of an entire program,
explain any tricky or hard to understand parts of clarify the code where necessary
a program.
i. Documentation i. Documentation
Section Section
Two ways of commenting You’ll often see something like this for multiline comments:
in C: /*
* Author: WWW
comments */
The only syntactically required asterisks are the ones enclosing the
comment; the others are just for style.
16
10/12/2022
Readability
Crucial.
Two quick and simple ways:
Slight but related diversion…
1. Have a sensible amount of
comments.
Readability 2. Utilise
whitespace/indentations
Readability
Readability
1. Have a sensible amount of comments. 2.Whitespace/Indentations
Self-documenting code reduces the need for
comments. C ignores white space.
That is, code E.g. the compiler doesn’t care about
with a clear and clean structure and indentation.
meaningful identifiers Whitespace/Indentations are
E.g., for variables and functions components of code quality - they:
You wouldn’t need a separate comment to improve readability.
explain the purpose of a function called
add_two_numbers()
show dependencies.
17
10/12/2022
Readability/Dependencies
ii. Link/Header
Declaration Section
int main()
} compiler:
18
10/12/2022
19
10/12/2022
Header Files
Header Files
"contain the information necessary
#define -> macros (short form of
to use these libraries" …
"macroinstructions").
1.function prototypes (function symbols, names, or keys representing a
definitions/declarations for list of commands, actions, or keystrokes.
functions included in the libraries.
They are programmable patterns that
2.structure declarations translate a sequence of input into a
preset sequence of output.
3.typedef statements
Non C example: omw for On my way!
4.#define directives
20
10/12/2022
Header Files
C example: Header Files
#define square(x) ((x) * (x))
Are a software reuse technique.
int num = square(5);
/* Their names are in lower case
letters
is the same as writing:
int num = ((5) * (5)); C is case sensitive to their names
…which will declare an integer type Called "Header" files as they are
variable called num, and set its value to 25. usually at the top (head) of the
source file.
*/
21
10/12/2022
22
10/12/2022
Structure of a Note
Function All C statements are terminated with a semi colon.
The following are NOT considered to be statements:
int main() //defines a function the preprocessor directive e.g. when you are
including a library
the function header e.g. int main()
{ //specifies the beginning of a function
rather than being a statement of code, it defines
the function
the curly braces
} //specifies the end of a function
these specify the start and end of the function
23