Unit-Iv: Mathematics, Biology and Computers For Chemists
Unit-Iv: Mathematics, Biology and Computers For Chemists
Unit-Iv: Mathematics, Biology and Computers For Chemists
CHEMISTS
UNIT-IV
3) LANGUAGES:
FLOW CHART
OPERATING SYSTEMS:
***************
HISTORY OF C
Documentation Section
The Documentation section consists of a set of comment lines
giving the name of the program, the author and other details,
which the programmer would like to use later.
Link Section
Definition Section
There are some variables that are used in more than one function.
Such variables are called Global variables and are declared in the
global declaration section that is out side of all the functions.
Subprogram Section
This sub program section contains all the user defined functions
that are called in the main function. User-defined functions are
generally placed after the main function, although they may
appear in any order. All sections except the main function may be
absent when they are not required.
2) char s [20];
scanf (“%s”,s)
This form of scanf reads a string until a new line char is
encounter.
printf( ) function: Output data can be return from the computer
and a standard output device using the library function printf ().
This function can be used to output any combination of numeric
value, single char and strings.
- - argn);
Where control string refers to a string that contains formatting
information and arg1, arg2 - - - argn are arguments that
represent individual output data items.
Eg : 1) printf ("%d %f", a,b)';
2) printf ("\n J.B.PG College");
3) printf ("sum of %d and %d is %d",a,b,c);
The arguments should match in number, order and types with the
format specification
Where variable name is a valid ‘C’ name that has been declared as
"char" type.
When this statement is encountered, the computer waits until a
key is pressed and then assigns this char as a value to getchar ( ).
Since getchar ( ) is used the right hand side of an assignment
statement and the character value of getchar ( ) is assigned to the
variable name on the left.
putchar function
The function putchar ( ) is used for writing characters one at a
time to the terminal.
Syntax: putchar (variable-name);
CHARACTER SET
The characters that can be used to form words numbers and
expressions depend upon on the computer on which the program
is run. The characters in C are grouped as follows.
1) Letters: - A to Z (Upper case) and a to z (Lower case)
2) Digits:-All decimal digits 0 to 9
3) Special characters: - , comma, . Period, ; semicolon, :
colon, ? Question mark, ‘apostrophe, “ quotation mark, ),!
Exclamation mark, | vertical bar, / slash, \ back slash, ~ tilde, _
under score, $ dollar sign, % percent sign, & ampersand, ^ caret,
* asterisk, - minus sign, + plus sign, < opening angle bracket (or
less than sign > closing angle bracket (or greater than sign), (left
parenthesis,) right parenthesis, [ left bracket, ] right bracket,
{ left brace, } right brace, # number sign.
4) Blank spaces (white spaces): - Blank spaces, Horizontal tab,
Carriage return, new line, form feed.
C TOKENS
In a C program the “smallest individual units” are known as C
tokens. C has six types of tokens.
KEYWORDS:
IDENTIFIERS:
CONSTANTS:
VARIABLES
A variable is a data name that may be used to store data value. A
variable may take different values at different times during
execution. Variable names may consist of letters, digits, and the
underscore ( _ ) character.
Rules:
1. They must begin with a letter. Some systems permit
underscore as the first character.
2. ANSI standard recognizes a length of 31 Characters.
3. Uppercase and lowercase are significant. That is, the
variable Total is not the same as total or TOTAL.
4. It should not be a keyword.
5. White space is not allowed.
Ex: marks sum distance value
DECLARATION OF VARIABLES
int and double are the keywords to represent integer type and
real type data values respectively.
Assignment Statement:
DATA TYPES
C language is rich in its data types. Every variable in ‘C’ has a data
type. Data types specify the size and type of values that can be
stored.
Integer types:
Integer types are for numbers with out fractional part. Negative
values are also allowed. C has three classes of integer storage.
They are short int, int and long int, in both signed and unsigned
forms. The size and range of these types are shown in the
following table:
Integer types are for numbers with fractional part. Floating point
numbers are stored in 32 bits, with 6 digits precision. Floating
point numbers are defined by C by the keyword float. When the
accuracy provided by a float number is not sufficient, the type
double can be used to define the number. A double data type
number uses 64 bits giving a precision of 14 digits. These are
known as double precision numbers. To extend the precision
further, we may use long double which uses 80 bits.
Character Type:
Void Types:
The void type has no values. This is usually used to specify the
type of functions. The type of a function is said to be void when it
does not return any value to the calling function.
Arrays:
Functions:
Pointers:
This tells the compiler three things about the variable pt_name.
1. The asterisk ( * ) tells that the variable pt_name
2. pt_name needs a memory location.
3. pt_name points to a variable of type data_type.
Example: int *p;
Declares the variable ‘p’ as a pointer variable that points to an
integer data type.
Structures:
Unions:
Enumerated :
Typedef :
C supports a feature known as “type definition” that allows users
to define an identifier that would represent an existing data type.
The user-defined data type identifier can later be used to declare
variables.
Here, units symbolizes int and marks symbolizes float. They can
be later used to declare variable as follows:
Batch1 and batch2 are declared as int variable and m1, m2, m3
are declared as floating point variables.
Examples:
If (age >55 && salary < 10000)
If (number<0 || number > 100)
SYMBOLIC CONSTANTS
CONTROL STATEMENTS
CONTROL
*********
ARRAYS
An Array is a group of related data items of the same data
type addressed by a common name, all the items are stored in
physically adjacent (contiguous) memory locations. The
individual elements of an array are accessed and manipulated
using the array name followed by their index. The index starts
from 0.
Example:
int marks [10];
FUNCTIONS
FUNCTION: A Function is a self contained block of code that
performs a particular task. Once a function has been designed and
packed, it can be treated a block box that takes some data from
the main program and returns a value. The inner details of
operation are invisible to the rest of the program. All that the
programs knows about a function is: what goes in and what
comes out. Every ‘C’ program can be designed using a collection
of these boxes.
Advantages of Functions:
1. It facilitates top down modular programming. The high level
logic of the overall program is solved first while the details
of each lower level functions are addressed later.
2. The length of a source program can be reduced by using
functions at appropriate places.
3. It is easy to locate and isolate a faulty function for further
investigation.
4. A function may be used by many other programs.
5.
PARTS OF FUNCTION:
A function has the following parts:
1. Function prototype declaration.
2. Definition of a function (function declarator)
3. Function call
4. Actual and Formal Arguments
5. The return statement.
TYPES OF FUNCTIONS:
In ‘C’ functions can be divided into two types:
1. Library Functions
2. User-defined Functions
LOCAL VARIABLES:
Local variables are the variables defined with in a function whose
scope, visibility is limited to that particular function. Local
variables belongs to one function cannot be accessed in to
another function. The local variables belongs to different
functions may have the same name.
GLOBAL VARIABLES:
Global variables are the variables whose scope and visibility is
along the program. These variables can be accessed into any sub
function. Whenever both the global, local variables have the same
name then the scope of the global variable is overridden by the
local variable.
BITWISE OPERATORS
There are three logical bitwise operators.
1. Bitwise Logical Operators
2. Bitwise Shift Operators
3. One’s Complement Operators.
1. BITWISE LOGICAL OPERATORS: There are three logical Bitwise
operators. They are
Bitwise AND (&)
Bitwise OR (|)
Bitwise Exclusive OR (^)
These are binary operators and require two integer type
operands. These operators work on their operands bit by bit
starting from the least significant (i.e the right most) bit, setting
each bit in the result.
PREPROCESSOR DIRECTIVES
The Preprocessor is a program that processes the source code
before it passes through the compiler. It operates under the
control of what is known as preprocessor command lines or
directives. Preprocessor directives are placed in the source
program before the main line. Before the source code passes
through the compiler, it examined by the preprocessor for any
preprocessor directives. Preprocessor directives begin with the #
and do not require a semicolon at the end. A set of commonly
used preprocessor directives are:
Preprocessor directives can be divided into three categories:
1. Macro Substitution Directives
2. File Inclusion Directives
3. Compiler Control Directives
1. MACRO SUBSTITUTION DIRECTIVES: Macro substitution is a
process where an identifier in a program is replaced by a
predefined string composed of one or more tokens. The
preprocessor accomplishes this task under the direction of
#define statement. This statement is known as a macro definition.
Syntax:
The string may be any text, while the identifier must be a valid ‘c’
name. there are different forms of macro substitution. The most
common forms are:
A. Simple Macro Substitution
B. Argumented Macro substitution
C. Nested Macro Substitution
A. Simple Macro Substitution: Simply string replacement is
commonly used to define constants.
Example: #define M 10
Will replace all occurrences of ‘M’ with ‘10’ starting from the line
of definition to the end of the program. However, a macro inside a
string does not get replaced.
A macro definition can include more than a simple constant value.
It can include expressions as well.
Example: #define AREA 5 * 12.46
#define SIZE sizeof(int) * 4
#define TWO-PI 2.0 * 3.1415926
B. Macro with Arguments: The preprocessor prermits us to define
more complex and more useful form of replacements.
Syntax: #define identifier (f1, f2, ………………fn) string
There is no space between the macro identifier and the left
parentheses. The identifiers f1, f2, …….fn are the formal macro
arguments that are analogous to the formal arguments in a
function definition. Subsequent occurrence of a macro with
arguments is known as a macro call. When a macro is called the
preprocessor substitutes the string, replacing the forma
parameters with the actual parameters.
Example: #define CUBE(x) x*x*x
gets( ) function:
The gets function is used to read a set of alpha numeric
characters from the keyboard until carriage return is pressed and
places a null character in the memory.
puts( ) function:
The put function continuous to send characters to the standard
output stream until a null character is found.
STRING HANDLING FUNCTIONS:
C library support a large number of string handling functions that
can be used to carry of many of the string manipulations.
strlen( ): The strlen function is used to find the number of
characters in a given string. A string constant (or) an array of
characters can be passed as an argument. The length of the string
excludes the end of string character (NULL).
Syntax: variable = strlen(array_name);
Example:
STRUCTURES
Array can be used to represent a group of data items that belong
to the same type, such as int or float. If we want to represent a
collection of data items of different data types using a single
name is called structure. The structure is a method for packing
data of different data types. A structure is convenient tool for
handling a group of logically related data items.
Defining a Structure:
Syntax:
Graphical Representation:
It would refer to the marks obtain in the 4th subject by the 6th
student.
FUNCTIONS AND STRUCTURES
‘C’ supports the passing of structure values as arguments to
functions. The general format of sending a copy of a structure to
the called function is:
UNIONS
In structures each member has its own storage location where as
all the members of a union use the same location. A union may
contain many members of different types. It can handle one
member at a time. Like Structures, a union can be declared using
the keyword union.
1. The first part declares the data types and specifies its
possible values. These values are called “enumerators”.
2. The second part declares variable of this data types.
Now we can give values to these variables.
person1=married;
person2=divorced;
Internally, the compiler treats the enumerators as integers, each
value on the list of values corresponds to an integer, starting with
0. Thus the above example, single is stored as 0, married is stored
1, divorced as 2 and widowed as 3.
Typedef provides a short and meaning full way to call a data type.
TYPE CASTING
Sometimes we are required to force the compiler to explicitly
convert the values of an expression to a particular data type. The
process of converting one data type into another is called casting.
Automatic conversion:
For some types, it is possible to assign a value of one type to
a variable of a different type without a cast. ‘C’ does the
conversion of the assigned value automatically. This is known as
automatic type conversion. Automatic conversion is possible only
if the destination type has enough precession to store the source
value. For example, int is large enough to hold a short int value.
Therefore,
This tells the compiler three things about the variable pt_name.
1. The ‘*’ operator tells that the variable pt_name is a pointer
variable.
2. pt_name needs a memory location.
3. pt_name points to a variable of type data type.