0% found this document useful (0 votes)
5 views

C-Lecture-1 Structured Programming Language

Uploaded by

pkpartho069
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C-Lecture-1 Structured Programming Language

Uploaded by

pkpartho069
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

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.

⚫ MS Word, Power point, Excel are all computer programs


The following flowchart shows the history of ANSI C:
Importance of C:
⮚ C language is efficient and fast.
⮚ C is highly portable.
⮚ C language is well suited for structured
programming.
⮚ C is a machine independent language.
⮚ C has the ability to extend itself
Basic Structure of C
Compiler converts
human readable
language to a
language which is
understandable by
the operating
system/hardware

The C compiler takes source


code (samp.c) as input and
turns it into machine
readable form.
A Simple C Program
● #include <stdio.h> //This is preprosessor directive
● int main ( void ) //this tells the starting point of your program
● {
● printf(“Hello World\n”) ; //print the text on monitor
● return 0 ; //return to operating system
● }
Program Header Comment
⚫A comment is descriptive text used to help a reader of
the program understand its content.
⚫All comments must begin with the characters /* and
end with the characters */
⚫These are called comment delimiters
Preprocessor directives
⚫ It is a program that process the source code before it
passes through the compiler.
⚫ A C program line 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<stdio.h>) identifies the header file
for standard input and output needed by the printf().
int main ( void )
⚫Every program must have a function called
main. This is where program execution begins.
⚫The reserved word “int” indicates that main()
returns an integer value.
⚫The parentheses following the reserved word
“main” indicate that it is a function.
⚫The reserved word “void” means nothing is there.
The Function Body
⚫A left brace (curly bracket) -- { -- begins the body of
every function. A corresponding right brace -- } --
ends the function body.

main()
{

}
return 0 ;

⚫Because function main() returns an integer value, there


must be a statement that indicates what this value is.
⚫The statement
return 0 ;
indicates that main() returns a value of zero to the operating
system.
⚫A value of 0 indicates that the program successfully
terminated execution.
The scanf function
⚫Read data from the standard input device (usually
keyboard) and store it in a variable.
⚫General format:
⚫ scanf(“Format string”, &variable);
⚫Notice ampersand (&) operator :
⚫ C address of operator
⚫ it passes the address of the variable instead of the
variable itself
⚫ tells the scanf() where to find the variable to store the
new value
Another C Program (con’t)
#include <stdio.h>
int main( void )
{
int value1, value2, product ;
printf(“Enter two integer values: “) ;
scanf(“%d%d”, &value1, &value2) ;
product = value1 * value2 ;
printf(“Product = %d\n”, product) ;
return 0 ;
}
Function main
⚫Identify the start of the program
⚫Every C program has a main ( )
⚫'main' is a C keyword. We must not use it for
any other variable.
⚫4 common ways of main declaration
int main(void) void main(void) main(void) main( )
{ { { {

return 0;
} } } }
The curly braces { }

⚫Identify a segment / body of a program


⚫ The start and end of a function
⚫Since the opening brace indicates the start of a
segment with the closing brace indicating the end of
a segment, there must be just as many opening
braces as closing braces (this is a common mistake
of beginners)
Statement
⚫ A specification of an action to be taken by the computer as the
program executes.
⚫ Each statement in C needs to be terminated with semicolon (;)
⚫ Example:
#include <stdio.h>
int main(void)
{
statement
printf(“ This is our new program\n”); statement
printf(“To be a good programmer ”);
statement
printf(“we all have to learn more \n”); statement
return 0;
}
Statement cont…
⚫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 that are converted to machine language
instructions and executed by the computer
C program skeleton
⚫In short, the basic skeleton of a C program looks like
this:

#include <stdio.h> Preprocessor directives


int main(void) Function main
{ Start of segment
statement(s);
return 0;
}
End of segment
Compile Time and Run Time
Compile time : Compile time refers to the event that
occur during the compilation process.

Run time : Run time refers to the event that occur


while the program is actually executing.
Input/Output Operations
⚫Input operation
⚫ an instruction that copies data from an input device
into memory
⚫Output operation
⚫ an instruction that displays information stored in
memory to the output devices (such as the monitor
screen)
Input/Output Functions
⚫A C function that performs an input or output
operation
⚫A few functions that are pre-defined in the header file
stdio.h such as :
⚫ printf()
⚫ scanf()
⚫ getchar() & putchar()
The printf function
⚫Used to send data to the standard output (usually
the monitor) to be printed according to specific
format.
⚫General format:
⚫ printf(“string literal”);
⚫ A sequence of any number of characters surrounded by
double quotation marks.
⚫ printf(“format string”, variables);
⚫ Format string is a combination of text, conversion
specifier and scape sequence.
The printf function cont…
⚫Example:
⚫ printf(“Thank you”);
⚫ printf (“Total sum is: %d\n”, sum);
⚫ %d is a placeholder (conversion specifier)
⚫ marks the display position for a type integer variable

⚫ \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

⚫ 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 checking the algorithm and
written program before you actually type it
Algorithm
⚫ It is a complete step by step representation of the solution
of the problem, represented in English like language.
⚫ An algorithm can be quite abstract or quite detailed.

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

⚫ Very popular method to represent the steps of the solution.


⚫ Uses many graphical symbols and thus, is more understandable.
⚫ The symbols used for various different types of statements/steps.
Flow Chart
Start

⚫ Very popular method to represent the steps of the Read n1


solution. Read n2

⚫ Uses many graphical symbols and thus, is more


understandable.
⚫ The symbols used for various different types of Sum = n1 + n2

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

Diamond Decision If x>10 ?

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

4. Calculate Sum = n1 + n2.


5. Write the sum.
6. Stop.
Print Sum

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

You might also like