fundamentals of the c programming language
Post on 14-Apr-2018
231 Views
Preview:
TRANSCRIPT
-
7/30/2019 Fundamentals of the C Programming Language
1/23
Fundamentals of the CProgramming Language
-
7/30/2019 Fundamentals of the C Programming Language
2/23
Objectives
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: logic/design error,syntax error, run-time error
-
7/30/2019 Fundamentals of the C Programming Language
3/23
The system software necessary to develop Capplication program are bundled into an integrateddevelopment 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 & programmer-defined libraries)
C Program Development
Environment
-
7/30/2019 Fundamentals of the C Programming Language
4/23
IDE Microsoft Visual C++
-
7/30/2019 Fundamentals of the C Programming Language
5/23
-
7/30/2019 Fundamentals of the C Programming Language
6/23
1. Prepare program (and data files) Use a text editor to type your source program statements.
Assign a name to your source program file (.C) and savethe file into disk storage.
(optional) Use the text editor to create a data files. Assignnames to the data files (.txt or .dat) and save the files
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.
-
7/30/2019 Fundamentals of the C Programming Language
7/23
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 theoutput otherwise debug the program
-
7/30/2019 Fundamentals of the C Programming Language
8/23
Output:
A Simple C Program Example
-
7/30/2019 Fundamentals of the C Programming Language
9/23
Elements of the C language/* Example Case: Apples
Author: Mdm Badariah Solemon
*/
#include
int main()
{
int Qty;
double Cost=0.0, Total=0.0;
printf ("Enter quantity of apples purchased (in Kg):");
scanf("%d", &Qty);
printf ("Enter the cost per Kg of apples (in RM per Kg):");
scanf("%lf",&Cost);
Total = Cost * Qty;
printf("\nTotal cost of apples = %.2f\n",Total);
return 0;
}
preprocessor
directive
variable
comment
specialsymbol
reservedword
punctuation
standard header file
statement
-
7/30/2019 Fundamentals of the C Programming Language
10/23
Program Comments
Statement in a program intended fordocumentation and clarification purposes
Have no effect on program execution.
Comments are inserted into the code using/*and */ to surround comment or// to begincomment lines./* This is a comment */
// This is known as comment line
/* The comments can span into morethan one lines */
-
7/30/2019 Fundamentals of the C Programming Language
11/23
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) identifies the
header file for standard input and output needed by
the printf().
-
7/30/2019 Fundamentals of the C Programming Language
12/23
Functions
Every C program has a functionmain
The functionmain usually calls input/outputfunctions printf(),scanf(), etc.
These input/output functions are discussed inChapter 4. The word main is a C reservedword. We must
not use it for declaring any other variable orconstant.
4 common ways ofmain declaration:int main(void)
{
return 0;
}
void main(void)
{
}
main(void)
{
}
main( )
{
}
-
7/30/2019 Fundamentals of the C Programming Language
13/23
The curly braces { } identify a segment / bodyof a program
The start and end of a function
The start and end of the selection or repetitionblock.
Since the opening brace { indicates the startof a segment with the closing brace indicating
the end of a segment, there must be just asmany opening braces as closing braces }(this is a common mistake of beginners)
-
7/30/2019 Fundamentals of the C Programming Language
14/23
Statements
A specification of an action to be taken by thecomputer as the program executes.
Each statement in C needs to be terminated withsemicolon (;)
Example:
#include
void main(void)
{
printf(I love programming\n);printf(You will love it too once );
printf(you know the trick\n);
}
-
7/30/2019 Fundamentals of the C Programming Language
15/23
Statement has two parts :
Declaration
The part of the program that tells the compiler the
names of memory cells in a program
Executable statements
Program lines (excluding comments) that areconverted to machine language instructions andexecuted by the computer.
A list of statements may be enclosed in braces {} (known as compound statement)
int age, total=0.0;
-
7/30/2019 Fundamentals of the C Programming Language
16/23
General form of a C program
preprocessor directives
main function heading
{
declarations
executable statements
}
#include
void main(void)
{
// statement(s);
}
-
7/30/2019 Fundamentals of the C Programming Language
17/23
C statements can extend over more than one
line. Example:
printf (\n Your coins are worth %d dollarsand %d cents.\n, dollar, change);
You can write more than one statement on a
line (but not recommended). Example:
printf(Enter your name:); scanf(%,&Name);
-
7/30/2019 Fundamentals of the C Programming Language
18/23
Common Programming Errors
Debugging - Process removing errors from
a program
Three (3) kinds of errors :
1. Syntax Error
a violation of the C grammar rules, detected during
program translation (compilation).
statement cannot be translated and program cannotbe executed
-
7/30/2019 Fundamentals of the C Programming Language
19/23
-
7/30/2019 Fundamentals of the C Programming Language
20/23
2. Run-time errors
An attempt to perform an invalid operation, detected
during program execution.
Occurs when the program directs the computer toperform 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 **
** depends on IDE Ms Visual Studio: warning
-
7/30/2019 Fundamentals of the C Programming Language
21/23
-
7/30/2019 Fundamentals of the C Programming Language
22/23
3. Logic Error/Design Error
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 algorithmand written program before you actually type it
-
7/30/2019 Fundamentals of the C Programming Language
23/23
Summary
You learned about: 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.
top related