Structured Programming
CSE-1101
Rafid Mostafiz
IIT, NSTU
Basic Stucture(C Language)
2
Basic Stucture
3
First C Program
4
/* My first simple C program */
#include<stdio.h>
int main ()
{
printf (“Welcome to C!\n”);
return 0;
}
Some Key Terms
5
Source Program
printable/Readable Program file
Object Program
nonprintable machine readable file
Executable Program
nonprintable executable code
Syntax errors
reported by the compiler
Linker errors
it means that your code compiles fine, but that some function
or library that is needed cannot be found.
Execution/Run-time errors
reported by the operating system
Integrated Development Environments
6
An integrated development environment (IDE) is
a software package that makes it possible to edit,
compile, link, execute, and debug a program
without leaving the environment.
Example: CodeBlocks
The General Form of a Simple Program
7
Simple C programs have the form
directives
int main(void)
{
statements
}
The General Form of a Simple Program
8
C uses { and } in much the same way that some
other languages use words like begin and end.
Even the simplest C programs rely on three key
language features:
Directives
Functions
Statements
Directives
9
Before a C program is compiled, it is first edited by
a preprocessor.
Commands intended for the preprocessor are called
directives.
Example:
#include <stdio.h>
<stdio.h> is a header containing information
about C’s standard I/O library.
Directives
10
Directives always begin with a # character.
By default, directives are one line long; there’s no
semicolon or other special marker at the end.
Functions
11
A function is a series of statements that have been
grouped together and given a name.
Library functions are provided as part of the C
implementation.
A function that computes a value uses a return
statement to specify what value it “returns”:
return x + 1;
The main Function
12
The main function is mandatory.
main is special: it gets called automatically when
the program is executed.
main returns a status code; the value 0 indicates
normal program termination.
If there’s no return statement at the end of the
main function, many compilers will produce a
warning message.
Statements
13
A statement is a command to be executed when the
program runs.
first.c uses only two kinds of statements. One
is the return statement; the other is the print
statement.
Asking a function to perform its assigned task is
known as calling the function.
first.c calls printf to display a string:
printf(“Welcome to C!");
Statements
14
C requires that each statement end with a
semicolon.
There’s one exception: the compound statement.
Directives are normally one line long, and they
don’t end with a semicolon.
Printing Strings
15
When the printf function displays a string
literal—characters enclosed in double quotation
marks—it doesn’t show the quotation marks.
To make printf advance one line, include \n
(the new-line character) in the string to be printed.
Printing Strings
16
The statement
printf(“Hello World\n");
could be replaced by two calls of printf:
printf(“Hello ");
printf(“World\n");
The new-line character can appear more than once in a
string literal:
printf(“Hello\nWOrld\n");
17
Any Question?