Programming in C Unit-1
Programming in C Unit-1
Programming in C Unit-1
Unit 1:
Introduction
History of ‘C’:
C is a powerful high-level language, which is developed by Dennis Ritchie in bell laboratories
in 1972. The UNIX operating system, which was also developed at bell laboratories, was coded almost
entirely in ‘C’.
In1983, American National Stranded Institute (ANSI) appointed a technical committee to
define a standard for ‘c’ .the committee approved a version of ‘c’ in 1989, which is now called as
“ANSI C”.
In 1990 the C was approved by the International Standard Organization (ISO)
Importance of ‘C’:
• It has a rich set of built in functions and operators can be used to write any complete program.
• It is recommended and best for writing both system software & application software
• The compilers available in market are written in ‘C’.
• Programs written in ‘C’ are efficient and fast.
• ‘C’ is highly portable, which means program written in one computer than can be run another
computer without any modification.
• ‘C’ programs can be able to extend itself.
• ‘C’ language is best for structure programming.
Documentation section:
The documentation section contains a set of command lines, which explains the name of the
program, the author name, date of written and other details
For eg: \*program for addition*\
Link section:
The link section offer instructions to the compiler to link functions from system library
For example: #include<stdio.h>
Definition section:
To define all symbolic constants in definition section
For eg: #define PI 3.14
Executable part:
It contains atleast one statement or instruction. These two parts must be appear between the
opening and closing braces. The program executions start at the opening braces and end the closing
braces. The closing braces of the main function indicates the logical end of the program all the
statements in the declaration and executable parts end with a semicolon(;).
1. Subprogram functions:
It contains all the user defined functions that are called in the main functions. User defined function
are generally placed immediately after the main function. Although it may appear in any order.
Note: All section, expect main function section may be absent.
Character set:
The character set that can be used to form words, numbers and expressions offers the
following character and the different categories.
‘C’ tokens
In a c program a smallest individual unit is called as a C tokens. C has 6 types of tokens.
1. Keyword-int,for
2. Identifiers-tot, avg.
3. Constants-555,256.
4. Strings-“jsc”.
5. Special symbol-{},[].
6. Operator-/,*,-, +.
Identifiers:
Identifiers means the names of the variable, functions and arrays this are user defined names
and consists of a sequence of letters&digits.both uppercase and lowercase letters are allowed,
although lowercase letters are commonly use. The underscore character is also permitted in
identifiers. It is used as a link between two words in long identifiers.
Constants:
It means a fixed value that does not changed during the execution of the program.
Type of constant:
1) Numeric constants:
a) Integer constants-eg: 987, 978, and 21.
b) Real constants-eg; 98.3.
2) Character constants:
a) Single character constant-eg:’n’.
b) String constant-eg”Bca”
c) Backslash character constant-“\n”
This special backslashes constants can be used for output functions or statement.eg:”\t” stand for
horizontal tab.
‘\a’-alert bell ‘\n’-newline ‘\t’-horizontal tab ’\b’-backspace.
Variable:
Variable is a data name that is used to store a data value. The value of a variable, which can be
changed during the execution of programs. All the variables must be declared before they can be
used.th value assigned to a variable is placed in the memory allocated to those variables.
Operator in ‘C’
The operator can be used to manipulate data and variables. There are 7 different types of operators.
They are
1) Arithmetic operator
2) Relational operator
3) Shorthand operator
4) Increment and decrement operator
5) Conditional operator
6) Bitwise operator
7) Special operator.
Arithmetic operators:
Most the common binary arithmetic operators are
+ add two numeric values.
- Subtract the second value from first.
/ divides the second value from the first
* Multiply two numeric values
% modules (divide the second value from the first and gives
reminder)
Relational operators:
> Greater than
< lesser than
>= Greater than or equal to
<= Lesser than or equal to
== Equal to
!= Not equal to
Logical operators:
&& Logical and
|| Logical or
! Logical not
Note: the above operators evaluate two expressions and evaluations stop as a true or false of the result
is known.
Assignment operators:
The assignment operators can be used to assign the result of the expression to a variable. In c
assignment operator is’=’
Syntax:
[Variable operator=expression]
Eg: n=m+2.
Conditional operator:
A ternary operator is used to take a decision. It is an alternate to instructor. syntax is
Expression? expression1; expression2;
Eg :(x > y)?
Printf(“x is greater than y”)
Printf(“y is greater than x”)
Bitwise operator:
Bitwise operator can be used for manipulation of data at bit level. These operators are used for
testing the bits or shifting the right or left bitwise operator cannot be applied to float or double.
& Bitwise and
<< Shift left
>> Shift right
! Bitwise or
^ Bitwise exclusive or
Special operators:
The special operators are comma and size of the operator.
Comma operator:
The comma operator can be used to link the related expression together.
Eg: x(n=5,m=2,n+m);
For(k=1,n=3;k<n;k++,n++)
Size of operator:
Assize of operator can be used to returns the no of bytes the operand occupy. The operand
may be a variable, a constant or a data type
Eg1: int total, x;
X=sizeof(total);
2. sizeof (float);
Note: a size of operator generally used to determine size of arrays, and structures. When the size is not
known to the programmer. In addition to that, it is also used to allocate dynamic memory space to the
variables during the execution of the program.
Printf(“%7.2f”,y);
7 5 . 8 0
Printf(“%-7.2f”,y)
7 5 . 8 0
Printing of single character:
A single statements can be displayed in a single formate “%wc”. Here default value ‘w’ is 1.
Char ch;
Ch= m;
Printf(“%c”,ch);