C-Lecture-1 Structured Programming Language
C-Lecture-1 Structured Programming Language
CSE 1101
Sabina Yasmin
Assistant Professor
Dept. of CSE
Varendra University
What is a program
⚫ A program is a set of instructions that a computer
uses to perform a specific function.
⚫ A program is a sequence of instructions that a
computer can interpret and execute.
main()
{
}
return 0 ;
return 0;
} } } }
The curly braces { }
⚫ \n is an escape sequence
⚫ moves the cursor to the new line
The scanf function cont…
⚫If you want the user to enter more than one value,
you serialise the inputs.
⚫Example:
float height, weight;
printf(“Please enter your height and weight:”);
scanf(“%f%f”, &height, &weight);
Few notes on C program…
⚫C is case-sensitive
⚫ Word, word, WorD, WORD, WOrD, worD, etc are all
different variables / expressions
Eg. sum = 23 + 7
⚫ What is the value of Sum after this addition ?
⚫Comments
⚫ are inserted into the code using /* to start and */ to end a
comment
⚫ Some compiler support comments starting with ‘//’
⚫ Provides supplementary information but is ignored by the
preprocessor and compiler
⚫ // This is a comment
⚫ /* This program is written by the students
of Varendra */
Common Programming Errors
⚫Debugging Process removing errors from a
program
⚫Three (3) kinds of errors :
⚫ Syntax Error
⚫aviolation of the C grammar rules, detected
during program translation (compilation).
⚫ If errors then statement cannot be translated and
program cannot be executed
Common Programming Errors
cont…
⚫ 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
Common Programming Errors
cont…
⚫Logic Error/Design Error
Algorithm
1. Start
2. Read n1.
3. Read n2.
4. Calculate Sum = n1 + n2.
5. Write the sum.
6. Stop.
Pseudo Code
⚫ More formal presentation than the algorithm.
⚫ Here we represent every step in a formal way which is very
close to the actual programming language representation.
1. BEGIN
2. input num 1,num 2
3. sum = num 1 + num 2
4. diplay total
5. END
Flow Chart
statements/steps.
Print Sum
Stop
Flowchart Symbols
Beginning or end of
oval START
program
Read n
Parallelogram Input or Output or
Print n
Direction of logic
flow
Flowchart Symbols
Rectangular
Processing Sum = a+b
Connector
Write a program to add two numbers
Start
Read n1
Algorithm Read n2
1. Start
2. Read n1.
3. Read n2. Sum = n1 + n2
Stop
Write a program to find bigger one between two numbers
Start
Read n1
Algorithm Read n2
1. Start
2. Read n1. No
3. Read n2.
Yes
if
4. If n1>n2. n1 > n2
print n1 is
?
bigger
5. else
Print n1 Print n2
print n2 is
bigger
6. Stop.
Stop
Write a program to Find Sum of first n numbers
Start
Read n
Algorithm i=1
1. Read n. Sum = 0
2. Initialize N=1.
3. Initialize sum S=0. Sum = Sum + i
4. Calculate S=S+N. i = i+1
5. Calculate N=N+1. No
6. If N>n, then goto step 7 else goto step 4.
7. Write the sum S. If i > n?
8. Stop. Yes
Print sum
Stop
12 Best Websites to Practice Coding for Beginners