0% found this document useful (0 votes)
23 views19 pages

Compiling and Debugging C Programs

The document outlines the process of writing and compiling a C program, detailing the types of files involved such as source code, header, object, and binary executable files. It describes the compilation steps including writing code, compiling, linking, and executing the program, as well as the structure of a C program. Additionally, it covers testing and debugging, types of errors (runtime, compile, and logical), and provides examples of each error type.

Uploaded by

wawudadora97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • programming environment,
  • function declarations,
  • preprocessor macros,
  • programming methodology,
  • runtime errors,
  • binary executable,
  • user-friendly programming,
  • program output,
  • types of errors,
  • programming input
0% found this document useful (0 votes)
23 views19 pages

Compiling and Debugging C Programs

The document outlines the process of writing and compiling a C program, detailing the types of files involved such as source code, header, object, and binary executable files. It describes the compilation steps including writing code, compiling, linking, and executing the program, as well as the structure of a C program. Additionally, it covers testing and debugging, types of errors (runtime, compile, and logical), and provides examples of each error type.

Uploaded by

wawudadora97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • programming environment,
  • function declarations,
  • preprocessor macros,
  • programming methodology,
  • runtime errors,
  • binary executable,
  • user-friendly programming,
  • program output,
  • types of errors,
  • programming input

Writing and compiling a C program

Types of files

• Source code files. These files contain function


definitions, and have names which end in .c extension.
• Header files. These files contain function declarations
(function prototypes) and various preprocessor
statements. They are used to allow source code files to
access externally defined functions. Header files end in
.h extension.
Types of files

• Object files. These files are produced as the output of


the compiler. They consist of function definitions in
binary form, but they are not executable by
themselves. Object files often end in .obj.
• Binary executable. These are files produced as the
output of a program called a linker. The linker links
together a number of object files to produce a binary
file which can be directly execute.
Preprocessors

• The first step of the compile process is a special


preprocessor program.
• The preprocessor program reads the source file as
text, and produces another text file as output.
Examples of preprocessors.

• #define : Substitutes a preprocessor macro.


• #include : Inserts a particular header from another
file.
• #undef: Undefines a preprocessor macro.
• #ifdef: Returns true if this macro is defined.
Steps for writing and compiling c

• Step 1: Writing the code


– Use an editor to write the source code. Save the source
code files with .c extension .For example, myprog.c.
• Step 2: Compile the source code.
– Compile the program using a compiler. If the compiler
doesn't find any errors in the program, it produces an object
file.
Steps for writing and compiling c

• Step 3: Linking the program


– Link the program using a linker. If no errors occur, the linker
produces an executable program located in a disk file with
an .exe extension and the same name as the object file for
example; [Link] is linked to create [Link].
• Step 4: Execute the program.
– Test the program to determine whether it functions
properly.
Program structure in C

A program in c has the following components:


• #include <stdio.h> : This is the first line of the
program.
• main(): The main function is where program execution
begins.
• Comment(s): Any statement enclosed by /*...*/ will be
ignored by the compiler and is put to add additional
comments in the program.
Program structure in C

• { } : The opening and closing braces are used to enclose a


function block in this case main.
• printf(...); : This library function available in C which
prints(outputs) string on the screen.
• scanf(…); This library function available in C which takes(input)
from the user e.g. through the keyboard.
• Statement(s): It is a complete instruction that directs the
computer to carry out some [h].
• return 0; terminates main()function and returns the value 0 to
the operating system.
Exercise

• Write a simple C program that prints the text “Hello


World” on the screen.
Exercise

• Write a simple C program that will display the


following text outputs.

Welcome to Programming Methodology.


The mode of study is online.
Thank you for choosing KCA University.
Writing and compiling C program
• Write a simple C program that prints the text
below:
This is C Programming.
This programming language is user
friendly.
Testing and debugging

• Testing is a process of finding defects in the code or


finding if the program does what it is supposed to do.
• Debugging is the process of finding and removing a
specific bug(s)/error(s) from the program. Testing
process can be manual or automated but debugging is
always manual.
Types of errors
• In programming, an error also known as a bug is a
mistake or omission that may prevent the program
from compiling and running correctly as per the
expectation of the programmer.
• There are three types of errors in programming:
– Runtime Errors
– Compile Errors
– Logical Errors
Runtime Errors

• These are errors that occur during the execution of a


program.
• These errors occur due to some illegal operation performed in
the program.
• These errors may stop program execution.
• Some illegal operations that may produce runtime errors are:
– Dividing a number by zero
– Trying to open a nonexistent file
– Lack of free memory space
Compile Errors

• Compile errors are those errors that occur at the time


of compilation of the program. These errors may be
further classified as:
– Syntax Errors
– Semantic Errors
Syntax Errors

• These are errors detected during compile time when


the rules of the programming language are not
followed.
– Example printf("Hello World!\n"):

The above statement in a c program will produce syntax error


as the statement is terminated with : rather than ;
Semantic Errors
• These are errors reported by the compiler when the
statements written in the program are not meaningful
to the compiler. Usually arise from wrongly typed
expressions and formulas.
– Example a+b=sum;
• In the above statement we are trying to assign value
of sum in the value obtained by summation of a and b
which has no meaning in a program in c. The correct
statement will be sum=a+b;
Logical Errors
• These are the errors in the output of the program. The
presence of logical errors leads to undesired or
incorrect output and are caused due to error in the
logic applied in the program to produce the desired
output.
• These errors may not be detected by the compiler and
so the programmer has to check the entire coding of
the program line by line.

You might also like