Computer Programming and
Algorithms II
Ch7 – IDE, First Program and Error Types
Why Learn C?
• It is one of the most popular programming languages in
the world.
• If you know C, you will have no problem learning other
popular programming languages such as Java, Python,
C++, C#, etc, as the syntax is similar
• C is very fast, compared to other programming
languages, like Java and Python
Difference between C and C++
• C++ was developed as an extension of C, and both
languages have almost the same syntax
• The main difference between C and C++ is that C++
supports classes and objects, while C does not.
Get Started With C
• To start using C, you need two things:
• A text editor, like Notepad, to write C code
• A compiler, like GCC, to translate the C code into a language that the
computer will understand
• There are many text editors and compilers to choose from. In this
lecture, we will use an IDE.
Get Started With C
• An IDE (Integrated Development Environment) is used to edit AND
compile the code.
• Popular IDE's include Code::Blocks, Dev C++, Eclipse, and Visual
Studio. These are all free, and they can be used to both edit and
debug C code.
Note: Web-based IDE's can work as well, but functionality is
limited.
C Integrated Development Environment (IDE)
• Dev C++ ,Download it for programming in C.
• [Link]
C Quickstart
• Let's create our first C file.
• Open Dev C++ File > New > Source File.
• Write the following C code;
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
C Quickstart
Line 1: #include <stdio.h> is a header file that allows us to use input and
output functions, such as printf() (which is used on line 4). Header files extend
the functionality of C programs.
Line 2: A blank line follows. While C ignores whitespace, we use it to improve
code readability.
Line 3: Another essential component of a C program is the main() function.
This function serves as the program’s entry point, and any code placed inside
its curly braces {} will be executed.
Line 4: The printf() function is used to display text on the screen. In this case, it
prints "Hello, World!" as output.
Line 5: return 0 ends the main() function.
Line 6: Do not forget to add the closing curly bracket } to actually end the main
function.
Important Points
• Every C statement ends with a semicolon ;
• The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
• The compiler ignores white spaces. However, multiple lines
makes the code more readable.
C Quickstart
Steps in Compilation:
1. Preprocessing: Handles #include, #define, and macro expansion.
#include <stdio.h> // Replaces with the actual contents of stdio.h
2. Compilation: Translates preprocessed code into assembly language.
Converts preprocessed C code into assembly language (.s file).
3. Assembly: Converts assembly code into machine code (object files).
Converts assembly code into machine code, generating an object file (.o
or .obj).
4. Linking: Combines object files and libraries into an executable file.
Combines object files and external libraries (like stdio.h functions) into a
final executable file.
C Quickstart
• While saving your file, its better to choose your program as C
source file.
Comments in C
• Comments can be used to explain code, and to make it
more readable.
• It can also be used to prevent execution when testing
alternative code.
• Comments can be singled-lined or multi-lined.
Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the
compiler (will not be executed).
• This example uses a single-line comment before a line of code:
// This is a comment
printf("Hello World!");
Single-line Comments
• This example uses a single-line comment at the end of a line of
code:
printf("Hello World!"); // This is a comment
C Multi-line Comments
• Multi-line comments start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler:
/* The code below will print the words Hello World!
to the screen*/
printf("Hello World!");
Statements
• A computer program is a list of "instructions" to be "executed" by a
computer.
• In a programming language, these programming instructions are
called statements.
• The following statement "instructs" the compiler to print the text
"Hello World" to the screen:
printf("Hello World!");
Many Statements
• Most C programs contain many statements.
• The statements are executed, one by one, in the same order as
they are written:
printf("Hello World!");
printf("Have a good day!");
return 0;
Error Types
1. Syntax Errors
2. Runtime Errors
3. Logical Errors
4. Linked Errors
5. Semantic Errors
1. Syntax Errors
• These are also referred to as compile-time errors.
• These errors occurs when the rule of C writing techniques or
syntaxes has been broken.
• These types of errors are typically flagged by the compiler prior to
compilation.
• The most commonly occurring syntax errors in C language are:
• Missing semi-colon (;)
• Missing parenthesis ({})
• Assigning value to a variable without declaring it
Example
#include <stdio.h>
int main()
{
printf("Hello!")
return 0;
}
Example
Example
#include <stdio.h>
int main()
printf("Hello");
return 0;
}
2. Runtime Errors
• Errors that occur during the execution (or running) of a program
are called RunTime Errors.
• These errors occur after the program has been compiled
successfully.
• When a program is running, and it is not able to perform any
particular operation, it means that we have encountered a run
time error.
Example
• Mathematically Incorrect Operations; Dividing a number by zero
• Mistakes in the Code; infinite loops
• Memory Leaks; producing infinite arrays or holding values
exceeding memory size.
Example
#include<stdio.h>
void main() {
int var = 2147483649;
printf("%d", var);
}
Output
#include<stdio.h>
void main() {
int var = 2147483649;
printf("%d", var);
}
-2147483647
[Link] Error
• Logical errors are those errors in which we think that our code is
correct, the code compiles without any error and gives no error
while it is running, but the output we get is different from the
output we expected.
• In 1999, NASA lost a spacecraft due to a logical error. This
happened because of some miscalculations between the English
and the American Units. The software was coded to work for one
system but was used with the other.
Example
#include <stdio.h>
void main() {
float a = 10;
float b = 5;
if (b = 0) {
printf("Division by zero is not possible");
} else {
printf("The output is: %f", a/b);
}
}
Example
#include <stdio.h>
int main() {
int a = 10, b = 5;
int sum = a - b; // Logic Error!
printf("Sum: %d\n", sum);
return 0;
}
4. Semantic Error
• Errors due to an improper use of program statements.
• A semantic error will be generated if the code makes no sense to
the compiler.
• Indicates that the statement is out of scope.
• Error message is obtained.
4. Semantic Error
• The most commonly occurring semantic errors are:
• use of un-initialized variables,
• type compatibility,
• array index out of bounds.
Example
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int j;
int a, b, c;
j++; // Semantic Error!
a * b = c; // Semantic Error! return 0;
return 0; }
}
5. Linker error
• When the program is successfully compiled and attempting to link
the different object files with the main object file, errors will occur.
• When this error occurs, the executable is not generated.
• This could be due to incorrect function prototyping, an incorrect
header file
A linker error
#include <stdio.h>
// Driver code
int Main()
{
printf("Geeks for Geeks");
return 0;
}