Chapter 5-Programming Fundamentals
Chapter 5-Programming Fundamentals
Chapter three: Fundamentals of programming C. The chapter also aims to familiarize the learners on how to
declare constants and variables in C language. We shall also
3.1 Chapter objectives
Therefore, by the end of this chapter, the learners should be discuss the various data types supported by C programming
able to: Language. Finally, we provide a summary for the chapter and
Differentiate between identifiers and keywords some self-testing exercise for the learners
Declare and use variables in C language to store 3.3 Identifiers
They are name given to various program elements such as
information
variables, constants, functions and arrays. Rules for naming
Understand the different types of variable scopes
identifiers
Understand different data types supported by C i). Can consists of letters and digits in any order
language
ii). The first character must be a letter or an underscore symbol.
Understand how and when to use comments in C
iii). Identifier names cannot have space
programming language.
iv). Both upper and lower case letters are permitted
Understand the different data types supported by C
programming language v). The only special character that can be used in identifier
definition is the underscore (_) and can be included often in
3.2 Introduction
This chapter aims at introducing learners to fundamental the middle of an identifier
concepts of C programming. The chapter will look at concepts
such as variables, constants as well as operators supported by
3.4 Key words program pretty easily. All characters available inside any
These are words that have a standard predefined meaning in C comment are ignored by C compiler. C multiple comments
programming language. E.g. main is a word that has a
start with /* and end with */. e.g.
predefined meaning in C language so no identifier can have
/* This is a comment */
such name
/* C comments can also span multiple lines */
3.5 Statements in C
A statement is meant to cause the computer to carry out some
For single comment, we use the double fowardslash (//). All
action. In C, all statements are usually terminated with a
statements preceded by the backslash shall be ignored by the
semicolon except for comments and control structure
compiler. e.g.
statements.
void main ()
3.6 Include <headerfile.h>
C has some predefined header files. Any program can include {
those files. The general format for include declaration is as
printf<< "welcome"; // prints welcome return 0;
follows
#include <headerfile.h> or #include “headerfile.h” }
wchar_t 2 or 4 bytes 1 wide character point, Boolean etc. Based on the data type of a variable, the
operating system allocates memory and decides what can be
stored in the reserved memory.
The sizes of variables might be different from those shown in
the above table, depending on the compiler and the computer 3.9.1 Variable Definition in C
you are using. When we declare a variable, we are telling the compiler where
and how much to create the storage for the variable. A variable
definition specifies a data type, and contains a list of one or 3.9.2.1 Local Variables
more variables of that type as shown in the following syntax: They are variables declared inside a function or block. They can
Data_type identifier [=value], [identifier=value]… [Identifiern=valuen]; be used only by statements that are inside that function or block
Here, data_type must be a valid C data type (char, wchar_t, int, of code. Local variables are not known to functions outside their
float, double, bool or any user-defined object like structure, etc., own. The following is the example uses local variables:
and identifier can be one or more identifier names separated by void main ()
commas.
{
Identifiers can be initialized (assigned an initial value) in their // Local variable declaration
declaration. The assignment operator (=) is always followed by
int a, b,sum;
a constant expression as follows:
// actual initialization
Data_type identifier_name = value;
a = 10; b = 20;
Some examples are:
sum = a + b;
E.g. float sum=0.0;
printf(“the sum is %d” ,sum);
3.9.2 Variable scope }
A variable scope means the extent to which a variable is 3.9.2.2 Global Variables
available in a program. There are three variable scopes namely:
They are variables defined on top of the program. The global A program can have same name for local and global variables
variables will hold their value throughout the life-time of your but value of local variable inside a function will take preference.
program. They can be accessed by any function i.e. it can be An example is presented below
available for use throughout your entire program after its int sum = 20;
declaration. A good example is presented below:
void main ()
Int sum;
{
void main ()
// Local variable declaration
{
int sum = 15;
// Local variable declaration:
printf(“the sum is%d”,sum);
int a, b;
return 0;
// actual initialization
}
a = 10; b = 20;
Program output: 15
sum = a + b;
3.9.2.3 Formal parameters
printf(“the sum is%d”,sum);
These are defined in a function
} Initializing Local and Global Variables
Anytime a local variable is defined, it is not automatically Constants can be treated just like regular variables only that
initialized by the system, you must initialize it yourself. However their values cannot be modified after their definition. There are
for global variables, they are initialized automatically by the different categories of constants
system when you define them as follows: 3.10.1 Integer literals/constants
Data type Initialize Integer constants are constant data elements that have no
Int 0 fractional parts or exponents. They always begin with a digit.
Char „\0‟ You can specify integer constants in decimal, octal, or
Float 0 hexadecimal form. They can specify signed or unsigned types
3.10.4 Character literals This format has the following general syntax
They are usually enclosed in single quotes. A character literal #define identifier value
% Modulus Operator and X% y will yield > Checks if the value of left (X>Y) is true
remainder of after an integer operand is greater than the
division value of right operand, if yes
++ Increment operator, increases X++ will yield 16 then condition becomes true.
integer value by one < Checks if the value of left (X<Y) is not true
-- Decrement operator, decreases Y++ will yield 9 operand is less than the value
integer value by one of right operand, if yes then
3.12.2 Relational/comparison Operators condition becomes true.
C programming language supports the following relational >= Checks if the value of left (X>=Y) is true
operators. Assume we have two variables; x holds 15 and y operand is greater than or equal
holds 10, then; to the value of right operand, if
Operator Description Example yes then condition becomes
operands are equal or not, if yes >= Checks if the value of left (X>=Y) is true
!= Checks if the values of two (X!=Y) is true operand is greater than or equal
10 | C H A P T E R T H R E E - B A S I C C O N C E P T S F O R P R O G R A M M I N G
MAT 224 -INTRODUCTION TO DATA PROCESSING AND COMPUTERS
<= Checks if the value of left (X<=Y) is not Operator Description Example
operand is less than or equal to true && Called Logical AND operator. If (X&&Y) is false
the value of right operand, if yes both the operands are non zero
then condition becomes true. i.e. then the condition becomes
There are following logical operators supported by C language. || Called Logical OR Operator. If (X||Y) is true
The result of logical operators can only be a Boolean value; true any of the two operands is
or false. Assume variable x holds 1 and variable y holds 0, then: nonzero, then condition
becomes true.
! Called Logical NOT Operator. !(X||Y) is false
Used to reverses the logical
state of its operand. If a condition
is true, then Logical NOT
operator will make false.
11 | C H A P T E R T H R E E - B A S I C C O N C E P T S F O R P R O G R A M M I N G
MAT 224 -INTRODUCTION TO DATA PROCESSING AND COMPUTERS
12 | C H A P T E R T H R E E - B A S I C C O N C E P T S F O R P R O G R A M M I N G
MAT 224 -INTRODUCTION TO DATA PROCESSING AND COMPUTERS
on the left hand side should be the same as the type returned “The area of the circle is %d\n”, is referred as control string
by the expression. while area is the variable to be outputted
13 | C H A P T E R T H R E E - B A S I C C O N C E P T S F O R P R O G R A M M I N G
MAT 224 -INTRODUCTION TO DATA PROCESSING AND COMPUTERS
%d is a conversion specifier indicating that the type of the e Floating point number Pointer to float
corresponding variable to be printed is integer f Floating point number Pointer to float
14 | C H A P T E R T H R E E - B A S I C C O N C E P T S F O R P R O G R A M M I N G