0% found this document useful (0 votes)
9 views49 pages

Structure of C Programming

Analytical thinking, good communication skills, and creative thinking are important skills for programmers. Programmers must also be open-minded to listen to others' input and solve problems. The general structure of a C program includes preprocessor directives, functions, variables, data types, statements, comments, and opening and closing braces to delimit blocks of code. Standard library functions like printf() and scanf() are commonly used.

Uploaded by

Olamide Kole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views49 pages

Structure of C Programming

Analytical thinking, good communication skills, and creative thinking are important skills for programmers. Programmers must also be open-minded to listen to others' input and solve problems. The general structure of a C program includes preprocessor directives, functions, variables, data types, statements, comments, and opening and closing braces to delimit blocks of code. Standard library functions like printf() and scanf() are commonly used.

Uploaded by

Olamide Kole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

SKILLS TO CRAVE FOR

1
SKILLS PROGRAMMERS MUST CRAVE FOR

• ANALYTICAL THINKING SKILL:-


–Being able to properly analyze information is the most
important aspect of critical thinking. When researching
a project, analytical thinking helps you to separate the
information that applies to your situation from that
which does not. It also helps in the process of gathering
information, interpreting it, and in skeptically
evaluating data.
SKILLS PROGRAMMERS MUST CRAVE FOR

• GOOD COMMUNICATION SKILL:-

– Good communication is crucial in the critical thinking


process. Proper communication with stakeholders will
help you gather the information you need to solve the
identified problem and for convincing others that your
conclusions are correct.
SKILLS PROGRAMMERS MUST CRAVE FOR

• CREATIVE THINKING SKILL:

– This has to do with being able to discover certain


patterns of information and making abstract
connections between unrelated data. Creative thinking
improves critical thinking. When analyzing a
procedure or process, you can creatively come up with
ways to make the procedure or process faster and more
efficient. Creativity is a skill that can be strengthened
over time and is very valuable.
SKILLS PROGRAMMERS MUST CRAVE FOR

• OPEN-MINDEDNESS.

– Previous education and life experiences leave their


mark on a person’s ability to objectively evaluate
certain situations. By acknowledging these biases,
you can improve your critical thinking and overall
decision process.
– If you find yourself in a team, be willing to listen to
team members, hear them out and adjust your
approach based on their input.
SKILLS PROGRAMMERS MUST CRAVE FOR

• PROBLEM SOLVING SKILL.


– The ability to correctly analyze a problem and
implement its solution is another very useful and
valuable skill.
INTRODUCTION TO PROGRAMMING

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

S/NO. FUNCTION DESCRIPTION


1. printf() Used for displaying output on the screen
2. scanf() Used for taking input from the user.
3. getchar() Used for returning characters on the screen.
4. putchar() Used for displaying output as a single character on the screen.
5. fgets() Used for taking a line as an input.
6. puts() Used for displaying a line as an output.
7. isalpha() Used for checking if the character is an alphabet or not.
8. isdigit() Used for checking if the character is a digit or not.
9. isalnum() Used for checking if the character is alphanumeric or not.

12
STANDARD LIBRARY FUNCTIONS IN C

S/NO. FUNCTION DESCRIPTION


10. isupper() Used for checking if the character is in uppercase or not
11. islower() Used for checking if the character is in lowercase or not.
12. toupper() Used for converting character to uppercase.
13. tolower() Used for converting the character into lowercase.
14. isprint() Used for checking if the character is a printable character or not
15. fopen() Used for opening a file.
16. fclose() Used for closing a file.

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:-

interprets, using its rules of precedence and


association; and computes, to produce another
value, called the output. Expressions either
assigns values to variables, or, work directly
with values. An expression can be any of: Infix,
Prefix or Postfix.

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.

– Prefix:- If it is an expression in which the


operator is written before the operands.

– Postfix:- If it is an expression in which the


operator is written after the operands.

18
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Statements and Expressions Cont’d:-

Every expression consists of at least one


operand and can have one or more operators.
Operands are either values or variables that
have values assigned to them. Operators are
symbols that represent particular actions. We
will discuss more about operators later in lesson
4 practical manual.

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.

• A variable, is the most simple form of storage in


memory for items of data, that can be altered at runtime.
Every variable has a data type and a name that uniquely
identifies it. Variables must be declared and initialized
before use in programs where they are needed.

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.

• A Constant, is the most simple form of storage in


memory that stores an item of data, that cannot be
altered at runtime. Every constant has a data type and a
name that uniquely identifies it. Constants must be
declared and initialized before use, in programs where
they are needed. It is recommended that identifiers for
constants should always be written in capital letters.
27
GENERAL FORM OR
STRUCTURE OF C PROGRAMS
– Arrays and Functions:-
• These where also mentioned in the structure provided.
They will be introduced later in this manual, but treated
in more detail in CSC202.

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

– The size of character to be stored, will determine if the data


type to be used will be any of: char, unsigned char, signed
char, etc.

– A very good programmer knows how to conserve memory


space by making use of the best data types to store values,
so as to avoid redundant spaces.

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

1. short int 2 -32,768 to 32,767 %hd

2. unsigned short int 2 0 to 65,535 %hu

3. int 4 -2147483648 to 2147483647 %d

4. unsigned int 4 0 to 4,294,967,295 %u

-2,147,483,648 to
5. long int 8 2,147,483,647 %ld

6. unsigned long int 8 0 to 4,294,967,295 %lu

7. long long int 8 -(2^63) to (2^63)-1 %lld

33
BASIC DATA TYPES
MEMOR FORMAT
Y SPECIFIER OR
S/NO. DATA TYPE (BYTES) RANGE OF NUMBERS PLACEHOLDER

unsigned long long 0 to


8. int 8 18,446,744,073,709,551,615 %llu

9. signed char 1 -128 to 127 %c

10. unsigned char 1 0 to 255 %c


1.2E-38 to 3.4E+38 (6
11. float 4 decimal places) %f
2.3E-308 to 1.7E+308 (15
12. double 8 decimal places) %lf
3.4E-4932 to 1.1E+4932
13. long double 16 (19 decimal places) %Lf

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

• Best programming practices involves five key


things, namely:

• The use of proper identifiers


• The use of comments
• The use of white spaces and
• The use of indentation
• The use of escape sequences

37
BEST PROGRAMMING PRACTICES.

• USE OF PROPER IDENTIFIERS

Identifier is basically a user defined name that uniquely


identifies any of the following entities: constant,
variable, array, function, etc. Giving proper identifier
names, makes a program, self-documenting because, the
name will tell what it does or what information it stores.

Examples: studentid, productid, nationid, stateid, lgaid,


wharehouseid, patientid, patientname, staffid, etc.
38
BEST PROGRAMMING PRACTICES.

• TO CREATE PROPER IDENTIFIERS


– Frequently make use of language guidelines
– Do not avoid giving long names. This will
maintain clarity
– Use uppercase and lowercase letters
– Avoid giving same name to more than one
identifier

39
BEST PROGRAMMING PRACTICES.
• USE OF COMMENTS

– Comments can be inserted as −

• Opening to a program to explain its objective


• At the beginning and/or end of logical or
functional blocks
• Notes, about special scenarios or exceptions

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.

– Comments and indentations are ignore by the


compiler but they aid those who read through the
codes.

41
BEST PROGRAMMING PRACTICES.

• USE OF WHITE SPACES

– White space is any portion of a program that is left


unused. It is created by inserting blank spaces or blank
lines in a program, so as to improve readability and
make programs look less crowded.
– White space, when used effectively, help programmers
to separate instructions. It plays a great role in helping
to quickly locate bugs or whatever is of interest in the
program.
42
BEST PROGRAMMING PRACTICES.

• USE OF WHITE SPACES


– Whitespace character is the space character which can
either be in form of multiple spaces or blank lines.

• Listed below, are 6 places in a program where white


spaces can be used. They are:

– As blank lines after preprocessor directives,


– After new variables are declared,
– Immediately after comments,
43
WHERE WHITE SPACES CAN BEST BE
USED IN YOUR PROGRAM
• 6 places in a program where white spaces can be used
– Cont’d.:
– Between logical or functional blocks of code in
programs,
– As blank spaces around operators and
– Other places in a program where a programmer
feels they are required

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.

• USE OF ESCAPE SEQUENCES


– In C Programming, escape sequences are non-
printing characters that are associated with
backslash, giving special meaning to the escaped
character.

– Escape sequences are mostly used within the block


of statement of a printf() function.

– There are 12 escape sequences in C Programming


and they are all listed in the table on the next page.
47
USE OF ESCAPE SEQUENCES
IN C PROGRAMS
ESCAPE SPECIAL
S/NO. SEQUENCE MEANING DESCRIPTION
1 \n New Line Statement following the escape sequence, \n,
will be printed on a New Line. An example is
"Good\nMorning". Here, n is the character
which escapes from the sequences of word,
GoodMorning.
2 \t Horizontal tab Statement followed by \t will be printed after 8
spaces.
3 \b Backspace \b will backspace a character.
4 \r Carriage Return \r will replace the existing characters by its
defined characters
5 \a Alert Bell \a will audio you a bell sound
6 \' Single quote \' will print single quote '.

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

You might also like