0% found this document useful (0 votes)
31 views13 pages

CSC214 Word

Uploaded by

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

CSC214 Word

Uploaded by

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

COURSE TITLE: C PROGRAMMING

LANGUAGE

COURSE CODE: CSC 214

1
 BRIEF HISTORY
C programming language was first introduced by Dennis Ritchie at the AT
&T’s Bell Laboratories (American Telephone & Telegraph) while developing
an operating system in USA in 1972 and was implemented for the first time
on DEC PDP-11 computer. Back then, all the systems used assembly
language which required pages of codes for even simple tasks.
The UNIX system which was built by both Dennis Ritchie and Ken
Thompson using assembly language. Later they changed the chip system of
UNIX from PDA-7 to PDP-11. At that time B (developed by Thompson
himself) was the most advanced programming language and they thought
of implementing it on UNIX but the compatibility issue of B with PDA-11 led
to the development of new high-level programming language which was
later named C as it was the successor and influenced from B.
C evolved from two previous programming languages BCPL (developed
by Martin Richards) and B (developed by Ken Thompson). Denise Ritchie
used the concept of BCPL and B to develop C and added data typing and
some other powerful features.
 FEATURES OF C LANGUAGE
 It is simple and easy to learn.
 It is efficient and fast.
 It is a structured/procedural programming language.
 It is machine independent/portable.
 It is a mid-level programming language.
 It has rich set of library i.e. inbuilt functions.
 It is extensible in nature. I.e. it can adopt new features.
 It is widely used in operating system and embedded system
development
 APPLICATIONS OF C PROGRAMMING
 It is used in creating computer applications.
 It is used in writing embedded software.
 It is used in creating Compilers, Assemblers and Interpreters
 It is used in creating operating system.
 It is used in creating Databases.
 It is used in creating Network Drivers and Text Editors.
 LOW LEVEL, MIDDLE LEVEL & HIGH LEVEL LANGUAGES
 LOW LEVEL LANGUAGES: They run directly on the processor.
They are designed to give better machine efficiency i.e. faster program
execution. Examples of low level languages are: Assembly languages
and machine languages.

2
 MIDDLE LEVEL LANGUAGES: They combine the characteristics
of both low level language and high level language. I.e. they sit
between low level and high level languages. Example: C language.
 HIGH LEVEL LANGUAGES: They evolved from middle level
languages. They are designed to give better programming efficiency
i.e. faster program development. They are also extremely dynamic in
nature (powerful, adapt to change). Examples: PASCAL, C++, PHP,
JAVA, PYTHON etc.
 LIST OF C PROGRAMMING COMPILERS
CodeBlocks, Dev C, Borland C, Turbo C, Microsoft Visual C, IBM C++,
Intel C++, Portable C compiler, Minimalist GNU for Windows (MinGW)
 STRUCTURE OF C PROGRAMMING
/* C program to print Helloworld */
#include <stdio.h>
int main ( )
{
printf("Hello World!");
return 0;
}
 /* - - - - - */ This represent comments in c programming. Any text
written between these symbols will be treated as a comment by the
compiler.
Comments do not affect program while running because C compiler
ignores it. Comments are used by programmers for the
documentation of the program.
 #include <stdio.h> This is the directive to the C preprocessor. This
line tells preprocessor to include the contents of standard
input/output header file (stdio.h) .or library functions in the program
while the program is being compiled.
 int main ( ) Main function is the point where the execution of any C
program begins.
 { This is the beginning of the main function.
 printf("Hello World!") This is the standard output functions which
instruct the computer to print anything written between the quotes
(” ..text..”) on the console.
 ; This represent a statement or line terminator.
 return 0 It indicates the termination of the function “main” returning
the integer value 0. The 0 value represent successful execution and 1
for unsuccessful execution.
 } This is the end of the main function.

3
 Token A token is the smallest unit in a C program. A token is divided
into six different types They are:
I. Keywords
II. Identifiers
III. Strings
IV. Constants
V. Operators
VI. Special characters: , . ; “ ! _ $ % { } [ ] ( ) & + - * / < > #
 C PROGRAMMING ERRORS
At the point of writing C programs, errors also known as bugs may occur
unwillingly which may prevent the program to compile and run
correctly. We have basically three (3) types of errors in c programming.
 Runtime Errors: They are errors that occur during the execution of
a C program. I.e. dividing a number by zero, finding the square
root of a negative number.
 Compile Time Errors: They are errors that occur at the point of
compilation of our programs. We have two (2) types of compile
time errors.
 Syntax Errors: They occur when the rules of c programming
language are not followed. i.e. int a,b: instead of int a,b;
 Semantic Errors: They are errors that occur when we write
programs that are not meaningful to the compiler. E.g
x + y = z; instead of z = x + y;
 Logical Errors: They are errors that produce undesired output or
incorrect output. They occur as a result of error in the logic
applied in the program.
 C PROGRAMMING KEYWORDS
Every word used in a C program is classified either as a Keyword or
an Identifier. These words have predefined meaning in c language and
meaning can’t be changed as well. These keywords/identifiers cannot be
used as variable names. C programming has 32 keywords. They are: Int,
char, float, double, signed, unsigned, void, short, long, break,
continue, const, case, auto, enum, if ,else, for, do, while, static, void,
default, register, typedef, sizeof, goto, volatile, union, extern, return,
struct

 C PROGRAMMING VARIABLES AND CONSTANTS

4
 VARIABLES: A variable is a location in the computer’s memory
where value is stored that our programs can manipulate. A variable
is a value that can change at any point in time during the execution
of a program. i.e.
Int age = 20;

Datatype variable name value


 TYPES OF VARIABLES
 Local variable
 Global variable
 Static variable
 Automatic variable
 External variable

 RULES FOR NAMING VARIABLE NAMES


 First letter of a variable must be alphabet (underscore is also
allowed).
 Variable can only contain letters, digits and underscores.
 Maximum length of identifier allowed is 31 characters.
 White space is not allowed.
 Keywords cannot be used as variable.

 CONSTANTS
Constants are values that do not change throughout the execution
of a Program. i.e.
Const float pi = 3.142;

Data type constant name constant value


 Constant types are:
 Integer constant.
 Real/floating point constant.
 String constant.
 Character constant.
 Declared constant.
 Enumerated constant
 PRINTF( ), SCANF( ) AND GETS( ) FUNCTION IN C
PROGRAMMING
 printf( ) function
The printf( ) function is used for output. It prints the given
statement
to the console.
5
The syntax of printf( ) function:
Printf("string: format specifier", name of variable/constant);

 scanf( ) function
The scanf( ) function is used for input. It reads the input data from
the console.
The syntax of scanf( ) function
scanf("format specifier", address of variable);
 DATA TYPES IN C PROGRAMMING
Data types specifies the type of values a variable can hold/store and how
this value can be manipulated in the memory. C dataypes includes:
 Fundamental data types:
I. Int: It is used to store integer value only. No decimal points
are allowed. It takes 2 bytes of memory. Range -32768 to
32767
II. Char: It is used to store single character only. It occupies 1
byte of memory. Range -128 to 127
III. Float: It is used to store floating point numbers. It occupies
4 byte of memory. Range 3.4e-38 to 3.4e38
IV. Double: It is used to declare long float in C programming.
 Derived data types: function, arrays, pointers.
 User-defined data types: struct, union, enumeration.
 Format specifier: Format specifier is used for data types of variables.

Format Data Type


specifier
%d int
%c char
%f Float
%lf Double

 C PROGRAMMING OPERATORS
Operators are symbols that tell the compiler to perform certain
mathematical or logical manipulation/operation.
 C programming operators
 Arithmetic operator

6
+ Addition, - Subtraction, * Multiplication, / Division,
% Modulus Division
 Relational operator
< Less than, <= Less than or equal to, > Greater than, >=
greater than or equal to, = = Equal to, != Not equal to
 Logical operator
&& (Logical AND): If both results are true, then the
condition becomes true, else false.
|| (Logical OR): If any of the two results is true, then the
condition becomes true.
! (Logical NOT): If a condition is true, then Logical NOT
will make it false.
 Assignment operator =
 Increment/Decrement operator
++ Increment operator
-- Decrement operator
 Bitwise operator ( &, |, ^, <<, >> )
 C PROGRAMMING FLOW CONTROL
 C PROGRAMMING IF STATEMENT
It’s a one-way flow control in which the statements will only
execute if the given expression is true. It's used to execute some
statement code block if the expression is evaluated to true.
Otherwise, it will get skipped. The syntax of if statement is:
If (test_expression)
{
Block of code to be executed;
}

 C PROGRAMMING IF-ELSE STATEMENT


It's used to execute some statement code block if the expression is
evaluated to true, otherwise executes else statement code block.
The syntax of if-else statement is:
If (test_expression)
{
Block of code to be executed;
}
else
{
Block of code to be executed;
}
7
 C PROGRAMMING IF…ELSE IF…ELSE
If..else if…else statement is used to select a particular block of
code from several block of codes. It’s a way of using conditional
statements inside another conditional statement. The syntax of
If..else if…else statement is:
If (test_expression1)
{
Block of code to be executed;
}
else if (test_expression2)
{
Block of code to be executed;
}
else
{
Block of code to be executed;
}

 C LOOP
The process of executing a collection of statement repeatedly is
called looping. C loop execute a block of commands/statements a
specified number of times until a condition is met. There are three
(3) types of loops in C language.
I. For Loop
II. While Loop
III. Do-While Loop
(I) FOR LOOP
It allows a block of code/statement to be executed repeatedly
unless or until some sort of condition is satisfied.
Syntax of For Loop
For (initialization; condition; increment/decrement)
{
Statement(s)
}
(II) WHILE LOOP
The while loop repeats the block of code/statement until certain
condition is satisfied. While loop has one control condition, and
executes as long the condition is true. The condition of the loop is
tested before the body of the loop is executed.

8
Syntax of While Loop
While (condition)
{
Statement(s)
Increment;
}
(III) DO…WHILE LOOP
In do…while loop, the code inside the loop will be executed and
then the condition is checked. I.e. It executes the block of
statement at least once as long as the condition remains true.
Syntax of Do…While Loop
do
{
Statement(s)
}
while (condition);
 BREAK AND CONTINUE STATEMENTS IN C
PROGRAMMING
 The Break statement is used to exit the loop instantly. When a
break statement is encountered inside a loop, the control directly
comes out of the loop and the loop gets terminated.
It is used with If statement and Switch-case statement.
while (test_condition)
{
statement1;
if (condition )
break;
}
 The continue statement when encountered inside a loop, doesn’t
terminate the loop, rather interrupts a particular iteration. It skips
the remaining statements in the body of that loop and performs the
next iteration of the loop.
while (test_condition)
{
statement1;
if (condition )
continue;
}
 ARRAYS IN C PROGRAMMING
An Array is a group/collection of same data types/items. They are used to
store similar type of elements i.e. the data type must be the same for all
9
elements. For example, an int, float, double, char array holds the elements
of int, float, double, char types. Hence, it is convenient to store same data
types in a single variable and later access them using array index.
 Types of Arrays in C programming
 One-Dimensional Array
 Two-Dimensional Array
(I) One-Dimensional Array
One-dimensional array or 1-D array is an array of one dimension.
It has one subscript. Subscripts starts with 0. i.e. [0], [1]… [n]
The syntax of one-dimensional array in C:
Data type arrayname [arraysize]
If we want to store the score a student obtained in mathematics in WAEC
at 6 different sittings, we don't need to define different variables for the
score in each sitting. Instead, we can define an array which can store the
scores in each sitting at the contiguous memory locations. For example
int score [6]
int: data type, score: array name and 6: array size
 Initializing one-dimensional Array in C Programming
Arrays can be initialized at declaration time. We can also initialize
each element of the array by using the index.
The syntax of one-dimensional array initialization
Data type array name [array size] = { list items };
For example: int score [6] = {60,53,,58,69,62,70};
int: data type
score: array name
6: array size
{60, 53, 58, 69, 62, 70 }: list items
The above declaration will create an array like this
60 53 58 69 62 70
score[0] score[1] score[2] score[3] score[4] score[5]
char alphabet[6] = {‘a’,’b’,’c’,’d’,’e’,’f’};
datatype array name array size list items
The number of elements inside the curly brackets { } must not be larger
than the number of elements specified inside the square brackets [ ]
while declaring arrays.
 Accessing elements of one-dimensional Array in C
programming
In one-dimensional array in C, elements are accessed by specifying
the array name and the index value within the square brackets.

10
Array indexing starts from 0 and ends with size -1. If we try to
access array elements out of the range, the compiler will not show
any error message rather it will return some garbage value.
The syntax of accessing one-dimensional array is:
arrayname [index];
(II) Two-Dimensional Array
Two-dimensional array or 2D array is an array of two dimension.
It has two subscripts. The first subscript represents row and the
second subscript represents column. The two dimensional (2D) array
in C programming is also known as matrix. A matrix can be
represented as a table of rows and columns.
The syntax of two-dimensional array in C:
Datatype arrayname [row size][column size];
 Initializing two-dimensional Array in C Programming
There are two ways to initialize a two Dimensional arrays during
declaration. i.e.
int age [2][3] = { {12,35,18},{19,22,4} }
The above declaration will create an array like this
Column0 Column1 Column2
Row0 12[0][0] 35[0][1] 18[0][2]
Row1 19[1][0] 22[1][1] 40[1][2]
 Accessing elements of two-dimensional array in C
programming
In two-dimensional array in C, elements are accessed by specifying
the array name, row index value and column index value within
the square brackets. The syntax of accessing two dimensional array
is: arrayname [row index][column index].
 STRINGS IN C PROGRAMMING
A string is a collection of characters (i.e. letters, numerals, symbols, and
punctuation marks) in a linear sequence. In C, a string is a sequence of
characters concluded with a NULL character ‘\0’. For example
char str [ ] = “hello\0”;
like many other programming languages, strings in C are enclosed within
double quotes (“ ”), whereas characters are enclosed within single
quotes (‘ ’). When the compiler finds a sequence of characters enclosed
within the double quotation marks, it adds a null character (\0) at the end
by default.

11
For example, the above string will be stored like this
h e l l o \0

 Declaring a String in C Programming


A string in C is an array with character as a data type. C does not
support string as a data type. Hence, character arrays must be used
to display a string in C. The syntax of declaring a string in C is:
char variablename [ array size];
 Initializing a String in C Programming
There are four methods of initializing a string in C:
(i) Assigning a string with size
We can directly assign a string to a character array keeping
in mind the array size at least one more than the length of
the string that will be assigned. For example
char name [7] = “london”;
(ii) Assigning a string without size
We can directly assign a string to a character array without
any size. The size gets determined automatically by the
compiler at compile time.
char name [ ] = “london”;
(iii) Assigning character by character with size
We can also assign a string character by character with size.
However, it is essential to set the end character as ‘\0’. For
example, char name [7] = {‘l’, ‘o’, ‘n’, ‘d’, ‘o’, ‘n’, ‘\0’};
(iv) Assigning character by character without size
We can also assign a string character by character without
size. It is also essential to set the end character as ‘\0’. For
example, char name [ ] = {‘l’, ‘o’, ‘n’, ‘d’, ‘o’, ‘n’, ‘\0’};
 Read String from the User
The most common method of reading string is using scanf ( )
function, which reads a sequence of character until it encounters i.e
space, newline, tab etc. for example. char place [20];
scanf(“%s”, &place);
If we enter Abuja is beautiful, the output will be Abuja. scanf( )
function stops taking input once it encounters space.
 How to Read a Line of Text
To read and print strings with whitespace, we can use the
combination of fgets( ) and puts( ) function.

12
(i) fgets( ) function: It is used to read a specified number of
characters. The syntax is;
fgets(stringname, sizeof(stringname), stdin);

13

You might also like