What is C
Introduction -History
- C is a general-purpose programming language.
- Developed by Dennis Ritchie in 1972 at AT & T bell labs.
- C has now become a widely used professional language for various reasons
(a)- Easy to learn.
(b)- Structured language.
(c)- It produces efficient programs.
(d)- It can be compiled on a variety of computers.
Facts about C
C was invented to write an operating system called Unix. C is a successor of B Language which was introduced around 1970. The language was formalized in 1988 by the American National Standard Institute (ANSI). By 1972 Unix OS almost totally written in C. Today C is the most widely used system programming language. Most of the state of art software has been implemented using C.
Why to use C?
C was initially used for system development work. C was adopted as a system development language because of it produces code that runs nearly as fast as code written in assembly language.
C Compilers
When you write any program in C language and then to run that program you need to compile that program using a C compiler which converts your program into machine language (i.e. Binary format)
Program Development Cycle
Steps of the program development life cycle are :
Step 1: Gather and analyze the program requirements
Step 2: Design the User Interface
Step 3: Design the program
Step 4: Code the program
Step 5: Test the program
Step 6: Document the program
Step 7: Maintain the program
Flowchart Symbols
It is a pictorial presentation of the facts presented in the algorithm. It helps to reach ease of reading the algorithm since it makes use of symbolic shapes. Only problem with this tool is, once drawn, it is difficult to modify or upgrade existing flowchart.
Pseudo code: Its Necessity
It is a textual version of an algorithm where an algorithm written in steps is elaborated using English like a statement. Thus it is not a code but it is an explanation about code. Each statement is a precise, short and meaningful phase that explain codes all tasks of an algorithm. The language used in written pseudo code does not use keywords or any programming related syntax. The pseudo code helps to resolve the complexities of algorithm using simple and meaningful English statements. It makes the task of written algorithm simple by dictating its meaning module wise.
Sentinel Value to end a program
The sentinel value is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.
Example
- Null character: It is use for indicating the end of a null-terminated string.
- Null pointer: It is use for indicating the end of a linked list or a tree.
- End-of-file: It is use for indicating end of file.
Structure of C program
The structure of the C program is mention below.....
Documentation section: In this section we can give title of program and explain its working.
Link section: All header files that needs to be included are mentioned here.
Definition section: All macros, Global constant, functions can be defined here.
Global Declaration: All global declarations are done here
main(): Main methods start here. Word "main" is a keyword that compiler looks for.
Declaration Section: All local variable declaration as well if required initializations is done here.
Executable part: Main body of the program.
Subprogram section: Main body of the program.
Compiler and execution of a program
In the step, we should type our c program and save it with a .c extension. For example, Our program is to find the sum of two number can be saved with a name sum.c. It means our program source code is stored in the file sum.c.
Once the program is typed, the next step is to compile the program using c compiler. The compiler converts the program source code into equivalent machine code. This machine code is called object code. This file is generated with .obj as extension name.
For example, if the source code file name is sum.c, then its object file name will be sum.obj.
Once the object code is generated, the next step is to include the additional machine code needed by the program from the c library. Thus is called linking. For example, if we use printf() function in a c program then that function code which is available in the c library should be included into that c program.
Once, linking is completed, we get an executable file with .exe extension name, For example, If our source code file name is sum.c then the executable file name will be sum.exe.
Finally, the executable file should be run by the C compiler to obtain the output.
Character Set
Character set denotes any digits, alphabets and special symbols.
The Characters are given below
Identifiers
In C programming, identifiers are names given to C entities such as variables, function, structure etc. Identifiers are created to give a unique name to C entities to identify it during the execution of the program.
For Example:
int money;
int mongo_tree;
Here money is an identifier which denotes variables of type integer. Similarly, mango_tree is another identifier which denotes another variable of type integer.
C keywords
Keywords are the words whose meaning has already been
explained to the C compiler (or in a broad sense to the computer).
The keywords can not 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. Some C compilers allow you to
construct variable names that exactly resemble the keywords.
However, it would be safer not to mix up the variable names and
the keywords. The keywords are also called ‘Reserved words’.
explained to the C compiler (or in a broad sense to the computer).
The keywords can not 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. Some C compilers allow you to
construct variable names that exactly resemble the keywords.
However, it would be safer not to mix up the variable names and
the keywords. The keywords are also called ‘Reserved words’.
There are only 32 keywords available in C.
Data Types
It always tells which type of data/value you want to store in a variable. Data type is a classification for identifying one of various types of data. Computation or programming language needs to deal with various types of data.
Integer type data and its categories
Character type data and its categories
Floating- Point type data and its categories
Conversion facts
Constants
The alphabets, numbers and special symbols when properly
combined form constants, variables, and keywords. Let us see what
are ‘constants’ and ‘variables’ in C. A constant is an entity that
doesn’t change whereas a variable is an entity that may change.
In any program, we typically do lots of calculations. The results of
these calculations are stored in the computer's memory. Like human
memory, the computer memory also consists of millions of cells.
The calculated values are stored in these memory cells. To make
the retrieval and usage of these values easy these memory cells
(also called memory locations) are given names. Since the value
stored in each location may change the names given to these
locations are called variable names. Consider the following
example.
combined form constants, variables, and keywords. Let us see what
are ‘constants’ and ‘variables’ in C. A constant is an entity that
doesn’t change whereas a variable is an entity that may change.
In any program, we typically do lots of calculations. The results of
these calculations are stored in the computer's memory. Like human
memory, the computer memory also consists of millions of cells.
The calculated values are stored in these memory cells. To make
the retrieval and usage of these values easy these memory cells
(also called memory locations) are given names. Since the value
stored in each location may change the names given to these
locations are called variable names. Consider the following
example.
Types of constant
C constants can be divided into two major categories
(a) Primary Constants
(b) Secondary Constants
Variables
In any programming language, the programmer needs some memory where it can store all initial values to be used in later stage of execution.
These values are sometimes received from a user while the execution of the program or sometimes are predefined and used directly.
Variables represent words which store changing values. The programmer needs to store these values at some memory location that is tagged by a unique name for identity.
Rule for variable name
- Start the name with alphabets.
- Not start with blank spaces.
- Can't use any special character except underscore symbol ( _ )
- Variables should be declared first in the program.
- Can't use keywords.
Declarations
This instruction is used to declare the type of variables being used
in the program. Any variable used in the program must be declared
before using it in any statement. The type declaration statement is
written at the beginning ofmain( ) function.
in the program. Any variable used in the program must be declared
before using it in any statement. The type declaration statement is
written at the beginning ofmain( ) function.
float rs, grosssal ;
char name, code ;
There are several subtle variations of the type declaration
instruction. These are discussed below:(a)
(b)While declaring the type of variable we can also initialize it as
shown below.int i = 10, j = 25 ;
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;The order in which we define the variables is sometimes
important sometimes not. For example,int i = 10, j = 25 ;is same asint j = 25, j = 10 ;However,float a = 1.5, b = a + 3.1 ;is alright, butfloat b = a + 3.1, a = 1.5 ;
is not. This is because here we are trying to use a even before
defining it.(c) The following statements would workint a, b, c, d ;
a = b = c = 10 ;However, the following statement would not workint a = b = c = d = 10 ;Once again we are trying to use b (to assign to a) before
defining it.
Arithmetic Instruction
A C arithmetic instruction consists of a variable name on the left
hand side of = and variable names & constants on the right hand
side of =. The variables and constants appearing on the right hand
side of = are connected by arithmetic operators like+, -, *, and /.Ex.: int ad ;
float kot, deta, alpha, beta, gamma ;
ad = 3200 ;
kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;Here,*, /, -, + are the arithmetic operators.= is the assignment operator.
2, 5 and 3200 are integer constants.
3.2 and 0.0056 are real constants.ad is an integer variable.kot, deta, alpha, beta, gamma are real variables.
The variables and constants together are called ‘operands’ that are
operated upon by the ‘arithmetic operators’ and the result is
assigned, using the assignment operator, to the variable on lefthand side.
A C arithmetic statement could be of three types. These are as
follows:(a)
(b)
(c)Integer mode arithmetic statement - This is an arithmetic
statement in which all operands are either integer variables or
integer constants.Ex.: int i, king, issac, noteit ;
i = i + 1 ;
king = issac * 234 + noteit - 7689 ;Real mode arithmetic statement - This is an arithmetic
statement in which all operands are either real constants or
real variables.Ex.: float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;Mixed mode arithmetic statement - This is an arithmetic
statement in which some of the operands are integers and
some of the operands are real.Ex.: float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;It is very important to understand how the execution of an
arithmetic statement takes place. Firstly, the right hand side is
evaluated using constants and the numerical values stored in the
variable names. This value is then assigned to the variable on the
left-hand side.
Though Arithmetic instructions look simple to use one often
commits mistakes in writing them. Let us take a closer look at
these statements. Note the following points carefully.
(a)
(b)
(c)
(d)
C allows only one variable on left-hand side of=. That is, z =
k * lis legal, whereas k * l = z is illegal.
In addition to the division operator C also provides a modular
division operator. This operator returns the remainder on
dividing one integer with another. Thus the expression 10 / 2
yields 5, whereas, 10 % 2 yields 0. Note that the modulus
operator (%) cannot be applied on a float. Also note that on
using % the sign of the remainder is always same as the sign
of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2
yields 1.
An arithmetic instruction is often used for storing character
constants in character variables.char a, b, d ;
a = 'F' ;
b = 'G' ;
d = '+' ;When we do this the ASCII values of the characters are stored
in the variables. ASCII values are used to represent any
character in memory. The ASCII values of ‘F’ and ‘G’ are 70
and 71 (refer the ASCII Table in Appendix E).
Arithmetic operations can be performed onints, floats andchars.
Thus the statements,char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z = x + y ;
are perfectly valid, since the addition is performed on the
ASCII values of the characters and not on characters
themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98,
and hence can definitely be added.
(e)
(f)
No operator is assumed to be present. It must be written
explicitly. In the following example, the multiplication
operator after b must be explicitly written.
Unlike other high level languages, there is no operator for
performing exponentiation operation. Thus following
statements are invalid.a = 3 ** 2 ;
b = 3 ^ 2 ;If we want to do the exponentiation we can get it done this
way:#include <math.h>
main( )
{
int a ;
a = pow ( 3, 2 ) ;
printf ( “%d”, a ) ;
}Here pow( ) function is a standard library function. It is being
used to raise 3 to the power of 2.#include <math.h> is a
preprocessor directive. It is being used here to ensure that thepow( ) function works correctly. We would learn more about
standard library functions in Chapter 5 and about preprocessor.
instruction. These are discussed below:(a)
(b)While declaring the type of variable we can also initialize it as
shown below.int i = 10, j = 25 ;
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;The order in which we define the variables is sometimes
important sometimes not. For example,int i = 10, j = 25 ;is same asint j = 25, j = 10 ;However,float a = 1.5, b = a + 3.1 ;is alright, butfloat b = a + 3.1, a = 1.5 ;
is not. This is because here we are trying to use a even before
defining it.(c) The following statements would workint a, b, c, d ;
a = b = c = 10 ;However, the following statement would not workint a = b = c = d = 10 ;Once again we are trying to use b (to assign to a) before
defining it.
A C arithmetic instruction consists of a variable name on the left
hand side of = and variable names & constants on the right hand
side of =. The variables and constants appearing on the right hand
side of = are connected by arithmetic operators like+, -, *, and /.Ex.: int ad ;
float kot, deta, alpha, beta, gamma ;
ad = 3200 ;
kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;Here,*, /, -, + are the arithmetic operators.= is the assignment operator.
2, 5 and 3200 are integer constants.
3.2 and 0.0056 are real constants.ad is an integer variable.kot, deta, alpha, beta, gamma are real variables.
The variables and constants together are called ‘operands’ that are
operated upon by the ‘arithmetic operators’ and the result is
assigned, using the assignment operator, to the variable on lefthand side.
A C arithmetic statement could be of three types. These are as
follows:(a)
(b)
(c)Integer mode arithmetic statement - This is an arithmetic
statement in which all operands are either integer variables or
integer constants.Ex.: int i, king, issac, noteit ;
i = i + 1 ;
king = issac * 234 + noteit - 7689 ;Real mode arithmetic statement - This is an arithmetic
statement in which all operands are either real constants or
real variables.Ex.: float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;Mixed mode arithmetic statement - This is an arithmetic
statement in which some of the operands are integers and
some of the operands are real.Ex.: float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;It is very important to understand how the execution of an
arithmetic statement takes place. Firstly, the right hand side is
evaluated using constants and the numerical values stored in the
variable names. This value is then assigned to the variable on the
left-hand side.
Though Arithmetic instructions look simple to use one often
commits mistakes in writing them. Let us take a closer look at
these statements. Note the following points carefully.
(a)
(b)
(c)
(d)
C allows only one variable on left-hand side of=. That is, z =
k * lis legal, whereas k * l = z is illegal.
In addition to the division operator C also provides a modular
division operator. This operator returns the remainder on
dividing one integer with another. Thus the expression 10 / 2
yields 5, whereas, 10 % 2 yields 0. Note that the modulus
operator (%) cannot be applied on a float. Also note that on
using % the sign of the remainder is always same as the sign
of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2
yields 1.
An arithmetic instruction is often used for storing character
constants in character variables.char a, b, d ;
a = 'F' ;
b = 'G' ;
d = '+' ;When we do this the ASCII values of the characters are stored
in the variables. ASCII values are used to represent any
character in memory. The ASCII values of ‘F’ and ‘G’ are 70
and 71 (refer the ASCII Table in Appendix E).
Arithmetic operations can be performed onints, floats andchars.
Thus the statements,char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z = x + y ;
are perfectly valid, since the addition is performed on the
ASCII values of the characters and not on characters
themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98,
and hence can definitely be added.
(e)
(f)
No operator is assumed to be present. It must be written
explicitly. In the following example, the multiplication
operator after b must be explicitly written.
a = c.d.b(xy) b = c * d * b * ( x * y ) | usual arithmetic statement C statement |
performing exponentiation operation. Thus following
statements are invalid.a = 3 ** 2 ;
b = 3 ^ 2 ;If we want to do the exponentiation we can get it done this
way:#include <math.h>
main( )
{
int a ;
a = pow ( 3, 2 ) ;
printf ( “%d”, a ) ;
}Here pow( ) function is a standard library function. It is being
used to raise 3 to the power of 2.#include <math.h> is a
preprocessor directive. It is being used here to ensure that thepow( ) function works correctly. We would learn more about
standard library functions in Chapter 5 and about preprocessor.
Statements
Statements in C are like sentences in text. Every C statements performs an action. Every C statements ends a semicolon (;). For example, the simple statement printf("my first program"); send the text "my first program" on the screen. Besides simple statements, C also supports compound statements. A compound statement is a set of simple statements enclosed indide curly braces, {and}. To understand the concept of compound statements, we will give an example of it statement allows one to take decision.
if we have the if statement
if(3>1)
printf("three is greater than one");
If the condition (3>1) turns out to be true, statement after if statement is executed. As 3 is indeed greater than 1, condition 3>1 will trrn out to be true, which will cause execution of the next statement. The message" three is greater than one" will be displayed In case, we wish to execute a set of statements if the given condition turns out to be true, we can use a compound statement.
Compound statement, in the above example consist of a pair of simple statements. These two statements have been written inside a pair of curly braces.
No comments:
Post a Comment