A computer needs to be programmed to solve a problem.
C is a programming language developed at AT&T bell labs.
Any programming Language can be divided in to two categories.
Problem oriented (High level language)
Machine oriented (low level language)
C is considered as a middle level Language.
C is modular,portable,reusable
C is a language of few words that are known as keyword.
C code can be written in routines called function
Algorithm: to understand about the program
C is famous because reliable,simple,easy
Compactness,generality,efficiency,modularity and simplicity
Preparing to program
(1)Determine the objective of the program.
Determine the methods you want to use .
Create the program to solve the problem
Run the program for output
Feature of C Program
Structured language
General purpose language
Portability
Code Re usability
Ability to customize and extend
Limited Number of Key Word
Structured Language
It has the ability to divide and hide all the information and instruction.
Code can be partitioned in C using functions or code block.
C is a well structured language compare to other.
Portability
Portability is the ability to port or use the software written .
One computer C program can be reused.
By modification or no modification.
General Purpose Language
Make it ideal language for system programming.
It can also be used for business and scientific application.
ANSI established a standard for c in 1983.
The ability of c is to manipulate bits,byte and addresses.
It is adopted in later 1990.
Code Re usability and Ability to Customize and Extend
A programmer can easily create his own function
It can can be used repeatedly in different application
C program basically collection of function
The function are supported by 'c' library
Function can be added to 'c' library continuously
Limited number of keywords and unix library function
There are only 32 keywords in 'C'
27 keywords are given by ritchie
5 is added by ANSI
the strength of 'C' is lies in its in-built function
unix system provides as large number of C function
some function are used in operation .
other are for specialized in their application
'C' program structure
Pre-processor directives
Global declarations
Main( )
{
Local variable deceleration
Statement sequences
Function invoking
}
Writing,compiling and executing
Program need to be compiled before execution
'C' is case sensitive
Linking is the process of joining other program files
All keywords are lowercased
C is case sensitive
Keywords cannot be used for any other purpose
C statement always ends with a semicolon
No blank spaces are allowed with in a variable,constant or keyword
File should be have the extension .c
Recap of previous session
Introduction to programming
Features of 'C'
Writing,Compiling,Linking and executing
Writing a 'C' program
Introduction
Data types are used to declare a variety of different values.
Variables are important tool of a programmer
They are used to keep account of various values .
Like in a game ,weight or height of the player etc
Data types
Variable is an identifier
Variable must be declared before it is used in the program
'C' has 5 basic built-in data types
'C' supports aggregates data types
Data type defines a set of values that a variable can store along with a set of operations that can be performed on it.
A variable takes different values at different times
Modifiers are used to alter the meaning of the base type to fit the needs of various situations more precisely .
'C' has five basic built in data type
Char (character value)
Int (integer value)
Float (Floating point value)
Double (Double floating point value)
Void (Valueless)
Void is used to
Declare explicitly that a function is not returning a value
Declare explicitly that a function has no parameter
Create generic pointer
'C' also supports several aggregate types including structure ,union ,enumeration and user defined types.
Declaring a variable
A variable should be declare before it is used in the program
Example: data-type var-list;
Data type must be valid data type and var-list may be consist of one or more identifier names separated by commas operator.
The declaration indicates the compiler about the variable names and types of data
Example : int count;
double balance;
float amount;
Int, double,and float are keywords and cannot be used as variable names.
Assigning values
Values can be assigned to variable using the assignment operator (=).
var-name=expression;
An expression can be a single constant or combination of variables,operators and constants.
Data-type var-name=constant;
int value=250;
char name= “salini”
double amount =76.80
Type Modifier
The basic data types may have various modifiers preceding them except type void.
A modifier is used to alter the meaning of the base type to fit the needs of various situations more precisely.
The list of modifier:
Signed
Unsigned
Long
Short
Character
This is a group of 8 data bits.
'C' represents either a letter of roman alphabet.
Small integer in the range of 0 through to +255.
To give a named memory area in which a single letter has to be stored
Declaration
char letter;
Integer
There are two types of integers
Short
Long
The actual number of bits used in these types is implementation dependent.
For sixteen bit machine ,the long integer is two machine words long,and short integer is one machine word long.
short int smaller_number;
long int big_number;
Real number
Float offers two sizes-float and double that are similar to integer type
Approximately float represents six significant digit and double is twelve.
float length_of_water_line;
double displacement;
Signed and unsigned prefixes
The deceleration can be preceded by the word unsigned for both the character and integer types which the range so that 0 is the minimum,and the maximum is twice that of the signed data type.
If the word in memory is going to be used as a bit pattern or a mask and not a number, the use of unsigned is strongly urged.
The default for the int types is always signed , and char is machine dependent.
Character Set
Characters are used to form the words, numbers and expression
The characters in 'C' are grouped into the following categories:
character set- alphabets from A....Z,a....z
All decimal digits from 0.....9
Special characters include
, . ; : ? ' " ! / | ~ - $
% # & ^ * _ + < > ( ) { } [ ] blank space
Keywords and Indentifier
Every ' C' word is classified as either a keyword or an identifier.
All identifier have fixed meaning, which can not be changed.
Rules for identifier
Legal characters are a-z, A-Z,0-9 and _
Case significant
The first character must be a letter or _
Identifier can be of any length
The reserved keywords should not be used as identifier.
Valid identifier invalid identifier
Count . 1count
Test23 .hi!
high_bal . high balance
keywords
Auto double int struct break
Long switch case enum register
Typedef char extern return union
Float unsigned short continue for
Signed void default goto sizeof
Volatile do if static while
Else const
Constants
Supports numerical as well as character constants.
'C' supports several types of constants.
'C' has two types of modifier.
Variable of type const cannot be modified
Constants are fixed values that do not change during the execution of a program.
'C' supports both numeric and character constants
C supports several types constant
Numeric constant
Integer constant
Floating point (real) constant
Character constant
Single character constant
String constant
The following rules apply to all numeric constant
Non- digit characters and blanks cannot be included
Can be preceded by minus sign
Escape sequences
Certain non printing characters as well as the backslash () and the apostrophe('), can be expressed in terms of escape sequence.
Example for new line
Bell a carriage return
Backspace formfeed f
Horizontal tab quotation mark ”
Vertical tab v apostrophe '
Backslash \ question mark ?
Null
Access modifier
C has two type modifiers that can be used to control the way in which variables may be accessed or modified.
These are
Const
example const int my_age=39;
Volatile
example volatile int date
Operator
Three kinds of operators.
Two types of casting.
'C' includes a class of operators .
That act upon a single operand to produce a new value.
Ternary operator operates on three operands.
'C' is very rich in built -in operator.
An operator is a symbol that indicates the compiler to perform specific mathematical or logical manipulation.
Three general classes of operator
Arithmetic operator
Relational operator
Assignment operator
Special operators to perform particular task
Ternary (conditional) operator
Size of operator
Comma operator
& Operator
. and -> operator
Arithmetic Operators
Operator Action Example
_ Subtraction a-b
+ Addition a+b
* Multiplication a*b
/ division a/b
% modulus Division a%b
- Decrement a- or -a
++ increment a+ or ++
casting
When operand differ in type they may undergo type conversion before the expression takes on its final value.
The final result will be expressed in the highest precision possible, consistent with data type of the operand.
There are two types of casting
Implicit
Explicit
Implicit casting
If either operand is long double, convert the other to long double
If either operand is double , convert the other to float.
Convert char and short to int.
If either operand is long convert the other to long.
Explicit casting
The expression can be converted to a different data type if desired data type, enclosed in parentheses
(data type) expression
int a+m;
sqrt ((double)x)
The cast produces the value of the proper type;x itself is not altered
Linking with math library
This is following code links to the math library
cc -o cast cast.c – lm
the -l switch stands for 'library'
The library that is searched in the case is libm.a the path u search that is '/usr/lib' where libm.a is found .
Unary operator
'C' include a class of operators that act upon a single operand to produce a new value.
The most common unary operation is unary minus.
Example
-143 -(a+m)
-12 -3.2
Increment and Decrement
x=x+1;
x+=1;
x++;
x=x-1;
x-=1;
Postfix and Prefix
A postfix expression is obtained by post fixing a++ or – operator
The value of the expression is the value of the operand.
x=10;
y=++x;
x=10;
y=x++;
Relational and Logical operator
Operator Meaning
< is less then
<= is less then or equal to
> is greater then
>= is greater then or equal to
== is equal to
!= is not equal
&& Logical AND
|| Logical OR
! Logical NOT
Conditional (:?) Operator
var=exp1?exp2:exp3
x=10;
y=x>9 ?100:200;
x=10;
if (x>9)
{
y=100;
}
else
{
y=200;
}
Recap of previous session
Data type
Constants
Operator
POINTERS
Simply stated, a pointer is an address. Instead of being a variable, it is a pointer to a variable stored
somewhere in the address space of the program. It is always best to use an example so load the file named POINTER.C and display it on your monitor for an example of a program with some pointers in it.
For the moment, ignore the data definition statement where we define index and two other fields
POINTERS CONT..
beginning with a star. It is properly called an asterisk, but for reasons we will see later, let's agree to call
it a star. If you observe the statement in line 8, it should be clear that we assign the value of 39 to the
variable named index. This is no surprise, we have been doing it for several programs now. The
statement in line 9 however, says to assign to pt1 a strange looking value, namely the variable index with
POINTERS CONT....
An ampersand in front of it. In this example, pt1 and pt2 are pointers, and the variable named index is a simple variable. Now we have a problem similar to the old chicken and egg problem. We need to learn
how to use pointers in a program, but to do so requires that first we define the means of using the pointers in the program.
The following two rules will be somewhat confusing to you at first, but we need to state the definitions
before we can use them. Take your time, and the whole thing will clear up very quickly.
POINTERS
TWO VERY IMPORTANT RULES
The following two rules are very important when using pointers and must be thoroughly understood.
1. A variable name with an ampersand in front of it defines the address of the variable and therefore points to the variable. .
2. A pointer with a star in front of it refers to the value of the variable pointed to by the pointer line.
POINTERS
Twelve of the program can be read as "The stored (starred) value to which the pointer pt1 points is
assigned the value 13". This is commonly referred to as dereferencing the pointer. Now you can see why it is convenient to think of the asterisk as a star, it sort of sounds like the word store.
0 comments
Post a comment