Introduction to d
9.1 Introduction
developed by
I Dennis Ritchie at1970 in Bell Laboratories
used
to be
by the UNIX
operating system
characteristics of G
1. C is a
high-level programming language, near to uses
than Hardware.
e. small size - a has
only 32
keywords.
3. I makes extensive use of function calls, which enhances
re-usability, easier debugging etc.
4. C is well suited for structured programming.
5. c portable Language.
is
3. Facilitates low level (bitwise) programming.
uses of d
1.
widely used for system programming. The portability,
efficiency el
2.
Widely accepted by professionals that compilers, libraries
and interpreters of other PCs written in C
3. c is used as intermediate language for implementing
other Languages.
4. c is
widely used to develop end user
applications.
9.2 structure of a
program.
-
Documentation section Documentation section
preprocessor directives/ Enclosed
-
within .........
->
Header files #...... */
-
Global Declaration -
used write
to problem statement,
#popfor program, versions etc
main()
2 pre-processor Directives
Local Declaration
.....
-
contains specical instructions that
statements indicate how to prepare the program
.....
for compilation
Y -
Definition of all syntaxes used
- user defined functions in the program are defined in
function (c)
directives
2....... -
Global Declaration
3
identifiers used in the program
function 21) are defined in global or local
2 .....
.....
declaration parts.
3
Page I
main
included in every
-
mandatory function
will
to be
start executing from
a
program
-
every program main
user-defined functions
contain number ofuser-defined functions
I
may will any
-
which perform well defined task.
Example
#* Program will print Hello JNNCE * - Documentation
section
-> director
# include stdioh)
Preprocessing
function
intmainc ->
mandatory main
2
Printf("Hello JNNCE"); output
return 0; Hello JNNCE
9.4 Files used in
a
program
has kinds of files associated with it:
Every program 4
Files in a 'program
object
v v
source Header Executable
file file file file
source file:contains Code of the program, saved with
file extension oc
Header file: Header include directive will
included with
files
instruct the preprocessor to include definitions
of functions used in the program before
compilation.
Ex:Staio.h ip
op functions
functions
string. 4 string handling
math.h for mathematical functions
objectfile :
Generated by the compiler as a result of
the source code
processing
-
contains compactbinary code of the
function definitions.
-
saved with oobjextension Page-2
executable:Generated by the linker. The linker links
Binary file
files variousobjectfiles to produce a
binary
thatcan be directly executed.
-
saved with sexe extension
9.5
Compiling and executing a
programs
overview:
·
objfile · exe file
source object -> Linker -> Executable
>compiler -> files
file files
N
· a file g(c
compiler Library
files
·b file
compiler-cheeks the Syntax Errors in the source code
- translates the source code into an objectcode.
object file contains the machine instructions
for the CPU, and calls to the operating
system A PI.
Linker -
Linker combines the objectfile
the with
produce final executable
library routines to the
file.
9.6 comments in a program
-comments written in a
program are notchecked
for syntax errors.
- used for documentation in source code
11..... used to comment a
single
2-ways 1. is
statement
EX:
/Binary Search Logic
2. (f...... is used to comment
...... / multiple lines
Ex: /*
This program will read
employee details
salary calculates
Income tax
907 C Tokens
-
Basic building blocks in C
language. Smallest individual
unit in program.
Page-3
C TO SERS
↓
constants spichars operators
↓ ↓ ↓
keywords variables strings
Keywords: - set
ofreserved
identifiers (used
words that
cannot be used as
defined]
Ex:int, main, const, case, default, for, while, it.....
identifiers: used in the
- to
identify data & other objects
program
-
names given to elements such as variables, arrays
and functions in the program by the programmers.
Rules for forming identifier names
Cannot include characters
any special punctuations
: or
like
marks #, p,1,3,., etc.
2. There cannot be two successive underscores.
3.
Keywords cannot be identifiers
4. Case-sensitive
5. identifies begin
must with a letter or an underscore.
6. Maximum 31 characters.
Valid identifiers Invalid identiers
roll-number, marks, basic pay, 23 students, marks, emp-num
dept_code, stars, wollnol
(da), auto
DATATYPES in G
c
language is
strictly typed language, all data in c
-
stored using some datatype.
Dalatype Keyword size in format
-Range
bytes specified
character char | % -
1285127
int olod,. 32768032767
Integer 2 -
floating point float 4 %of 3.47 - 3873.4E +
38
Double double & ·/lf 1.7E-308 to 1.77 + 308
valueless void ⑧ - ->
Page 4 -
variables
-
meaningfull given to a memory location. The value
name
of the
memory location can be changed any number
of times during the execution ofprogram
Declaring variables:Defining the variables, to be used to
store the data in the program
Syntax:
data type var-name;
EXI: int age; age' is variable
age: 45 which say store
age 45; integer, allocates
=
100 0
2.
Bytes ofmemory.
float salary;
EX2;
salary: 1000.50 salary is variable
which can store
Salary 1000.50;
=
100 0 rational number &
2 Assignment) 4 bytes.
allocated with
Initilization of variables:Assigning the values to the
variables during the declaration is called initialization.
Syntax:datatype var-name value;
=
Ex:int emp-num 10118;
=
char section='B';
constant
-
are identifiers whose values do not change while values of
variables can be changed at
any time,
values of constants
can never be changed.
Declaration: const datatype Varname: Value;
=x:const float pi 3.1415;
=
constants can be of 4-types depending on the type of data.
Page-5
9014 Input/output statements in
- 10 statements involves in
accepting input and printing output
in I.
outputstatement:printf()
Input statement:scanfs
Printf Cs
- used tosend data to the outputterminal, may be text
or values of the variables.
Syntax: 1. Printf ("string to
display");
EX: statement output
Printf ("Hello JNNCE);Hello JNNCE
Printf(" -------"),-------
Printflifofman specifiers,variablesent
2.
↓
of variables
to be printed
EX. statement output
inta10, b
=
50,c 4
=
=
Printf (" 10%did", a, b, c) 10504
printf("a xd,b yd, c 1d a, b, c;
=
=
= a 10,b 50,c 4
= =
=
Printf ("Factorial of 10 24", c); = Factorial of
4 =24
floatSal=1200.5432;
printed salary..7",een salary 1200.5432
=
Salary 1200.54
=
2. ScanfCs
-
used to read formatted data from keyboard.
syntax: scant ("formatspecifiers", variable list);
Exi statement Explaination.
scant ("God", Gndm); Reads one integer & stores in hum
be
every variable should
prefixed with ' to denote
page -
6
the address of location.
scant ("%7%d",
esalary, guum);Read
a values, firstfloat
value (salary& second (num) int.