A C Programming.pdf
-
Upload
ahmedsadat -
Category
Documents
-
view
227 -
download
0
Transcript of A C Programming.pdf
-
8/13/2019 A C Programming.pdf
1/87
Welcome to S.T.T.P. ON
C-PROGRAMMING
Day-1
Introduction to
Organised by CCTEK Sub Center
Govt.Polytechnic Bijapur
By: S.S.Sadat, Lect
Malik Sandal Polytechnic Bijapur
-
8/13/2019 A C Programming.pdf
2/87
12/10/2013 S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
2
-
8/13/2019 A C Programming.pdf
3/87
Introduction to CProgramming
12/10/2013 S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
3
-
8/13/2019 A C Programming.pdf
4/87
Agenda
1.0 Programming Language
2.0 Program Development Steps
3.0 History & Introduction to C 4.0 Basics Environment of C
5.0 Simple C Program
6.0 Characters and Tokens 7.0 Input/output in C
12/10/2013 S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
4
-
8/13/2019 A C Programming.pdf
5/87
12/10/2013 S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
5
1.0 Programming Language
A programming language is a formal language
designed to communicate instructions to computer.
A vocabulary and set of grammatical rules for
instructing a computer to perform specific tasks.
Programming languages can be used to create programs thatcontrol the behaviour of a machine and/or to express
algorithms precisely.
http://en.wikipedia.org/wiki/Formal_languagehttp://en.wikipedia.org/wiki/Machine_instructionhttp://en.wikipedia.org/wiki/Computerhttp://www.webopedia.com/TERM/C/computer.htmlhttp://en.wikipedia.org/wiki/Program_(machine)http://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Program_(machine)http://www.webopedia.com/TERM/C/computer.htmlhttp://en.wikipedia.org/wiki/Computerhttp://en.wikipedia.org/wiki/Machine_instructionhttp://en.wikipedia.org/wiki/Formal_language -
8/13/2019 A C Programming.pdf
6/87
Types of Language
First
Generation
SecondGeneration
Third
Generation
12/10/2013 6S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
7/87
First Generation
Machine language
Natural language of a particular computer
Consists of strings of numbers(1s, 0s)
Instruct computer to perform elementary operationsone at a time
Machine dependant
Strings of numbers giving machine specific instructions
The only language the computer can understand
is machine language (binary language).
12/10/2013 7S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
http://www.webopedia.com/TERM/M/machine_language.htmlhttp://www.webopedia.com/TERM/M/machine_language.html -
8/13/2019 A C Programming.pdf
8/87
Second Generation
Assembly language
English like abbreviations
Translators programs called Assemblers to convert
assembly language programs to machine language.
E.g. add overtime to base pay and store result in gross pay
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
12/10/2013 8S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
http://www.webopedia.com/TERM/A/assembly_language.htmlhttp://www.webopedia.com/TERM/A/assembly_language.html -
8/13/2019 A C Programming.pdf
9/87
-
8/13/2019 A C Programming.pdf
10/87
Fifth Generation
languages used for artificial
intelligence and neural networks
12/10/2013 10S.S.Sadat Lect Malik Sandal PolytechnicBijapur,Mobile No +91 9448179626, E-mail
http://www.webopedia.com/TERM/A/artificial_intelligence.htmlhttp://www.webopedia.com/TERM/A/artificial_intelligence.htmlhttp://www.webopedia.com/TERM/N/neural_network.htmlhttp://www.webopedia.com/TERM/N/neural_network.htmlhttp://www.webopedia.com/TERM/A/artificial_intelligence.htmlhttp://www.webopedia.com/TERM/A/artificial_intelligence.html -
8/13/2019 A C Programming.pdf
11/87
your program into machine language
(Translators)
Translators are system software used to
convert high-level language program into
machine-language code.
two ways to do this:
Compile the program
Interpretthe program
12/10/2013 11
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
http://www.webopedia.com/TERM/C/compile.htmlhttp://www.webopedia.com/TERM/C/compile.html -
8/13/2019 A C Programming.pdf
12/87
Convert
Compiler : Coverts the entire source program at a
time into object code file, and saves it in
secondary storage permanently.
Interpreter : Each statement of source program istranslated into machine code and executed
immediately.
Translation is repeated for every execution of the
source program.
12/10/2013 12
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
http://www.webopedia.com/TERM/C/convert.htmlhttp://www.webopedia.com/TERM/C/convert.html -
8/13/2019 A C Programming.pdf
13/87
Machine Language Assembly Language High-Level Language
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
entry main,^m
sub12 #12,sp
jsb C$MAIN_ARGS
moveb $CHAR_STRING_CON
pusha1 -8(fp)
pusha1 (r2)
calls #2,SCANF
pusha1 -12(fp)
pusha1 3(r2)
calls #2,SCANF
mull3 -8(fp),-12(fp),-
pusha 6(fp)calls #2,PRINTF
clrl r0
ret
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
00000000 00000100 0000000000000000
01011110 00001100 11000010 0000000000000010
11101111 00010110 0000000000000101
11101111 10111110 0000000000001011
11111000 10101101 11011111 0000000000010010
01100010 11011111 0000000000010101
11101111 00000010 11111011 0000000000010111
11110100 10101101 11011111 0000000000011110
00000011 10100010 11011111 0000000000100001
11101111 00000010 11011111 0000000000100100
01111110 11110100 10101101
11111000 10101110 11000101 0000000000101011
00000110 10100010 11111011 000000000011000111101111 00000010 11111011 0000000000110100
01010000 11010100 0000000000111011
00000100 0000000000111101
1
2
3
4
5
6
7
8
9
1
0
#include
int main(void)
{
int n1, n2,product;
printf(Enter two numbers : );
scanf(%d %d,&n1,&n2);
product = n1 * n2;
printf(%d,product);
return 0;
}
A low level language corresponds closely to machinecode so that a single low-level language instruction
translates to a single machine language instruction.
12/10/2013 13
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
14/87
Programming languages and scripting
languagesALGOL
Ada
AIML
Assembly
AutoHotkey
BASICBatchfile
BCPL
C
C#
C++
COBOL
CPL
D
DarkBASIC
dBASE
FORTRANFoxPro
HDML
HTML
Java
JavaScriptJCL
LISP
Live Script
LOGO
Pascal
Perl
PHP
Pick
Python
PrologRuby
SGML
Smalltalk
SQL
TclTrue BASIC
VHDL
Visual Basic
Visual FoxPro
WML
WHTML
XML
12/10/2013 14
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
http://www.computerhope.com/jargon/f/fortran.htmhttp://www.computerhope.com/jargon/a/ada.htmhttp://www.computerhope.com/jargon/a/aiml.htmhttp://www.computerhope.com/jargon/a/al.htmhttp://www.computerhope.com/jargon/a/autohotkey.htmhttp://www.computerhope.com/jargon/b/basic.htmhttp://www.computerhope.com/jargon/b/batchfil.htmhttp://www.computerhope.com/jargon/b/bcpl.htmhttp://www.computerhope.com/jargon/c/c.htmhttp://www.computerhope.com/jargon/c/csharp.htmhttp://www.computerhope.com/jargon/c/cplus.htmhttp://www.computerhope.com/jargon/c/cobol.htmhttp://www.computerhope.com/jargon/c/cpl.htmhttp://www.computerhope.com/jargon/d/dlang.htmhttp://www.computerhope.com/jargon/d/darkbasi.htmhttp://www.computerhope.com/jargon/d/dbase.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/h/hdml.htmhttp://www.computerhope.com/jargon/h/html.htmhttp://www.computerhope.com/jargon/j/java.htmhttp://www.computerhope.com/jargon/j/javascri.htmhttp://www.computerhope.com/jargon/j/jcl.htmhttp://www.computerhope.com/jargon/l/lisp.htmhttp://www.computerhope.com/jargon/l/livescri.htmhttp://www.computerhope.com/jargon/l/logo.htmhttp://www.computerhope.com/jargon/p/pascal.htmhttp://www.computerhope.com/jargon/p/perl.htmhttp://www.computerhope.com/jargon/p/php.htmhttp://www.computerhope.com/jargon/p/pick.htmhttp://www.computerhope.com/jargon/p/python.htmhttp://www.computerhope.com/jargon/r/ruby.htmhttp://www.computerhope.com/jargon/s/sgml.htmhttp://www.computerhope.com/jargon/s/smaltalk.htmhttp://www.computerhope.com/jargon/s/sql.htmhttp://www.computerhope.com/jargon/t/tcl.htmhttp://www.computerhope.com/jargon/t/truebasi.htmhttp://www.computerhope.com/jargon/v/vhdl.htmhttp://www.computerhope.com/jargon/v/vb.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/w/wml.htmhttp://www.computerhope.com/jargon/x/xhtml.htmhttp://www.computerhope.com/jargon/x/xml.htmhttp://www.computerhope.com/jargon/x/xml.htmhttp://www.computerhope.com/jargon/x/xhtml.htmhttp://www.computerhope.com/jargon/w/wml.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/v/vb.htmhttp://www.computerhope.com/jargon/v/vhdl.htmhttp://www.computerhope.com/jargon/t/truebasi.htmhttp://www.computerhope.com/jargon/t/tcl.htmhttp://www.computerhope.com/jargon/s/sql.htmhttp://www.computerhope.com/jargon/s/smaltalk.htmhttp://www.computerhope.com/jargon/s/sgml.htmhttp://www.computerhope.com/jargon/r/ruby.htmhttp://www.computerhope.com/jargon/p/prolog.htmhttp://www.computerhope.com/jargon/p/python.htmhttp://www.computerhope.com/jargon/p/pick.htmhttp://www.computerhope.com/jargon/p/php.htmhttp://www.computerhope.com/jargon/p/perl.htmhttp://www.computerhope.com/jargon/p/pascal.htmhttp://www.computerhope.com/jargon/l/logo.htmhttp://www.computerhope.com/jargon/l/livescri.htmhttp://www.computerhope.com/jargon/l/lisp.htmhttp://www.computerhope.com/jargon/j/jcl.htmhttp://www.computerhope.com/jargon/j/javascri.htmhttp://www.computerhope.com/jargon/j/java.htmhttp://www.computerhope.com/jargon/h/html.htmhttp://www.computerhope.com/jargon/h/hdml.htmhttp://www.computerhope.com/jargon/f/foxpro.htmhttp://www.computerhope.com/jargon/f/fortran.htmhttp://www.computerhope.com/jargon/d/dbase.htmhttp://www.computerhope.com/jargon/d/darkbasi.htmhttp://www.computerhope.com/jargon/d/dlang.htmhttp://www.computerhope.com/jargon/c/cpl.htmhttp://www.computerhope.com/jargon/c/cobol.htmhttp://www.computerhope.com/jargon/c/cplus.htmhttp://www.computerhope.com/jargon/c/csharp.htmhttp://www.computerhope.com/jargon/c/c.htmhttp://www.computerhope.com/jargon/b/bcpl.htmhttp://www.computerhope.com/jargon/b/batchfil.htmhttp://www.computerhope.com/jargon/b/basic.htmhttp://www.computerhope.com/jargon/a/autohotkey.htmhttp://www.computerhope.com/jargon/a/al.htmhttp://www.computerhope.com/jargon/a/aiml.htmhttp://www.computerhope.com/jargon/a/ada.htmhttp://www.computerhope.com/jargon/a/algol.htm -
8/13/2019 A C Programming.pdf
15/87
2.0 Program Development Steps1)Statement of Problem
a) Working with existing system and using properquestionnaire, the problem should be explained
clearly.
b) What inputs are available, outputs are required
and what is needed for creating workable solution
should be understood clearly.
2)Analysis
a) The method of solutions to solve the problem canbe identified.
b) judge that which method gives best results
among different methods of solution.
3)Designing
a) Algorithms and flow charts will be prepared.
b) Keep focus on data, architecture, user interfaces
and program components.4)Implementation
The algorithms and flow charts developed in the
previous steps are converted into actual programs in
the high level languages like C.
4.a)Compilation
Translate the program into machine code. Thisprocess is called as Compilation. Syntax errors are
found quickly at the time of compiling the program.
These errors occur due to the usage of wrong
syntaxes for the statements.
Eg: x=a*y+b
There is a syntax error in this statement, since, each
and every statement in C language ends with a
semicolon (;).
4.b)Execution
The next step is Program execution. In this phase, we
may encounter two types of errors.
Runtime Errors: these errors occur during the
execution of the program and terminates theprogram abnormally.
Logical Errors: these errors occur due to incorrect
usage of the instructions in the program. These
errors are neither detected during compilation or
execution nor cause any stoppage to the program
execution but produces incorrect result
12/10/2013 15
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
16/87
Algorithms
Step by step procedure designed to perform an operation,
and which (like a map or flowchart) will lead to the
sought result if followed correctly.
Algorithms have a definite beginning and a definite end, and a
finite number of steps.
An algorithm produces the same output information as given
the same input information, and several short algorithms can
be combined to perform complex tasks .
The word algorithm comes from the name of a Persian
author, Abu Jafar Mohammed ibn Musa al Khowarizmi (c. 825
A.D.), who wrote a textbook on mathematics.
12/10/2013 16
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
http://www.businessdictionary.com/definition/procedure.htmlhttp://www.businessdictionary.com/definition/design.htmlhttp://www.businessdictionary.com/definition/operation.htmlhttp://www.businessdictionary.com/definition/map.htmlhttp://www.businessdictionary.com/definition/flowchart.htmlhttp://www.businessdictionary.com/definition/sales-lead.htmlhttp://www.businessdictionary.com/definition/result.htmlhttp://www.businessdictionary.com/definition/definite.htmlhttp://www.businessdictionary.com/definition/produce.htmlhttp://www.businessdictionary.com/definition/output.htmlhttp://www.businessdictionary.com/definition/information.htmlhttp://www.businessdictionary.com/definition/input.htmlhttp://www.businessdictionary.com/definition/complex.htmlhttp://www.businessdictionary.com/definition/task.htmlhttp://www.businessdictionary.com/definition/task.htmlhttp://www.businessdictionary.com/definition/complex.htmlhttp://www.businessdictionary.com/definition/input.htmlhttp://www.businessdictionary.com/definition/information.htmlhttp://www.businessdictionary.com/definition/output.htmlhttp://www.businessdictionary.com/definition/produce.htmlhttp://www.businessdictionary.com/definition/definite.htmlhttp://www.businessdictionary.com/definition/result.htmlhttp://www.businessdictionary.com/definition/sales-lead.htmlhttp://www.businessdictionary.com/definition/flowchart.htmlhttp://www.businessdictionary.com/definition/map.htmlhttp://www.businessdictionary.com/definition/operation.htmlhttp://www.businessdictionary.com/definition/design.htmlhttp://www.businessdictionary.com/definition/procedure.html -
8/13/2019 A C Programming.pdf
17/87
Flow Chart
A flowchart is a type ofdiagram that
represents an algorithm or process, showing
the steps as boxes of various kinds, and their
order by connecting them with arrows.
12/10/2013 17
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
http://en.wikipedia.org/wiki/Diagramhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Process_(science)http://en.wikipedia.org/wiki/Process_(science)http://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Diagram -
8/13/2019 A C Programming.pdf
18/87
Basic Flowchart shapes
12/10/2013 18
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
19/87
Loops
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
19
-
8/13/2019 A C Programming.pdf
20/87
Flow chart symbols
Oval Terminal
Parallegram Input/output
Rectangle Process
Document Hard copy
Diamond Decision
Circle Connector
Double sided Rectangle Sub program
Hexagon Iteration
Trapezoid Manual Operation
Cylinder Magnetic Disk Storage
12/10/2013 20
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
21/87
Area of rectangle
12/10/2013 21
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
22/87
1. Flowchart for a program to calculate
the area of a square
12/10/2013 22
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
23/87
Sum and average
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
23
-
8/13/2019 A C Programming.pdf
24/87
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
24
-
8/13/2019 A C Programming.pdf
25/87
3.0 History & Introduction to C
Evolved from two previous languages BCPL , B
BCPL (Basic Combined Programming Language)
used for writing OS & compilers B used for creating early versions of UNIX OS
Both were typeless languages
C language evolved from B (Dennis Ritchie Bell
labs)
12/10/2013 25
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
26/87
Introduction to C
Dennis M. Ritchie
C programming language is also a high-level programming languagedeveloped by
Dennis Ritchie at Bell Labs
in 1972from an almost unknown language
named B.
in 1978 the publication of
The C Programming Language by
Kernighan & Ritchie
http://www.computerhope.com/people/dennis_ritchie.htmhttp://www.computerhope.com/history/196080.htmhttp://www.computerhope.com/history/196080.htmhttp://www.computerhope.com/people/dennis_ritchie.htm -
8/13/2019 A C Programming.pdf
27/87
"ANSI C"
In 1983, the American National Standards
Institute (ANSI) established a committee to
provide a modern, comprehensive definition
of C. The resulting definition, the ANSIstandard, or "ANSI C", was completed late
1988
12/10/2013 27
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
28/87
first "portable" language
Cborn in the Computer Science Research Department of Bell Labs
in Murray Hill,
The first major program written in C was theUnix operating system,
12/10/2013 28
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
29/87
Why use C? Mainly because it produces code that runs nearly
as fast as code written in assembly language.Some examples of the use of C might be: Operating Systems Language Compilers, Embedded systems
Assemblers Text Editors Print Spoolers Device drivers ,Network Drivers Modern Programs Data Bases , graphics and computational geometry Language Interpreters Application programs & Utilities
Mainly because of the portability that writing standard C programs can
offer12/10/2013 29S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
30/87
Development with C
Four stages
Editing: Writing the source code by using some editor
Preprocessing or libraries: Already available routines
compiling: translates or converts source to object code for a specific platform
source code -> object code
linking: resolves external references and produces the executable module
Portable programs will run on any machine but..
Note! Program correctness and robustness are most important thanprogram efficiency
12/10/2013 30
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
31/87
4.0 Basics Environment of C C systems consist of 3 parts
Environment
Language
C Standard Library
Development environment has 6 phases Edit
Pre-processor
Compile
Link Load
Execute
12/10/2013 31
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
B i f C E i
-
8/13/2019 A C Programming.pdf
32/87
Basics of C Environment
Editor DiskPhase 1Program edited inEditor and stored
on disk
Preprocessor DiskPhase 2
Preprocessor
program processesthe code
Compiler DiskPhase 3
Creates object code
and stores on disk
Linker DiskPhase 4
Links object code
with libraries and
stores on disk12/10/2013 32
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
33/87
Basics of C Environment
LoaderPhase 5
Puts program in
memory
Primary memory
CPUPhase 6
Takes each instruction
and executes it storingnew data values
Primary memory
12/10/2013 33
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
34/87
5.0 Simple C Program
/* A first C Program*/
#include
void main()
{printf("Hello World \n");
}
12/10/2013 34
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
35/87
Structure of a C program
#include
void main (void)
{
printf(\nHello World\n);
}
Preprocessor directive (header file)
Program statement
#include #define VALUE 10
int global_var;
void main (void)
{
/* This is the beginning of the program */
int local_var;
local_var = 5;
global_var = local_var + VALUE;
printf (Total sum is: %d\n, global_var); // Print out the result}
} Preprocessordirective
Global variable declarationComments
Local variable declaration
Variable definition
12/10/2013 35
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
36/87
Simple C Program
Line 1: #include
As part of compilation, the C compiler runs a programcalled the C preprocessor. The preprocessor is able to
add and remove code from your source file.
In this case, the directive #include tells thepreprocessor to include code from the file stdio.h.
This file contains declarations for functions that theprogram needs to use. A declaration for the printffunction is in this file.
12/10/2013 36
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
37/87
Preprocessor Directives
The first thing to be checked by the compiler. Starts with #.
Tell the compiler about specific options that itneeds to be aware of during compilation.
There are a few compiler directives. But only 2 ofthem will be discussed here.
#include
Tell the compiler to include the file stdio.h during compilation
Anything in the header file is considered a part of the program
#define VALUE 10
Tell the compiler to substitute the word VALUE with 10 duringcompilation
12/10/2013 37
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
38/87
Simple C Program
Line 2: void main()
This statement declares the main function.
A C program can contain many functions but mustalways have one main function.
A function is a self-contained module of code that canaccomplish some task.
Functions are examined later. The "void" specifies the return type of main. In this
case, nothing is returned to the operating system.
12/10/2013 38
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, E-mail
-
8/13/2019 A C Programming.pdf
39/87
Simple C Program
Line 3: {
This opening bracket denotes the start of the
program.
12/10/2013 39
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
40/87
Simple C Program
Line 4: printf("Hello World \n");
Printfis a function from a standard C library that isused to print strings to the standard output, normally
your screen. The compiler links code from these standard libraries
to the code you have written to produce the finalexecutable.
The "\n" is a special format modifier that tells theprintfto put a line feed at the end of the line.
If there were another printfin this program, its stringwould print on the next line.
12/10/2013 40
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
41/87
Simple C Program
Line 5: }
This closing bracket denotes the end of the
program.
12/10/2013 41
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
42/87
6.0 Characters and Tokens
Characters are thebasic building blocks
in C program,
equivalent to letters
in English language
Includes every
printable character
on the standard
English language
keyboard except .
12/10/2013 42
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
43/87
Character Set of C-Language
Numeric digits: 0 - 9
Lowercase/uppercase letters: a - z
and A Z
12/10/2013 43
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Special Symbols : ~ ! @ # $ % ^ & ( ) _ - + = | \ - * + : ; < > , . ? /
White Spaces : space , Horizontal tab, Vertical tab, New Line
Form Feed.
-
8/13/2019 A C Programming.pdf
44/87
-
8/13/2019 A C Programming.pdf
45/87
There are 6 kinds of tokens in C:
Reserved words (keywords)
Identifiers
Constants
String literals Boundaries or Separators or Delimiters
Operators
12/10/2013 45
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
46/87
Reserved Words
Key words : have a predefined meaning and these
meanings cannot be changed.
Have special meaning to the compiler, cannot be
used as identifiers in program. Should be typed in lowercase.
Example: const, double, int, main, void, while, for,
else (etc..)
12/10/2013 46
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
47/87
Example
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
12/10/2013 47
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
48/87
Primary Data Types
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
48
Data
Types
int float Double char
-
8/13/2019 A C Programming.pdf
49/87
Basic Data Types
basic data types:
int (used to declare numeric program variables of
integer type)
char (used to declare character variable)
double (used to declare floating point variable)
In addition, there are float, void, short, long, etc.
Declaration: specifies the type of a variable.
Example: int local_var;
Definition: assigning a value to the declaredvariable.
Example: local_var = 5;
12/10/2013 49
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
50/87
Constants(Literals)
Fixed values that do not change during execution
of a program types of constants:
Boolean constants : 0 ( false) and 1 (true)
Integer constants
Positive or negative whole numbers with no fractional part
Example:
const int MAX_NUM = 10;
const int MIN_NUM = -90;
Floating-point constants Positive or negative decimal numbers with an integer part, a
decimal point and a fractional part
Example:
const double VAL = 0.5877e2; (stands for 0.5877 x 102)
12/10/2013 50
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
51/87
Character constants
A character enclosed in a single quotation mark
Example: const char letter = n;
const char number = 1;
printf(%c, S);
Output would be: S
Enumeration Values are given as a list
Example:
enum Language {
Malay,
English,
Arabic
;
12/10/2013 51
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
52/87
String Literals
A sequence of any number of characters
surrounded by double quotation marks.
Example:
REFORMASI
My name is Salman
Example of usage in C program:
printf(My room number is BN-1-012\n);
Output: My room number is BN-1-012
12/10/2013 52
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
String Constants : Hello world , Have a nice day!
Complex Constants : real part + imaginary part * I ex : 12.3 + 3.45 * I
-
8/13/2019 A C Programming.pdf
53/87
Identifiers(variables)
Words used to represent certain program entities(program variables, function names, etc).
Example:
int my_name;
my_name is an identifier used as a program variable
void CalculateTotal(int value)
CalculateTotal is an identifier used as a function name
12/10/2013 53
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
54/87
Variables in C
Variables contain values that must be kept duringthe completion of a program (storage) for futureuse.
In C, a variable MUST be declared before it can beused.
Variables can be declared at the start of any blockcode, but most are found at the start of each
function (main inclusive) Local variables are created when the function is
called and destroyed on return from that
12/10/2013 54
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
55/87
Constructing variable names
Rules for define identifiers :a) First character must be alphabetic
character or under scoreb) Second character onwards alphabetic
character of digit or under score.c) First 63 characters of an identifier aresignificant.
d) Cannot duplicate a key word.
e) May not have a space or any other specialsymbol except under score.f) C language is Case-sensitive.
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
55
-
8/13/2019 A C Programming.pdf
56/87
How to declare a variable?
a declaration begins with the type, followed
by the name of the variable:
int x; //declares an integer variable x
More than one variable can be assignedand a
starting value can be given:
float x,y=-2.5,tab[20];
12/10/2013 56
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
57/87
A variable can be declared globally or locally.
A globally declared variable can be accessed from
all parts of the program.
A locally declared variable can only be accessed
from inside the function in which the variable isdeclared.
12/10/2013 57
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
58/87
Punctuators (separators)
Symbols used to separate different parts of the Cprogram.
These punctuators include:
* + ( ) - , ; : #
Usage example:
void main (void)
{
int num = 10;
printf (%i, num);
12/10/2013 58
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Delimiters : Language Pattern of c-languageuses special kind of symbols
: (colon, used for labels) ; (semicolonterminates statement ) ( ) parameter list[ ] ( array declaration and subscript ), { }( block statement )
# ( hash for preprocessor directive ) ,
(comma variable separator )
-
8/13/2019 A C Programming.pdf
59/87
Operators
Tokens that result in some kind of computation oraction when applied to variables or or other
elements in an expression.
Example of operators:
* + = - /
Usage example:
result = total1 + total2;
12/10/2013 59
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
60/87
Comments
Explanations or annotations that are included in aprogram for documentation and clarification
purpose.
Completely ignored by the compiler duringcompilation and have no effect on program
execution.
Starts with / and ends with /
Some compiler support comments starting with//
12/10/2013 60
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
61/87
statement
A specification of an action to be taken by the
computer as the program executes.
In the previous example, there are 2 lines following
variable declaration and variable definition thatterminate with semicolon ;.
global_var = local_var + VALUE;
printf(Total sum is: %d\n, global_var);
Each line is a statement.
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
61
-
8/13/2019 A C Programming.pdf
62/87
Input/output statement
Used the read the data value
scanf(%d %d%d,&a, &b &c); input function
Output used to print the result
Printf(The sum=%d, sum) ; output function
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
62
-
8/13/2019 A C Programming.pdf
63/87
-
8/13/2019 A C Programming.pdf
64/87
7.0 Input/output in C
C language consists of input and output
statement in the form of input functions to
read the data to be processed and output
function to print the result. A C program consists of one or more functions
that contain a group of statements which
perform a specific task.
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
64
-
8/13/2019 A C Programming.pdf
65/87
Basic Functions
A C program must at least have one function: thefunction main.
We can create our own function or use the
functions that has been created in the library, in
which case we have to include the appropriateheader file (example: stdio.h).
12/10/2013 65
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
pre-defined in the header file stdio h
-
8/13/2019 A C Programming.pdf
66/87
pre-defined in the header file stdio.h
These functions are: printf()
scanf()
getchar() & putchar()
In addition to those functions, we will also learn
about Format Specifier and Escape Sequence
which are used with printf() and scanf().
12/10/2013 66
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
printf()
-
8/13/2019 A C Programming.pdf
67/87
printf()
Used to send data to the standard output (usually
the monitor) to be printed according to specific
format.
General format:
printf(control string, variables);
Control string is a combination of text, formatspecifier and escape sequence.
Example:
printf(Thank you); printf(Total sum is: %d\n, global_var);
%d is a format specifier
\n is an escape sequence
12/10/2013 67
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Conversion Specifiers (Format Specifier)
-
8/13/2019 A C Programming.pdf
68/87
p ( p )
Code Format
%a Hexa decimal output in the form of 0xh.hhhhp+d(C99 only)
%s String of characters (until null zero is reached )
%c Character%d Decimal integer
%f Floating-point numbers
%e Exponential notation floating-point numbers
%g Use the shorter of %f or %e
%u Unsigned integer
%o Octal integer%x Hexadecimal integer
%i Signed decimal integer
%p Display a pointer
%n The associated argument must be a pointer to integer, This sepecifier causesthe number of characters written in to be stored in that integer.
%hd short integer%ld long integer
%lf long double
%% Prints a percent sign (%)
Tells the printf() function the format of the output to be printed put.
12/10/2013 68
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Conversion Specifiers
-
8/13/2019 A C Programming.pdf
69/87
Conversion Specifiers
(Format Specifier)
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
69
Code Format %s String of characters (until null zero is reached )
%c Character
%d Decimal integer
%f Floating-point numbers
%e Exponential notation floating-point numbers
%lf long double
%% Prints a percent sign (%)
Commonly used specifiers
-
8/13/2019 A C Programming.pdf
70/87
-
8/13/2019 A C Programming.pdf
71/87
Escape Sequence (commonly used)
\n new line
\t tab
\r carriage return
\a alert
\\ backslash
\ double quote
12/10/2013 71
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Escape sequence is used in the printf() function to do something to
the output.
scanf()
-
8/13/2019 A C Programming.pdf
72/87
scanf()
Read data from the standard input device (usuallykeyboard) and store it in a variable.
General format:
scanf(Control string, &variable);
The general format is pretty much the same asprintf() except that it passes the address of thevariable (notice the & sign) instead of the variableitself to the second function argument.
Example:int age;
printf(Enter your age: );
scanf %d, &a e ;
12/10/2013 72
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
73/87
getchar() and putchar()
getchar() - read a character from standard input putchar() - write a character to standard output
Example:
#include
void main(void)
{
char my_char;
printf(Please type a character: );
my_char = getchar();printf(\nYou have typed this character: );
putchar(my_char);
}
12/10/2013 73
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Data Types ( pre defined )
-
8/13/2019 A C Programming.pdf
74/87
Type Typical Size in Bits Minimal Range
char 8 127 to 127
unsigned char 8 0 to 255
signed char 8 127 to 127
int 16 or 32 32,767 to 32,767
unsigned int 16 or 32 0 to 65,535
signed int 16 or 32 Same as int
short int 16 32,767 to 32,767
unsigned short int 16 0 to 65,535signed short int 16 Same as short int
long int 32 2,147,483,647 to 2,147,483,647
long long int 64 (263) to 263 1 (Added by C99)
signed long int 32 Same as long int
unsigned long int 32 0 to 4,294,967,295
unsigned long long int 64 264 1 (Added by C99)
float 32 3.4e-38 to 3.4e+38
double 64 1.7e-308 to 1.7e+308
long double 80 3.4e-4932 to 1.1e+4932
void -- data type that not return any value
12/10/2013 74
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
75/87
8.0 Arithmetic in C
12/10/2013 75
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
C op era tion Arithmeticoperator
Algeb raicexpression
C exp ression
Addition + f + 7 f + 7
Subtraction - pc p - c
Multiplication * bm b * mDivision / x / y x / y
Modulus % r mod s r % s
-
8/13/2019 A C Programming.pdf
76/87
Precedence order
Highest to lowest
12/10/2013 76
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Operator(s) Operation(s) Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If thereare several pairs of parentheses on the same level (i.e.,
not nested), they are evaluated left to right.
*, /, or% Multiplication,Divi
sion, ModulusEvaluated second. If there are several, they are
evaluated left to right.
+or- Addition
Subtraction
Evaluated last. If there are several, they are
evaluated left to right.
-
8/13/2019 A C Programming.pdf
77/87
ExampleAlgebra:
z = pr%q+w/x-y
C:
z = p * r % q + w / x y ;
Precedence:
1 2 4 3 5
12/10/2013 77
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
-
8/13/2019 A C Programming.pdf
78/87
ExampleAlgebra:
a(b+c)+ c(d+e)
C:
a * ( b + c ) + c * ( d + e ) ;
Precedence:
5
12/10/2013 78
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
1 2
3 4
k
-
8/13/2019 A C Programming.pdf
79/87
Decision Making
Checking falsity or truth of a statement
Equality operators have lower precedence
than relational operators
Relational operators have same precedence
Both associate from left to right
12/10/2013 79
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
k
-
8/13/2019 A C Programming.pdf
80/87
Decision Making
12/10/2013 80
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
Standard algebraic
equality operator orrelational operator
C equality or
relationaloperator
Example of C
condition
Meaning of C
condition
Equality Operators
= == x == y xis equal to y
not = != x != y xis not equal to y
Relational Operators
> > x > y xis greater than y
< = >= x >= y xis greater than or
equal to y
-
8/13/2019 A C Programming.pdf
81/87
Assignment operators
Used to assign the RHS value to LHS variable
= a=10; b=5; sum=a+b;
+= a+=b; is equivalent to a=a+b;
-= c-=d: c=c-d;
*= p*=d; p=p*d;
/= d/=d; d=d/s;
%= i%=j; i=i%j;
12/10/2013 81
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
height = 8; length = 12; width = 10;
volume = height * length * width;
/* volume is now 960 */
I / d
-
8/13/2019 A C Programming.pdf
82/87
Increment/ decrement operators
++ ++a prefix increment (++a)
++ a++ postfix increment (a++)
-- --a prefix decrement(- -a)
-- a-- postfix decrement (a- -)
12/10/2013 82
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
I / d
-
8/13/2019 A C Programming.pdf
83/87
Increment/ decrement operatorsmain(){
int c;c = 5;printf(%d\n, c);printf(%d\n, c++);printf(%d\n\n, c);
c = 5;printf(%d\n, c);printf(%d\n, ++c);printf(%d\n, c);
return 0;}
5
5
6
5
6
6
12/10/2013 83
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
/* prefix operators */
-
8/13/2019 A C Programming.pdf
84/87
/ prefix operators /
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
84
#include int main() {
int a = 7, b = 12, c;
c = b * (++a) + 5 * (++a);
printf( a = %d, a); printf(\n b = %d,b);
printf(\n c = %d,c);
}
Output:
a = 9
b = 12
c = 153 ( 12 * 9 + 5 * 9)
-
8/13/2019 A C Programming.pdf
85/87
/* prefix and postfix operators */
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
85
#include int main() {
int a = 7, b = 12, c;
c = b * (a++) + 5 * (++a);
printf( a = %d, a); printf(\n b = %d,b);
printf(\n c = %d,c);
}
Output:
a = 9
b = 12
c = 136 ( 12 * 8 + 5 * 8)
/* fi d tfi t */
-
8/13/2019 A C Programming.pdf
86/87
/* prefix and postfix operators */
12/10/2013
S.S.Sadat Lect Malik Sandal Polytechnic
Bijapur,Mobile No +91 9448179626, [email protected]
86
#include int main() {
int a = 7, b = 12, c;
c = b * (a++) + 5 * (++a);
printf( a = %d, a); printf(\n b = %d,b);
printf(\n c = %d,c);
}
Output:
a = 9
b = 12
c = 136 ( 12 * 8 + 5 * 8)
-
8/13/2019 A C Programming.pdf
87/87