Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

Structured Programming

CSE1201
Course Teacher: Jannatul Ferdous Ruma
2

Structure of C Program
A C program basically consists of the following parts:
• Preprocessor Commands

• Functions

• Variables

• Statements & Expressions

• Comments
Structure of C Program
Preprocessor
Commands
Main Function

Comment

Library
Function End of
Program
Preprocessor Commands

 These commands tells the compiler to do preprocessing before


doing actual compilation.
 Like #include <stdio.h> is a preprocessor command which
tells a C compiler to include stdio.h file before going to actual
compilation.

4
5

Functions
• Functions are main building blocks of any C Program.
• Every C Program will have one or more functions and there is one
mandatory function which is called main() function.

• The C Programming language provides a set of built-in functions.


• In the above example printf() is a C built-in function which is used to
print anything on the screen.
Comments

• Comments are used to give additional useful information


inside a C Program.
• All the comments will be put inside /*...*/ as given in the
example above.
• A comment can span through multiple lines.
/* comment for multiple line */

// single line comment


6
Variable and Variable Declaration
• Variable is a named memory location that can hold various values.
• All variables must be declared before they can be used.
• When we declare a variable, we tell the compiler what type of
variable is being used.
• A declaration associates a group of variables with a specific data type.

7
Structure of C Program
Preprocessor
Commands
Main Function

Comment

Library
Function End of
Program

8
C’s Basic Data Type
Type Keyword format Memory
Specifier Requirements
Character data char %c 1 Byte
Signed whole numbers int %d 2 or 4 Byte
Floating-point numbers float %f 4 Byte
Double-precision floating- double %lf 8 Byte
point number
valueless void ---

9
How to Declare Variables
• To declare a variable, use this general form:
type var-name;
• In C, a variable declaration is a statement and it must end in
a semicolon (;).

10
Variable
• Variables consist of letters and digits, in any order, except that the
first character must be a letter.
• Both upper-and lowercase letters are permitted, though common
usage favors the use of lowercase letters for most types of variables.
• Upper- and lowercase letters are not interchangeable (i.e., an
uppercase letter is not equivalent to the corresponding lowercase
letter.)

11
Variable (cont.)
• The underscore character (_) can also be included, and is considered
to be a letter.
• An underscore is often used in the middle of an variable.
• A variable may also begin with an underscore, though this is rarely
done in practice.

12
Variable (cont.)
• Case-sensitive
• COUNT and count are not same

• As a rule, an identifier should contain enough characters so that its


meaning is readily apparent.
• On the other hand, an excessive number of characters should be
avoided.

13
Variable (Cont.)
Can not Use blank space

Can use letter of


Can not Use any keyword
alphabet (A-Z, a-z)

The first character must


Variable Digits (0-9)
be a letter

Can not start a variable


name with digit Underscore (_)
14
Is it Valid Variable Name?
Apon 1joty

joty-5
apon

apon123 this_is_a_long_name

_sojeb_1 VaReNdRa
15
Is it Valid Variable Name?
4th The first character must be letter

“x” Illegal characters (“)

Order-no Illegal characters (-)

My variable Illegal characters (blank space)


16
Identifiers
• Identifiers are names that are given to various program elements,
such as variables, functions and arrays.

17
Keywords
• There are certain reserved words, called Keywords, that have
standard, predefined meaning in C

• Can be used only for their intended purpose

• Can't use as identifiers

18
Keywords

19
Expressions
• An expression is a combination of operators and operands.
• C expressions follow the rule of algebra

Expression Operator
Arithmetic Expression +, -, *, /, %
Logical Expression AND, OR, NOT
Relational ==, !=, <, >, <=, >=

20
Assign value to variable

• To assign a value to a variable, put its name to the left of an equal sign (=).

• Put the variable you want to give the variable to the right of the equal sign.

• It is a statement, so end with a ‘;’

variable value
;
21
22
Input Numbers From Keyboard
• There are several methods
• The easiest is – scanf() function.

• Input from Keyboard

23
24
Escape Sequences
• There are certain characters in C when they are preceded by a
backslash (\) they will have special meaning.
• They are used to represent like newline (\n) or tab (\t).

25
Defining Constants
• There are two simple ways in C to define constants:
• Using #define preprocessor.
• Using const keyword.

26
Using #define preprocessor
• Following is the form to use #define preprocessor to define a
constant:

• Symbolic constants are usually defined at the beginning of a program.

• Name of the constant • Constant value


• Can be any valid identifier name • Can be character/numeric/string

27
Using #define preprocessor

28
Symbolic Constants

29
30
Variables Types
• There are two types of variables in c program they are,
• Local variable
• Global variable
Global Variables
• These variables are declared /*
/* Compute
Compute Area
circle
circle */*/
Area and
and Perimeter
Perimeter of
of aa

outside all functions. #include <stdio.h>


#include <stdio.h>
float
float pi
pi == 3.14159;
3.14159; /* /* Global
Global */
*/
• Life time of a global variable is int
int main()
main() {{
the entire execution period of float
float area;
int rad
area; /*
= 5;
/* Local
Local */
*/
int rad = 5;
the program. area
area == pi
pi ** rad
printf("%f",
rad ** rad;
area);
rad;
printf("%f", area);
• Can be accessed by any function. return
return 0;
0;
}}
Local Variables
• These variables are declared /*
/* Compute
Compute Area
circle */
circle */
Area and
and Perimeter
Perimeter of
of aa

inside some functions. #include


#include <stdio.h>
<stdio.h>

• Life time of a local variable is the int


int main()
main() {{
float
float pi
pi == 3.14159;
3.14159;
entire execution period of the float
float area; /*
int rad
area;
= 5;
/* Local
Local */
*/
int rad = 5;
function in which it is defined. area
area == pi
pi ** rad
printf("%f",
rad ** rad;
area);
rad;
printf("%f", area);
• Cannot be accessed by any other return
return 0;
0;
function. }}

• In general variables declared


inside a block are accessible only
in that block.
SCOPE OF VARIABLE : GLOBAL VARIABLE

• The scope of global variables will be throughout the program. These


variables can be accessed from anywhere in the program.
• This variable is defined outside the main function. So that, this
variable is visible to main function and all other sub functions.
SCOPE OF VARIABLE : GLOBAL VARIABLE
#include<stdio.h>
void test();
int m = 22, n = 44;
int a = 50, b = 80;
Output
int main() From main function
{ values : m = 22 : n = 44 : a = 50 : b = 80
printf("From main function"); From test function
printf("\nvalues: m=%d:n=%d:a=%d:b=%d", values : m = 22 : n = 44 : a = 50 : b = 80
m,n,a,b);
test();
}

void test()
{
printf("\n\nFrom test function");
printf("\nvalues: m=%d:n=%d:a=%d:b=%d",
m,n,a,b);
}
Type Conversion

• A type cast is basically a conversion from one type to another. There are two types
of type conversion:
• Implicit Type Conversion Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without programmers intervention.
• All the data types of the variables are upgraded to the data type of the variable
with largest data type
Type Conversion (Example)
#include<stdio.h> #include <stdio.h>
int main() main ()
{ {
int x = 10; // integer x int a;
char y = 'a'; // character c a = 15/6;
x = x + y; // ‘a’ ascii is 97 printf("%d",a);
float z = x + 1.0; }
printf("x = %d, z = %f", x, z);
return 0;
}

Output: Output:
x = 107, z = 108.000000 2
Implicit Type Conversion
i (int) = 7, f (float)= 5.5, c (char) = ‘w’.

Expression Value Type


i+f 12.5 Float
i+c 126 Integer
i + c – ‘0’ 78 Integer
(i + c) – (2 * f / 123.8 Float
5)
Implicit Type Conversion
● An operation between char and int will result in int.
● An operation between float and double will result in double.
● An operation between float and long double will result in long double.
● An operation between float and char or int will result in float
Explicit Type Conversion
● This process is also called type casting and it is user defined.
● Here the user can type cast the result to make it of a particular data type.
#include<stdio.h>

int main()
{
double x = 1.2;

int sum = (int)x + 1; // Explicit conversion from double to int

printf("sum = %d", sum);


return 0;
}
Output:
sum = 2
EXPRESSION
 In programming, an expression is any legal combination of symbols that represents a
value.
 C Programming Provides its own rules of Expression, whether it is legal expression
or illegal expression. For example, in the C language x+5 is a legal expression.
 Every expression consists of at least one operand and can have one or more
operators.
 Operands are values and Operators are symbols that represent particular actions.
Types of Expression

Type Explanation Example


Infix Operand1 Operator Operand2 a>b
Prefix Operator Operand1 Operand2 >ab
Postfix Operand1 Operand2 Operator ab>
STATEMENT
A statement causes the computer to carry out some action.
o Types of Statement:
There are three types of statement in C.

1. Expression Statement: The simplest kind of statement in C is an expression (followed by a


semicolon, the terminator for all simple statements).
Examples: a=3; c= a+b; ++i; printf(“area= %f”, area); etc
STATEMENT
2. Compound Statement: A compound statement consists of several individual statements enclosed
within a pair of braces { }.
Examples:
if(radius>0)
{
float pi = 3.1416;
circumference = 2 * pi * radius;
Area = pi * radius * radius;
}
STATEMENT
3. Control Statement: A control statement are used to create special program features, such as
logical tests, loops, and branches.
Examples:
while(count<= n )
{
printf(“x=“);
scanf(“%f”, &x);
sum += x;
++count;
}
EXPRESSION AND STATEMENT
Expression Statement
a+b a = 3;
x=y x = y;
c=a+b c = a + b;
x<=y printf (“%d”, n);
x == y ; // NULL Statement
++i ++i;

You might also like