Introduction to Algorithms and Programming
Reading source code
Reading source code to learn programming
The habit of reading source code helps programmers to:
- Learn problem solving techniques
- Recognize good software development practices
- Recognize the negative consequences of bad software development practices
- Learn programming techniques
In professional environments, reading source code is often required to:
- Improve existing software
- Fix errors in existing software
- Re-use techniques and existing software
- Inspect software during the testing phase
C is a procedural language
function is required for all C programs
- The main() function is the where the program starts.
- The program ends when the main() function ends.
- The main() function should be defined before all other functions.
The preprocessor directive
is used to include the source code from the stdio.h header file
into the current program.
- Header files have names ending with a .h extension.
- Header files used with a program should be included at the top of the source code file for easier maintenance.
C programs use 2 types of statements
- Simple statements end with a semicolons.
- Compound or complex statements are enclosed in curly braces. They contain one or more simple statements.
Function calls and input parameters
require a pair of parentheses
after the name of the function.
- Optionally, input parameters are provided by including it within the parentheses.
- A comma is required to separate multiple input parameters.
Source code comments
start with /*
and continue until it reaches a matching */
Do not use C++, Java or PHP style comments
, which use a double forward slash
at the start of comments.
// Do NOT use comments in this style
// They reduce portability between C compilers
When reading the source code, pay attention to the usage of curly braces, parentheses and semicolons in the programs.
Steps to create C programs
Reserved keywords in the C language
be used as names of functions or data variables.