You are on page 1of 14

Programming Concepts Using C Language

Subject Code: TBC 101


UNIT-1
Bachelor of Computer Applications(BCA)

[1] Program Design Methodologies


A. Procedural:
It is a traditional program design approach that defines the flow of data and control in
a program. This approach is also called as structural programming paradigm that uses
procedures, subroutines or modules that contains computation methods.
B. Event-driven
In this the flow of the program is defined by the events. These events may occur due
to mouse click, key press etc. This approach is generally used for more interactive
Graphical User Interface based applications.
C. Data-driven
In this, the flow of a program is controlled by data itself but not on the logic. e.g.
reading the data, processing it and producing the output.
D. Object-oriented
This approach is based on real-time objects that contain data as properties and
methods as codes. Programs are designed with the help of classes and objects.
E. Logical
This paradigm works on logical units rather than functions. The complex problems are
divided into smaller logical units by using one of the following designs:
 Top-down design: In this a big program is broken down into smaller units, which
may be further broken down into even smaller units. Each unit is called a module.
Each module itself is a complete program

 Bottom-up design: In bottom-up approach, system design starts with the lowest
level of components, which are then interconnected to get higher level components.
This process continues till, we get the complete program.


Modular approach
Modular programming (also referred to as modular architecture) is a general
programming concept. It involves separating a program’s functions into independent
pieces or building blocks, each containing all the parts needed to execute a single
aspect of the functionality. Together, the modules make up the executable application
program.
[2] History of C
C was developed by Dennis Ritchie on a DEC PDP-11 machine that used the Unix
operating system. C is the advanced version of an older language called BCPL. BCPL was
developed by Martin Richards, and it influenced a language called B, which was invented
by Ken Thompson. B led to the development of C in the 1970s.
For many years, C was supplied with the Unix operating system. It was first described in
The C Programming Language by Brian Kernighan and Dennis Ritchie. In the summer of
1983 a committee was established to create an ANSI (American National Standards
Institute) standard that would define the C language. The standardization process took six
years (much longer than anyone reasonably expected).
The ANSI C standard was finally adopted in December 1989, with the first copies
becoming available in early 1990. The standard was also adopted by ISO (International
Standards Organization), and the resulting standard was called as ANSI/ISO Standard C. In
1995, Amendment 1 to the C standard was adopted, which added several new library
functions. The 1989 standard for C, along with Amendment 1, became a base document for
Standard C++. The version of C defined by the 1989 standard is called as C89.
However, work on C continued quietly along. The end result was the 1999 standard for C,
usually referred to as C99. The C99 standardization committee focused on two main areas:
the addition of several numeric libraries and the development of some special-use, but
highly innovative, new features, such as variable-length arrays and the restrict pointer
qualifier. This is the version of C in widest use, it is currently accepted by all C compilers,
and it forms the basis for C++.

[3] Importance of C
C programming is a general-purpose, procedural programming language developed at
AT&T Bell Labs by Dennis Ritchie in the early 1970s. A successor of the B programming
language, it was developed to overcome the challenges of BASIC, B, and BPCL
programming languages.
Despite the emergence of numerous new languages like Java, Python, JavaScript, and PHP,
the popularity of C is not hampered. In fact, these languages have borrowed many of their
basic features from C.

a) Simple and Efficient


The basic syntax C language is very simple and easy to learn. This enables a programmer to
redesign or create a new application.
b) Fast
C is a compiler-based program. This makes the compilation and execution of codes faster.
Newer programming languages come with numerous features, which increase functionality
but reduce efficiency and speed.
c) Function-Rich Libraries
C comes with an extensive set of libraries with several built-in functions. User can also
create his own (user-defined) functions and add them to C libraries.
d) Dynamic Memory Management
One of the most significant features of C language is its support for dynamic memory
management (DMA). It means that you can utilize and manage the size of the data structure
in C during runtime. C also provides several predefined functions to work with memory
allocation. For instance, free(), malloc(), calloc(), and realloc().
e) Mid-Level Programming Language
C supports the features and functionalities of high-level and low level programming
languages. C allows direct manipulation of hardware, which high-level programming
languages do not offer.
f) Pointers
With the use of pointers in C, you can directly interact with memory. As the name suggests,
pointers point to a specific location in the memory and interact directly with it. Using the C
pointers, you can operate with memory, arrays, functions, and structures.
g) Portable
C is highly portable, that is, ‘C’ programs written on one computer can be run on another
with little (or) no modification.

[4] Structure of C program


A C program is divided into different sections. There are six main sections to a basic c
program. The six sections are,

 Documentation
 Link
 Definition
 Global Declarations
 Main functions
 Subprograms
Figure: Basic Structure Of C Program

a) Documentation Section
The documentation section is the part of the program where the programmer gives the details
associated with the program. He usually gives the name of the program, the details of the
author and other details like the time of coding and description. It gives anyone reading the
code the overview of the code.

Example

/*
File Name: Helloworld.c
date: 03/10/2022
description: a program to display hello world no input needed
*/

b) Link Section (pre-processor directives)


This part of the code is used to declare all the header files that will be used in the program.
This leads to the compiler being told to link the header files to the system libraries.

Example

#include<stdio.h>

c) Definition Section
In this section, we define different constants. The keyword define is used in this part.

#define PI=3.14
d) Global Declaration Section
This part of the code is the part where the global variables are declared. All the global variable
used are declared in this part. The user-defined functions are also declared in this part of the
code.

float area(float r);


int a=7;

e) Main Function Section


Every C-programs needs to have the main function. It starts with the open curly bracket and
ends with the curly close bracket. It contains 2 parts. A declaration part and an Execution
part. The declaration part is the part where all the variables are declared. The execution part
shows the work of main function. Both the declaration and execution part are inside the curly
braces.

int main(void)
{
int a=10;
printf(" %d", a);
return 0;
}

f) Sub Program Section


All the user-defined functions are defined in this section of the program.

int add(int a, int b)


{
return a+b;
}

[5] Data Types


A data type defines a set of values that a variable can store along with a set of operations
that can be performed on that variable. Common data types are integer, character, and
floating-point. Although C has several builtin data types, it is not a strongly typed language,
as are Pascal and Ada. C provides several different types of data, each of which occupies
different memory. The basic data types are listed below. Note that the memory
requirements for each data type may vary from one C compiler to another.). The basic data
types can be enlarged by the use of the data type qualifiers short , long, signed and
unsigned. For example, integer quantities can be defined as short int , long int or unsigned
int.

Type Storage Size Value range


char 1 byte -128 to 127 or 0 to 255
Unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or
-2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
Unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
Unsigned long 4 bytes 0 to 4,294,967,295

Floating-Point Types
The following table provides the details of standard floating-point types with storage sizes
and value ranges and their precision:
Type Storage Value range Precision
size
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

The void Type


The void type specifies that no value is available. It is used in three kinds of situations:
S.N. Types and Description
1 Function returns as void
There are various functions in C which do not return any value or you can
say they return void. A function with no return value has the return type as
void. For example, void exit (int status);
2 Function arguments as void
There are various functions in C which do not accept any parameter. A
function with no parameter can accept a void. For example, int rand(void);
3 Pointers to void
A pointer of type void * represents the address of an object, but not its type.
For example, a memory allocation function void *malloc(size_t size);
returns a pointer to void which can be casted to any data type.
6. VARIABLES

[6] Variables
A variable is a name given to a storage area in main memory. Each variable has a data
type, which determines its size in memory; the range of values; and the set of operations
that can be applied to the variable. C supports following basic variable types −
Type Description
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.

A variable definition tells the compiler where and how much storage to create for the
variable. The syntax of variable definition is given below−
type variable_list;
Here, type is a valid C data type such as char, int, float, double, bool, or any user-defined
data type and variable_list is identifier name. Some valid declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The array is another kind of variable that is used in C. An array is a collection of same data
type elements.
Rules for Constructing Variable Names
A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some
compilers allow variable names whose length could be up to 247 characters.
 The first character in the variable name must be an alphabet or underscore.
 No commas or blanks are allowed within a variable name.
 No special symbol other than an underscore (as in gross_sal) can be used in a
variable name.
 It should be a keyword.
 Uppercase and lowercase symbols are different.

[7] Constants
A constant is an entity that doesn’t change whereas a variable is an entity that may change.
Constants can be of any of the basic data types.
There are four basic types of constants in C:
 integer constants
 floating-point constants
 character constants
 string constants.
a) Integer Constants
An integer constant is a sequence integer digits. It can be written in three number systems:
decimal, octal and hexadecimal. Several valid decimal integer constants are shown below.
0 1 743 5280 32767 9999
The following decimal integer constants are written incorrectly for the reasons stated.
12,245  illegal character (, ).
36.0  illegal character (.).
10 20 30  illegal character (blank space).
123-45-6789  illegal character (-).
0900  the first digit cannot be a zero.

An octal integer constant is made by digits 01234567. However the first digit must be 0.
EXAMPLE 0 01 0743 077777
The following octal integer constants are written incorrectly for the reasons stated.
743  Does not begin with 0.
05280  Illegal digit (8).
0777.777  Illegal character ( .).
A hexadecimal integer constant must begin with either 0x or 0X. It can consists any
combination of digits from the sets 0 through 9 and a through f.
EXAMPLE 0x 0X1 0X7FFF 0xabcd

b) Floating-Point Constants
A floating-point constant is a base-10 number that contains either a decimal point or an
exponent (or both).
EXAMPLE: Several valid floating-point constants are shown below.
0. 1. 0.2 827.602
5000. 0.00074 12.3 31 5.0066
2E-8 0.006e-3 1.6667E+8 .12121212e12

The following are not valid floating-point constants for the reasons stated.
1  Either a decimal point or an exponent must be present.
1,000.0  Illegal character (, ).
2E+10.2  The exponent must be an integer quantity (it cannot contain a
decimal point).
3E 10  Illegal character (blank space) in the exponent.

c) Character Constants
Character constants are enclosed between single quotes. For example, 'a' and '%' are
both character constants. C defines both multibyte characters, which consist of one or
more bytes, and wide characters (which are usually 16 bits long).
To specify a multibyte character, enclose the characters within single quotes, for
example, 'xy'. To specify a wide character constant, precede the character with an L.
For example:
wchar_t wc;
wc = L'A';
Here, wc is assigned the wide-character constant equivalent of A. The type of wide
characters is wchar_t, which is defined in the <stddef.h> header file, and is not a built-
in type.
d) String Constants
A string constant consists of sequence of characters, enclosed in (double) quotation
marks.EXAMPLE "green India” , "270-32-3456"
The compiler automatically places a null character (\0) at the end of every string
constant. Remember that a character constant (e.g., ‘A’ ) and the single-character
string constant ("A") are not equivalent. Also remember that a character constant has
an equivalent integer value, whereas a single-character string constant does not have
an equivalent integer value.

[8] Declaration of variables


To create a variable, specify the type and assign it a value:
Syntax  type variableName = value;
Ex-Create a variable called Num of type int and assign the value 15 to it:
int Num = 15;
You can also declare a variable without assigning the value, and assign the value later:
int Num;
Num = 15;
Note: If you assign a new value to an existing variable, it will overwrite the previous value:
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10

[9] Modifiers
Modifiers are the keywords to modify the default properties of int and char data types.
There are two types of type modifiers:
1. Size modifiers – short, long
2. Sign modifiers – signed, unsigned
They can be further sub-divided into these four data type modifiers :
1. long / long long
2. short
3. signed
4. unsigned
The modifier short and long can be applied to int type to get types short int and long int.
The modifier long can be applied to double to get the long double which stores extended
precision floating point number. In fact long is the only type modifier that can be applied
to double.
The modifiers signed and unsigned can be applied to char and integer types. When the
modifiers unsigned is used the number is always positive, and when signed is used number
may be positive or negative. If the sign modifier is not mentioned in integers, then by
default signed modifier is assumed. If the sign modifier is not mentioned for char type, then
whether the char type is signed or unsigned is machine dependent. The range of values for
signed data types is less than that of unsigned type. This is because in signed type, the
leftmost bit is used to represent the sign, while in unsigned type this bit is also used to
represent the value.
1. short It allows user to store small integer values from -32768 to 32767.
short int a = 18;
2. long It allows user to stores very large number (something like 9 Million Trillion)
from -9223372036854775808 to 9223372036854775807. Syntax “long long” is used
instead of “long int”.
long long a = 827337203685421584;
3. signed It is default modifier of int and char data type if no modifier is specified. It
says that user can store negative and positive values.
signed int a = -544;
signed int b = 544;
/* Both of the statements have same meaning even without "signed" modifier*/
4. unsigned When user intends to store only positive values in the given data type (int
and char).
unsigned int a = 486;

[10] Identifier
identifiers are names given to various program elements, such as variables, functions and
arrays. Identifiers consist of letters and digits, except that the first character must be a
letter. Both upper- and lowercase letters are permitted and are treated differently.
The underscore character ( - ) can also be included, and is considered to be a letter. An
underscore is often used in the middle of an identifier. An identifier may also begin with an
underscore.
EXAMPLE The following names are valid identifiers.
X, Y12, sum_1, _temperature, names, area, tax_rate, TABLE.
The following names are not valid identifiers for the reasons stated.
4th The first character must be a letter.
“x” Illegal characters (“).
order-no Illegal character (-).
Tax rate Illegal character (blank space).
An identifier can be arbitrarily long. Some compilers accept only the first 8 characters, and
other accept typically, 31 characters.
[11] Keywords
Keywords are the words whose meaning is fix to the C compiler. The keywords cannot be
used as variable names because if we do so we are trying to assign a new meaning to the
keyword, which is not allowed by the computer.
The standard keywords are
auto extern sizeof break
float static case for
Struct char goto switch
const if Typedef continue
int union default long
unsigned do register void
double return Volatile else
short while enum signed
Some compilers may also include some or all of the following keywords.
ada far Near asm
fortran pascal entry huge

[12] EXPRESSIONS
An expression represents a single data item, such as a number or a character. The expression
may consist of a constant, a variable, an array element or a reference to a function. It may
also consist of some combination of such entities, interconnected by one or more operators.
Expressions can also represent logical conditions that are either true or false.
EXAMPLE a + b, x = y, c = a + b, x <= y

[13] STATEMENTS
When c compiler found a statement it performs some action. There are three types of
statements in C. They are expression statements, compound statements and control
statements.
An expression statement consists of an expression followed by a semicolon. EXAMPLE
a = 3;, c = a + b ; ++i;
printf ("Area = %f area) ;
A compound statement consists of several individual statements enclosed within a pair of
braces { }. The individual statements may themselves be expression statements, compound
statements or control statements. Unlike an expression statement, a compound statement
does not end with a semicolon.
EXAMPLE
{
p i = 3.141593;
circumference = 2. * p i * radius;
area = p i * radius * radius;
}
Control statements are used to perform logical decision. Many control statements require
other statements within them example.
EXAMPLE 2.30 The following control statement creates a conditional loop in which
several actions are executed repeatedly, until some particular condition is satisfied.
while (count <= n) {
printf ( “%d”, x ) ;
++count;
}

[14] SYMBOLIC CONSTANTS


A symbolic constant is a name that substitutes for a sequence of characters. The characters
may be a numeric constant, a character constant or a string constant. Thus, a symbolic
constant allows a name to appear in place of a numeric constant, a character constant or a
string. When a program is compiled, each occurrence of a symbolic constant is replaced by
its corresponding character sequence. Symbolic constants are usually defined at the
beginning of a program.
A symbolic constant is defined by writing
#define name text
where name represents a symbolic name, typically written in uppercase letters, and text
represents the sequence of characters that is associated with the symbolic name.
EXAMPLE
#define TAXRATE 0.23
#define PI 3.141593
Now suppose that the program contains the statement
area = PI * radius * radius;
During the compilation process, each occurrence of a symbolic constant will be replaced by
its corresponding text. Thus,the above statement will become
area = 3.141593 * radius * radius;

You might also like