Structure of C Programming
Structure of C Programming
1
SKILLS PROGRAMMERS MUST CRAVE FOR
• OPEN-MINDEDNESS.
7
PARTS OF A C PROGRAM
• Preprocessor Declaratives
• Functions
• Variables/Identifiers
• Data Types and Placeholders
• Statements & Expressions
• Comments
• White Spaces
• Opening and Closing Braces
• Semicolon or Statement Terminator
• Escape Sequence(s)
8
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
preprocessor directives
global declarations; // Variables declared here are accessible from anywhere in this program
main() /* Note the use of bracket set (). They are used in conjunction with function
names*/
{ // Open left Curly brace for function main
/* Also, note the use of braces {}. They are used to delimit C statements associated with
functions */
Declaration of local variables to function main ;
// Variables declared here are accessible only within this function
statements associated with function main ; // All statements in C must end with a semicolon
} // Closing right Curly brace for function main
f1() // f1 represents a function
{ // Open left Curly brace for function f1
Declaration of local variables to function 1 ;
// Variables declared here are accessible only within this function
statements associated with function 1 ;
} // Closing right Curly brace for function f1
f2() // f2 represents another function
{ // Open left Curly brace for function f2
Declaration of local variables to function f2 ;
// Variables declared here are accessible only within this function
statements associated with function 2 ;
// Semi Colon is used to terminate C statements
} // Closing right Curly brace for function f2 9
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
• The whole essence of discussing the structure of C
programs, is for us to be familiar with the statements
or instructions used in C programs and know where
and how they are used or placed. Examples include:
– Preprocessor Declaratives :- In C programs, you
will find preprocessor declaratives at the very top
of the program. They help us access the standard
library functions by including certain header files.
Syntax is: #include <header file> e.g. #include
<stdio.h>.
10
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Standard Library Functions are basically the inbuilt
functions in the C compiler that makes it possible
for programmers to make use of available pre-
existing codes without having to rewrite them.
– On the next page, is a tabular representation of a
list of some standard library functions in C,
followed by some header files associated with
them.
11
STANDARD LIBRARY FUNCTIONS IN C
12
STANDARD LIBRARY FUNCTIONS IN C
13
HEADER FILES ASSOCIATED WITH
STANDARD LIBRARY FUNCTIONS IN C
S/NO. HEADER DESCRIPTION
FILE
1. <stdio.h> Standard input-output header associated with Input/Output
Functions such as scanf(), printf(), getchar(), putchar(), fgets(),
puts(), fopen() and fclose()
2. <stdlib.h> Standard library header associated with General Utility Functions
such as malloc(), calloc(), realloc() and free().
3. <string.h> String Header associated with String Functions such as strlen() and
strcpy().
4. <conio.h> Console input-output header associated with Console input/output
Functions such as clrscr() and getch()
5. <math.h> Math header associated with Mathematics Functions such as sqrt(),
pow(), fabs(), log(), sin(), cos()and tan().
14
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Functions :- In every C program, you must have
at least one function called main function which is
also a standard library function and the entry point
to every C Program. It is always written using the
syntax, functiontype functionname() e.g.
int main(). Functions will be introduced later in
this manual but discussed much more in CSC202.
15
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Statements and Expressions :-
• A statement is a syntactic unit of a C program,
that expresses actions to be carried out.
Statements are basically instructions to the
compiler.
• An Expression in a programming language is a
syntactic entity that may be evaluated to
determine its value. It is also a combination of
one or more Variables, Constants and Operators,
that the programming language
16
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Statements and Expressions Cont’d:-
17
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Statements and Expressions Cont’d:-
– Infix:- If it is an expression in which the
operator is in between the operands.
18
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Statements and Expressions Cont’d:-
19
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Opening and Closing Curly Braces ({, }) :-
• In C programs, opening and closing curly braces
are mainly used around a function body, around
the body of repetition control structures (while,
for or do/while loops), around the body of
decision control statements (if, else if or else),
around selection control statements (switch
case), to show blocks of instructions or
statements.
20
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Opening and Closing Curly Braces ({, }) :-
• What this simply communicates is that anytime
you have to write programs that make use of
functions or control structures, your block of
codes must be enclosed within curly braces.
• Braces that are correctly used, are referred to as
balanced braces.
• Braces that are not correctly used, are referred to
as unbalanced braces.
21
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Semicolon or Statement Terminator:-
• In C programs, a Statement Terminator is used to
indicate the end of a program statement. It is also used
between the expressions of the for loop statement.
– Other Terms Used:-
• Other terms you will encounter from time to time in a C
Program include: Declarations (global and local),
Identifiers, Variables, Constants, Data Types and
Placeholders. These will be discussed briefly, below.
22
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Declarations:-
• In C programs, using declarations requires the
knowledge of C-Scope Rules.
• A scope in any program, is a region where an
identifier can be accessed.
• A declaration is simply a language construct
that specifies properties of an identifier, its data
type and sometimes, its dimensions.
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Declarations:-
• Identifiers are basically user defined names that
uniquely identify Constants, Variables, Arrays,
Functions, Structures or Unions.
• In C programming, identifiers can be declared as
global, local, formal arguments or as formal
parameters.
• It is global if it is declared outside all functions, at
the top of your program. Global identifiers are
accessible from any point in the program.
24
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Declarations:-
• It is local if it is declared within a function or a
block of statements or instructions in your
program.
• It is formal arguments if it is declared in a
function call.
• It is formal parameters if it is declared in the
definition of a function.
25
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Variables:-
• In most C programs, the use of variables is useful when
space in memory is required for values either at runtime
or for future use.
26
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Constants:-
• In C programs, Constants are useful when space in
memory is required for values that cannot be altered at
runtime.
28
DATA TYPES IN C
• Data types, are keywords and declarations that
communicate the type and size of data that requires
memory space for storage or manipulation. Every
variable/constant/array/function/structure/union in C,
must have an associated data type that determines the
type of values it can hold and size of memory
required. Every data type requires a placeholder.
• There are three different data types in C, namely,
Basic Data Type, Derived Data Type and User-
Defined Data Type.
29
DATA TYPES IN C
• BASIC DATA TYPE:-
– We have four different basic data types, namely:
Integer Type, Float Type, Character Type and Void
Type.
– The range of whole numbers to store, determines if
the data type to be used will be any of: int,
unsigned int, short int, unsigned short int, long int,
unsigned long int, long long int, unsigned long
long int, etc.
30
DATA TYPES IN C
• BASIC DATA TYPE:-
– The range of decimal numbers to store, determines if the
data type to be used will be any of: float, double, long
double, etc
31
DATA TYPES IN C
• DERIVED DATA TYPE
– These are data types that are derived from basic
data types. There are three types of derived data
types, they are: functions, arrays and pointers.
• USER-DEFINED DATA TYPE
– User-defined data types are data types that are
created by users in relation to their requirements
using available resources. There are three types of
user-defined data types, they are: Structure,
Union and Enumeration.
32
BASIC DATA TYPES IN C
MEMOR FORMAT
Y SPECIFIER OR
S/NO. DATA TYPE (BYTES) RANGE OF NUMBERS PLACEHOLDER
-2,147,483,648 to
5. long int 8 2,147,483,647 %ld
33
BASIC DATA TYPES
MEMOR FORMAT
Y SPECIFIER OR
S/NO. DATA TYPE (BYTES) RANGE OF NUMBERS PLACEHOLDER
34
PLACEHOLDERS IN C
• Another term for placeholders is format specifiers.
• Format specifiers are used in C for input and output
purposes to help the compiler understand what type
of data is in a variable when accepting input using the
scanf() function and printing using printf() function.
• They are always preceded by a ‘%’ sign. Examples
are shown in some example programs in this manual.
35
For all your CSC102 related issues,
please contact:
Mrs Benedicta Olatokunbo
in COLNAS Software Laboratory.
36
BEST PROGRAMMING PRACTICES
37
BEST PROGRAMMING PRACTICES.
39
BEST PROGRAMMING PRACTICES.
• USE OF COMMENTS
40
BEST PROGRAMMING PRACTICES.
• USE OF COMMENTS
– Avoid all lengthy comments that may prove
counter productive. They may end up breaking the
flow of code and affect smooth reading.
41
BEST PROGRAMMING PRACTICES.
44
BEST PROGRAMMING PRACTICES.
• USE OF INDENTATION
– In programming languages, an indent is a tab or
distance of text from left or right margin, of
instruction or program statement.
– In programs, indentation is used to separate logically
separated blocks of code, to communicate program
structure.
– An indented program is more easily readable,
understood, modified, maintained and enhanced.
– Indentation is very useful when writing control
structure codes.
45
BEST PROGRAMMING PRACTICES.
• USE OF INDENTATION
• Indentation can be used in a program when a programmer
wants to do the following:
– Shows levels of nesting
– Make anyone reading through the codes tell what is executing in it.
– Show scope
– Make it easier to read through the codes
– Make good use of best practices
46
BEST PROGRAMMING PRACTICES.
48
USE OF ESCAPE SEQUENCES
IN C PROGRAMS
ESCAPE SPECIAL
S/NO. SEQUENCE MEANING DESCRIPTION
7 \" Double quote \" will print double quote "
8 \? Question \? will print? (Question mark).
9 \\ Backslash \\ will print a single backslash (\).
10 \f Form feed \f will print female sign (♀)
11 \v Vertical tab \v will print male sign (♂).
12 \0 Null The statement followed by \0 will not display.
49