C Programming(First Unit)
C Programming(First Unit)
BSc.CSIT
Semester-First
Unit-1
(Problem solving with computer)
5
Programmer
A programmer is a person who writes computer programs. Programmers create software applications
that are used by individuals, businesses, and organizations to perform specific tasks.
The duties of a programmer typically include:
1. Analysing the requirements for a new software program.
2. Designing a plan for the program, including choosing the appropriate programming language and
tools.
3. Writing code for the program, following best practices for writing clear, efficient, and maintainable
code.
4. Debugging and testing the program to identify and fix any errors or bugs.
5. Documenting the program and its code, so that others can understand how it works and make changes
as needed.
6. Maintaining and updating the program as necessary, including fixing bugs, adding new features, and
improving performance.
7. Collaborating with other team members, including designers, project managers, and other
programmers.
8. Staying up-to-date with new technologies, programming languages, and best practices in the field.
13
Algorithm
An algorithm is a sequence of instructions to solve a problem.
It is written in human understandable language like: English.
The algorithms are language independent i.e. the instructions
written in algorithms can be implemented in any language It
is written in simple English language.
Step 1: Start
Step 2: Declare variable P,T,R and I
Step 3: Read values P,T and R
Step 4: : Calculate Interest, I =(P*T*R)/100
Step 6: Display, I
Step 7: Stop
Compile by :-Pradip Khatiwoda 18
Example 2:-Algorithm to add two numbers entered by the
user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum=num1+num2
Step 5: Display sum
Step 6: Stop
Step 1: Start
Step 2: Declare variables N and R.
Step 3:Read the value of variable N.
Step 4:Devide N by 2 and calculate Remainder R
Step 5:Is(R=0)?
if yes ,Display N is even number
If no, Display N is odd number
Step 5: Stop
Step 1: Start
Step 2: I=2
Step 3: Display I
Step 4: I=I+2;
Step 5: if(I<=100) then go to step 3 else go to step 6
Step 6: Stop
52
Basic Structure of C program
There are some variables that are used in more than one function. Such variables
are called global variables and are declared in the global declaration section that is
outside of all the functions.
This section also declares all the user-defined functions.
E.g. int a=10;
int sum(int, int);
1. Syntax error
2. Run-time error
3. Linker error
4. Logical error
5. Semantic error
75
Linker error
A linker error occurs when the linker, which is a program that
combines object files and libraries to create an executable program,
encounters a problem while trying to link them together.
For example, if you have a program that uses a function from a library,
but that library is not included or cannot be found, the linker will
generate a linker error. Another common reason for linker errors is
when there are multiple definitions of the same function or variable
Linker errors are usually displayed as error messages during the
compilation process and prevent the executable from being generated.
To resolve linker errors, you need to identify the cause of the error and
make the necessary changes to your code
int main() {
printf("The sum of %d and %d is %d\n", 5, 10); // semantic error - not enough arguments
return 0;
}
In this example, the printf function is called with three format specifiers, but only two arguments are passed in.
According to the rules of C, each format specifier must have a corresponding argument of the correct type, so this
program will not compile due to a semantic error. To fix this semantic error, the programmer should pass in a third
argument for the missing format specifier:
#include <stdio.h>
int main() {
printf("The sum of %d and %d is %d\n", 5, 10, 5 + 10); // fixed semantic error - correct number of arguments
return 0;
}