c programming
DESCRIPTION
TRANSCRIPT
![Page 1: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/1.jpg)
Introduction to C Programming
Introduction
![Page 2: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/2.jpg)
Books
“The Waite Group’s Turbo C Programming for PC”, Robert Lafore, SAMS
“C How to Program”, H.M. Deitel, P.J. Deitel, Prentice Hall
![Page 3: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/3.jpg)
What is C?
C A language written by Brian Kernighan
and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" language
In recent years C has been used as a general-purpose language because of its popularity withprogrammers.
![Page 4: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/4.jpg)
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 – Assemblers – Text Editors – Print Spoolers – Network Drivers – Modern Programs – Data Bases – Language Interpreters – Utilities
Mainly because of the portability that writing standard C programs can offer
![Page 5: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/5.jpg)
History
In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world
In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.
![Page 6: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/6.jpg)
Why C Still Useful? C provides:
Efficiency, high performance and high quality s/ws flexibility and power many high-level and low-level operations middle level Stability and small size code Provide functionality through rich set of function libraries Gateway for other professional languages like C C++ Java
C is used: System software Compilers, Editors, embedded systems data compression, graphics and computational geometry, utility
programs databases, operating systems, device drivers, system level
routines there are zillions of lines of C legacy code Also used in application programs
![Page 7: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/7.jpg)
Software Development Method
Requirement Specification – Problem Definition
Analysis – Refine, Generalize, Decompose the problem definition
Design – Develop Algorithm
Implementation – Write Code
Verification and Testing – Test and Debug the code
![Page 8: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/8.jpg)
Development with C
Four stages Editing: Writing the source code by using some IDE or 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 than program efficiency
![Page 9: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/9.jpg)
Programming languages
Various programming languages Some understandable directly by computers Others require “translation” steps
– Machine language• Natural language of a particular computer• Consists of strings of numbers(1s, 0s)• Instruct computer to perform elementary
operations one at a time• Machine dependant
![Page 10: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/10.jpg)
Programming languages 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
![Page 11: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/11.jpg)
Programming languages
High-level languages
– To speed up programming even further– Single statements for accomplishing substantial tasks– Translator programs called “Compilers” to convert
high-level programs into machine language
– E.g. add overtime to base pay and store result in gross pay
grossPay = basePay + overtimePay
![Page 12: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/12.jpg)
History of 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)
** Typeless – no datatypes. Every data item occupied 1 word in memory.
![Page 13: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/13.jpg)
History of C
Hardware independent Programs portable to most computers Dialects of C
– Common C – ANSI C
• ANSI/ ISO 9899: 1990• Called American National Standards Institute ANSI C
Case-sensitive
![Page 14: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/14.jpg)
C Standard Library Two parts to learning the “C” world
– Learn C itself– Take advantage of rich collection of existing functions
called C Standard Library Avoid reinventing the wheel SW reusability
![Page 15: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/15.jpg)
Basics of C Environment C systems consist of 3 parts
– Environment– Language– C Standard Library
Development environment has 6 phases– Edit– Pre-processor– Compile– Link– Load– Execute
![Page 16: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/16.jpg)
Basics of C Environment
Editor DiskPhase 1
Program edited in Editor and storedon 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 disk
![Page 17: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/17.jpg)
Basics of C Environment
LoaderPhase 5
Puts program in memory
Primary memory
CPUPhase 6
Takes each instructionand executes it storingnew data values
Primary memory
![Page 18: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/18.jpg)
Simple C Program
/* A first C Program*/
#include <stdio.h>
void main()
{ printf("Hello World \n");
}
![Page 19: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/19.jpg)
Simple C Program
Line 1: #include <stdio.h>
As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file.
In this case, the directive #include tells the preprocessor to include code from the file stdio.h.
This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.
![Page 20: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/20.jpg)
Simple C Program
Line 2: void main()
This statement declares the main function. A C program can contain many functions but must
always have one main function. A function is a self-contained module of code that can
accomplish some task. Functions are examined later. The "void" specifies the return type of main. In this case,
nothing is returned to the operating system.
![Page 21: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/21.jpg)
Simple C Program
Line 3: {
This opening bracket denotes the start of the program.
![Page 22: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/22.jpg)
Simple C Program
Line 4: printf("Hello World From About\n");
Printf is a function from a standard C library that is used 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 final executable.
The "\n" is a special format modifier that tells the printf to put a line feed at the end of the line.
If there were another printf in this program, its string would print on the next line.
![Page 23: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/23.jpg)
Simple C Program
Line 5: } This closing bracket denotes the end of the program.
![Page 24: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/24.jpg)
Escape Sequence
\n new line \t tab \r carriage return \a alert \\ backslash \” double quote
![Page 25: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/25.jpg)
Memory concepts
Every variable has a name, type and value Variable names correspond to locations in computer
memory New value over-writes the previous value– “Destructive
read-in” Value reading called “Non-destructive read-out”
![Page 26: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/26.jpg)
Arithmetic in C
C operation Algebraic C
Addition(+) f+7 f+7
Subtraction (-) p-c p-c
Multiplication(*) bm b*m
Division(/) x/y, x , x y x/y
Modulus(%) r mod s r%s
![Page 27: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/27.jpg)
Precedence order
Highest to lowest• ()• *, /, %• +, -
![Page 28: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/28.jpg)
Example
Algebra:z = pr%q+w/x-y
C:z = p * r % q + w / x – y ;
Precedence:1 2 4 3 5
![Page 29: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/29.jpg)
Example
Algebra:a(b+c)+ c(d+e)
C:a * ( b + c ) + c * ( d + e )
;
Precedence: 3 1 5 4 2
![Page 30: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/30.jpg)
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
![Page 31: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/31.jpg)
Decision Making
Equality operators• ==• !=
Relational operators• <• >• <=• >=
![Page 32: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/32.jpg)
Summary of precedence order
Operator Associativity
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= left to right
![Page 33: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/33.jpg)
Assignment operators
= += -= *= /= %=
![Page 34: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/34.jpg)
Increment/ decrement operators
++ ++a ++ a++ -- --a -- a--
![Page 35: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/35.jpg)
Increment/ decrement operators
main(){
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;}
556
566
![Page 36: C PROGRAMMING](https://reader033.vdocument.in/reader033/viewer/2022061110/5452fd2caf7959904c8b77bb/html5/thumbnails/36.jpg)
Thank You
Thank You