Fundamentals of The C Programming Language
Fundamentals of The C Programming Language
Objectives
General form of a C program Common Programming Errors: logic/design error, syntax error, run-time error
The system software necessary to develop C application program are bundled into an integrated development environment (IDE) A typical IDE contains text editor, C compiler, preprocessor, libraries, other tools The C programming environment has 2 components:
Language features to carry out basic operations (e.g.: store data in variables, compare 2 data values, perform arithmetic operation, etc.) Libraries routines to carry out operations not part of the language (Standard libraries e.g.: #include <stdio.h> & programmer-defined libraries)
Steps:
Prepare Compile Link
Execute
These steps depend on the computer, the OS, and the C IDE that you will use.
1.
2.
Compile
Perform by a compiler. C compiler has 2 separate programs: the preprocessor and the translator. Preprocessor reads the source code and prepares the code for the translator. Translator translates the code into machine language.
3.
Link (Build)
As we will see later, a C program is made up of many functions. The linker assembles all the functions into final executable program (creates exe file).
4.
Execute
If successful, it is ready for execution and get the output otherwise debug the program
Output:
reserved word
statement
punctuation
special symbol
Program Comments
Statement in a program intended for documentation and clarification purposes Have no effect on program execution. Comments are inserted into the code using /* and */ to surround comment or // to begin comment lines.
/* This is a comment */ // This is known as comment line /* The comments can span into more than one lines */
Preprocessor directives
The C program line that begins with # provides an instruction to the C preprocessor It is executed before the actual compilation is done. Two most common directives :
#include #define
In our example (#include<stdio.h>) identifies the header file for standard input and output needed by the printf().
Functions
Every C program has a function main The function main usually calls input/output functions printf(), scanf(), etc.
The word main is a C reserved word. We must not use it for declaring any other variable or constant. 4 common ways of main declaration:
int main(void) { return 0; } void main(void) {
}
main(void) {
}
main( ) {
}
The start and end of a function The start and end of the selection or repetition block.
Since the opening brace { indicates the start of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces } (this is a common mistake of beginners)
Statements
A specification of an action to be taken by the computer as the program executes. Each statement in C needs to be terminated with semicolon (;) Example:
#include <stdio.h> void main(void) { printf(I love programming\n); printf(You will love it too once ); printf(you know the trick\n); }
Declaration
The part of the program that tells the compiler the names of memory cells in a program
int age, total=0.0;
Executable statements
Program lines (excluding comments) that are converted to machine language instructions and executed by the computer.
// statement(s);
}
You can write more than one statement on a line (but not recommended). Example:
printf(Enter your name:); scanf(%,&Name);
1.
Debugging - Process removing errors from a program Three (3) kinds of errors :
Syntax Error
a violation of the C grammar rules, detected during program translation (compilation). statement cannot be translated and program cannot be executed
2.
Run-time errors
An attempt to perform an invalid operation, detected during program execution. Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected **
3.
An error caused by following an incorrect algorithm Very difficult to detect - it does not cause run-time error and does not display message errors. The only sign of logic error incorrect program output Can be detected by testing the program thoroughly, comparing its output to calculated results To prevent carefully desk checking the algorithm and written program before you actually type it
Summary
C Program development environment Elements of the C language Program comments Preprocessor directives Functions Executable Statements General form of a C program Common Programming Errors: syntax error, run-time error, logic/design error.