Chapter-4
C
Programming
4.1 Introduction:
4.1.1
Overview, History, Features, Advantages and Disadvantages of C
Language.
4.1.2
Structure of C program.
4.1.3
Compiling Process.
4.1.4
C Preprocessor and Header Files
4.2 Fundamentals of C
4.2.1
Character Set used in C
4.2.2
Use of Comments
4.2.3
Identifiers, Keywords and Tokens.
4.2.4
Data Types in C
4.2.5
Constants and Variables
4.2.6
Type of Specifier
4.2.7
Statements – Simple and Compound Statements
4.3 Operators and Expressions
4.3.1
Operators: Precedence & Associativity
4.3.2
Expressions
4.3.3
Type Casting and Conversions
4.3.4
Introduction to Library Functions
4.4 Input/ Output (I/O) Functions
4.5 Control Structures
4.5.1
Decisions (if, if-else, else if, switch, ?: operator)
4.5.2
Looping (while, do while, for)
4.6 Array and String
4.6.1
Definition of array and string
4.6.2
Types of Array – One-Dimensional and Two-Dimensional (definition,
declaration, and initialization.)
4.6.3
String Function : strlen(),
strcat(), strcmp(), strrev(), strcpy(), strlwr() , strupr()
4.7 Functions
4.7.1
Concept of Function, function definition, function prototype.
4.7.2
Return and Void statements of a function
4.7.3
Accessing a Function – Function Call(by value, by reference)
4.7.4
Concept of Recursion
4.8 Structures and Unions
4.8.1
Definition and Difference between Structure and Union.
4.8.2
Structure: Declaration, Initialization and Size of Structure.
4.9 Pointers
4.9.1
Definition of Pointer
4.9.2
Address (&) and indirection (*) operator
4.9.3
Pointer Expression and Assignment
4.10
Working with Files
4.11
Concept of Data File
4.12
Sequential and Random File
4.13 Opening, Reading, Writing and
Appending on/from Data File
|
Overview
of C language
C is a general-purpose, structured,
procedural, compiled, high-level programming language that was originally
developed by Dennis M. Ritchie at AT&T’s Bell Lab for use with the UNIX
operating system by inheriting the features of B and BPCL with his own
features. C was originally first implemented on the DEC PDP-11 computer in
1972. It is suitable for preparing system software as well as application
software. The file extension of c program is .c.
C is also called middle (intermediate
language) level language because it combines the best features of high level
language and low level language.
A Brief History of C Language
The C
programming language was developed by Dennis Ritchie at Bell Labs during the
early 1970's. Quite unpredictably it derived from a computer language named B
and from an earlier language BCPL. Initially designed as a system programming
language under UNIX it, expanded to have wide usage on many different systems.
The earlier versions of C became known as K&R C after the authors of an
earlier book, "The C Programming Language" by Kernighan and Ritchie. C
was standardized by ANSI (American National Standard Institute) in 1980. It is
still used for some system and network programming as well as for embedded
systems.
Features and Importance of C:
a)
It is
highly portable language: - it means C programs written for one
computer can be run on another computer without little or no modifications.
b) It is structured language: -
because it has a fixed structure.
c)
It is
efficient and fast: -
it is efficient and fast due to its verities of data types and powerful
operators.
d) It can
handle low-level activities
e)
It can be compiled on a variety of computer
platforms
f)
It contains a powerful instructions set of
data operators that tell computer how to manipulate the data within a program.
g) It has
both the features of high level language as well as low level language.
h) It has rich system library: -
because it has large numbers of predefined keyword and library functions.
Advantages of C
a)
It is one of the efficient and fast
executing programming languages.
b) It is
easy for debugging, testing and maintaining.
c)
There is no limitation while programming
using C. We can develop any kinds of program.
d) C is
powerful language and the use of pointer has made it unique.
e)
Its compiler is easily available.
f)
Length of programs can be reduced by using
function.
g) Reusability
of function increases.
Disadvantages of C
a)
There is no runtime checking.
b) There
is no strict type checking.
c)
It is very difficult to fix the bugs.
d) It is
not powerful like as object oriented programming.
e)
Pointer is one of the unique features which
have made C powerful but if it is mishandled the system may crash so it is
risky too.
Basic structure of C program
Here, we can easily illustrated each
component of a basic structure of C program as below
·
Documentation
section: -
it contains a set of comments lines about the name of program, the author,
algorithm, methods used and other detail. E.g. // WAP to find sum of two
number.
·
Link
Section: -it
provides instruction to the compiler to link functions with program from the
system library. E.g. #include<stdio.h>, #include<conio.h> etc.
·
Definition
section: - in
this section, all symbolic constants are defined. E.g. #define pi 3.147
·
Global
declaration section:
- In this section, declared variable as global which are used in more than one
functions.
·
Main ()
function section:
- Every C program starts and ends with a main () function. It contains
declaration and executables parts where declaration part declares all the
variables used in the execution part. E.g. int a=2, b=5;
a=a+2;
b=b+8;
·
Subprogram
section: -This
section contains all the user-defined functions that are called in the main
function.
Documentation
Section
|
|||||
Link
Section
|
|||||
Definition
Section
|
|||||
Global
Declaration Section
|
|||||
Main()
function section
{
}
|
|||||
Subprogram
Section
User-defined
functions
|
Fig. Basic structure of a C
program
Example
// WAP to calculate the area of circle
using function. :
Documentation section
#include<stdio.h> : link section
#include<conio.h>
#define
pi 3.147 :
definition section
float
r; :global declaration section
float
area(float x);
void
main() : starting from here to main function
{ float value; :declaration part
clrscr();
printf("Enter
a radius of circle\t");
scanf("%f",&r);
value=area(r); :executable part and also calling user defined function
printf("\nArea
of cirlce is: %f",value);
getch();
}
float
area(float x) :user defined function
return(pi*x*x);
}
Compiling process
Header files and C preprocessor
Compilation refers to the processing
of source code files (.c, .cc, or .cpp) and the creation of an 'object' file.
This step doesn't create anything the user can actually run. Instead, the
compiler merely produces the machine language instructions that correspond to
the source code file that was compiled. In c
programming compiling process consists of following steps:
v Preprocessing:
-
They instruct compiler to do required pre-processing before actual compilation.
You can call this phase text substitution or interpreting special preprocessor
directives denoted by #.
v Compiler – the compiler translates
the high-level instructions in the .i file into low-level Assembly language
instructions. The generated file containing the translation is in text format
and typically has a .s file extension.
v Assembling – the
assembler converts the Assembly language text instructions in the .s file into
machine code. The generated object file containing the conversion is in binary
format and typically has a .o file extension.
v Linking –Linking refers to the creation of
a single executable file from multiple object files. If
these piece of code need some other source file to be linked then linker link
them to make it a executable file.
v Loader: - It
loads the executable code into memory. Program and data stack are created,
register get initialized.
Header files and C preprocessor
Header Files:
Header files are a collection
of macros, types, functions and constants. Any program that needs those
functions can include the corresponding header files.
List of
some commonly used Header file and their purposes:
Header Files
|
Purpose
|
Functions Declared
|
stdio.h
|
Used for standard input and
output (I/O) operations.
|
printf(), scanf(), getchar(),
putchar(), gets(), puts(), getc(), putc(), fopen, fclose(), feof()
|
conio.h
|
Contains declaration for
console I/O functions.
|
clrscr(), exit(), getch()
|
ctype.h
|
Used for character-handling
or testing characters.
|
isupper(), is lower,
isalpha()
|
math.h
|
Declares mathematical
functions and macros.
|
pow(), squr(), cos(), tan(),
sin(), log()
|
stdlib.h
|
Used for number conversions,
storage allocations.
|
rand(), srand()
|
string.h
|
Used for manipulating
strings.
|
strlen(), strcpy(), strcmp(),
strcat(), strlwr(), strupr(), strrev()
|
C
Preprocessor:
The C Preprocessor is
not a part of the compiler, but is a separate step in the compilation process.
C Preprocessor is just a text substitution tool and it instructs the compiler
to do require pre-processing before the actual compilation. All preprocessor
commands begin with a hash symbol (#).
Preprocessor directive is a
collection of special statement that is executed at the beginning of a
compilation process. It is placed in the source program before the main
function. They all begin with the symbol # (hash) and do not require a
semicolon at the end.
#define directive: it
defines a text substitution, macro substitution and symbolic constant.
Syntax:
#define identifier substitution_token
For Example:
#define TRUE 1
#define FALSE
#define MAX 100
#define PI 3.14
#include
directive: -This
directive searches for a header or source file, which can be processed by the
implementation, to be include in the program.
Syntax:
#include
<filename>
For
example: #include<stdio.h>
Labels: C programming
0 Comments:
Post a Comment
thank you
Subscribe to Post Comments [Atom]
<< Home