Você está na página 1de 87

Welcome to S.T.T.P.

ON
C-PROGRAMMING Day-1 Introduction to
By: S.S.Sadat, Lect Malik Sandal Polytechnic Bijapur

Organised by CCTEK Sub Center Govt.Polytechnic Bijapur

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

Introduction to C Programming
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 3

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
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 4

12/10/2013

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 that control the behaviour of a machine and/or to express algorithms precisely.
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

12/10/2013

Types of Language
Third Generation

Second Generation
First Generation

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

First Generation
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
Strings of numbers giving machine specific instructions

The only language the computer can understand is machine language (binary language).
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 7

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 ADD BASEPAY OVERPAY

STORE GROSSPAY
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 8

Third Generation
High-level programming languages To speed up programming even further Single statements for accomplishing substantial tasks Translator programs called Compilers to convert highlevel programs into machine language E.g. add overtime to base pay and store result in gross pay grossPay = basePay + overtimePay A high level language is an English like language where one instruction typically translates into a series of machine-language instructions.
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 9

Fifth Generation
languages used for artificial intelligence and neural networks

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

10

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 Interpret the program
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

12/10/2013

11

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 is translated into machine code and executed immediately. Translation is repeated for every execution of the source program.

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

12

Machine Language Assembly Language High-Level Language


1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 0000000000110001 11101111 00000010 11111011 0000000000110100 01010000 11010100 0000000000111011 00000100 0000000000111101 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 entry main,^m<r2> sub12 #12,sp jsb C$MAIN_ARGS moveb $CHAR_STRING_CON
pusha1 pusha1 calls pusha1 pusha1 calls mull3 pusha calls clrl ret -8(fp) (r2) #2,SCANF -12(fp) 3(r2) #2,SCANF -8(fp),-12(fp),6(fp) #2,PRINTF r0

1 2 3 4 5 6 7 8 9 1 0

#include<stdio.h> int main(void) { int n1, n2,product; printf(Enter two numbers : ); scanf(%d %d,&n1,&n2); product = n1 * n2; printf(%d,product); return 0; } 12/10/2013

A low level language corresponds closely to machine code so that a single low-level language instruction translates to a single machine language instruction.

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

13

Programming languages and scripting languages


ALGOL Ada AIML Assembly AutoHotkey BASIC Batchfile BCPL C C# C++ COBOL CPL D DarkBASIC dBASE
12/10/2013

FORTRAN FoxPro HDML HTML Java JavaScript JCL LISP Live Script LOGO Pascal Perl PHP Pick Python

Prolog Ruby SGML Smalltalk SQL Tcl True BASIC VHDL Visual Basic Visual FoxPro WML WHTML XML

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

14

2.0 Program Development Steps


1)Statement of Problem a) Working with existing system and using proper questionnaire, 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 can be 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. This process 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 the program 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
15

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

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 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 16

Flow Chart
A flowchart is a type of diagram 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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

17

Basic Flowchart shapes

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

18

Loops

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

19

Flow chart symbols


Oval
Parallegram Rectangle Document Diamond Circle

Terminal
Input/output Process Hard copy Decision Connector

Double sided Rectangle Hexagon Trapezoid


Cylinder
12/10/2013

Sub program Iteration Manual Operation

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

Magnetic Disk Storage


20

Area of rectangle

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

21

1. Flowchart for a program to calculate the area of a square

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

22

Sum and average

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

23

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

24

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

25

Introduction to C
C programming language is also a high-level programming language

developed by Dennis Ritchie at Bell Labs in 1972 from an almost unknown language named B. in 1978 the publication of The C Programming Language by Kernighan & Ritchie

Dennis M. Ritchie

"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 ANSI standard, or "ANSI C", was completed late 1988

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

27

first "portable" language


The first major program written in C was the Unix operating system,

Cborn in the Computer Science Research Department of Bell Labs in Murray Hill, S.S.Sadat Lect Malik Sandal Polytechnic
12/10/2013 Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 28

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
Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 29

Mainly because of the portability that writing standard C programs can S.S.Sadat Lect Malik Sandal Polytechnic offer
12/10/2013

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 than program efficiency

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

30

C systems consist of 3 parts


Environment Language C Standard Library

4.0 Basics Environment of C

Development environment has 6 phases


Edit Pre-processor Compile Link Load Execute
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 31

12/10/2013

Basics of C Environment
Phase 1

Editor

Disk

Program edited in Editor and stored on disk Preprocessor program processes the code Creates object code and stores on disk Links object code with libraries and stores on disk
32

Phase 2 Preprocessor

Disk

Phase 3

Compiler

Disk

Phase 4
12/10/2013

Linker

Disk
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

Basics of C Environment
Primary memory

Phase 5

Loader

Puts program in memory

Primary memory
Phase 6

CPU

Takes each instruction and executes it storing new data values

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

33

5.0 Simple C Program


/* A first C Program*/ #include <stdio.h> void main() {

printf("Hello World \n");

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

34

Structure of a C program
#include <stdio.h>

Preprocessor directive (header file)

void main (void) { printf(\nHello World\n); }

Program statement

#include <stdio.h> #define VALUE 10 int global_var;

Preprocessor directive Global variable declaration Comments

void main (void) { /* This is the beginning of the program */ int local_var; Local variable declaration local_var = 5; global_var = local_var + VALUE;

Variable definition

printf (Total sum is: %d\n, global_var); // Print out the result }
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 35

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.
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

12/10/2013

36

Preprocessor Directives
The first thing to be checked by the compiler. Starts with #. Tell the compiler about specific options that it needs to be aware of during compilation. There are a few compiler directives. But only 2 of them will be discussed here.
#include <stdio.h>
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 during compilation
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 37

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.
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 38

Simple C Program
Line 3: {

This opening bracket denotes the start of the program.

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

39

Simple C Program
Line 4: printf("Hello World \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.
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 40

Simple C Program
Line 5: }

This closing bracket denotes the end of the program.

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

41

6.0 Characters and Tokens


Characters are the basic 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 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 42

Character Set of C-Language

Numeric digits: 0 - 9 Lowercase/uppercase letters: a - z and A Z Special Symbols : ~ ! @ # $ % ^ & ( ) _ - + = | \ , - * + : ; <>,.?/ White Spaces : space , Horizontal tab, Vertical tab, New Line Form Feed.
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 43

Token
The smallest individual units of a C- program are called Tokens A token is a language element that can be used in forming higher level language constructs Equivalent to a word in English language Several types of tokens can be used to build a higher level C language construct such as expressions and statements
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

12/10/2013

44

There are 6 kinds of tokens in C:


Reserved words (keywords) Identifiers Constants String literals Boundaries or Separators or Delimiters Operators

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

45

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

46

Example
Keywords auto
break

double
else

int
long

struct
switch

case
char const

enum
extern float

register
return short

typedef
union unsigned

continue
default do
12/10/2013

for
goto if

signed
sizeof static

void
volatile while
47

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

Primary Data Types

Data Types

int

float

Double

char

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

48

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 declared variable.


Example: local_var = 5;
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 49

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 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 50

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

51

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
String Constants : Hello world , Have a nice day! Complex Constants : real part + imaginary part * I ex : 12.3 + 3.45 * I

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

52

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

53

Variables in C
Variables contain values that must be kept during the completion of a program (storage) for future use. In C, a variable MUST be declared before it can be used. Variables can be declared at the start of any block code, 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 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 54

Constructing variable names


Rules for define identifiers : a) First character must be alphabetic character or under score b) Second character onwards alphabetic character of digit or under score. c) First 63 characters of an identifier are significant. d) Cannot duplicate a key word. e) May not have a space or any other special symbol except under score. f) C language is Case-sensitive.
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 55

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

56

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 is declared.

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

57

Punctuators (separators)
Symbols used to separate different parts of the C program. These punctuators include:
* + ( ) , - , ; : * #

Usage example:
void main (void) { int num = 10; printf (%i, num); }

Delimiters : Language Pattern of c-language uses special kind of symbols : (colon, used for labels) ; (semicolon terminates statement ) ( ) parameter list [ ] ( array declaration and subscript ), { } ( block statement ) # ( hash for preprocessor directive ) , (comma variable separator )

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

58

Operators
Tokens that result in some kind of computation or action when applied to variables or or other elements in an expression. Example of operators:
*+=-/

Usage example:
result = total1 + total2;

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

59

Comments
Explanations or annotations that are included in a program for documentation and clarification purpose. Completely ignored by the compiler during compilation and have no effect on program execution. Starts with /* and ends with */ Some compiler support comments starting with //
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 60

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 that terminate 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, E-mail sssadat@secab.org

61

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, E-mail sssadat@secab.org

62

Assignment statement
Used to perform arithmetic expression sum=a+b+c; Control statement: used to control statement If(a>b) print(a is big); else print(b is big);
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 63

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, E-mail sssadat@secab.org

64

Basic Functions
A C program must at least have one function: the function 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 appropriate header file (example: stdio.h).

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

65

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

66

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, format specifier 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 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 67

Conversion Specifiers (Format Specifier)


Code %a %s %c %d %f %e %g %u %o %x %i %p %n %hd %ld %lf %% Format Hexa decimal output in the form of 0xh.hhhhp+d(C99 only) String of characters (until null zero is reached ) Character Decimal integer Floating-point numbers Exponential notation floating-point numbers Use the shorter of %f or %e Unsigned integer Octal integer Hexadecimal integer Signed decimal integer Display a pointer The associated argument must be a pointer to integer, This sepecifier causes the number of characters written in to be stored in that integer. short integer long integer long double Prints a percent sign (%)

Tells the printf() function the format of the output to be printed put. S.S.Sadat Lect Malik Sandal Polytechnic
12/10/2013 Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 68

Conversion Specifiers (Format Specifier)


Code %s %c %d %f %e %lf %% Format String of characters (until null zero is reached ) Character Decimal integer Floating-point numbers Exponential notation floating-point numbers long double Prints a percent sign (%)

Commonly used specifiers


12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 69

Back Slash ( Escape Sequence) Characters

Code
\b \f \n \r \t \" \' \\ \v \a \? \N \xN
12/10/2013

Meaning
Backspace Form feed New line Carriage return Horizontal tab Double quote Single quote Backslash Vertical tab Alert Question mark Octal constant (N is an octal constant) Hexadecimal constant (N is a hexadecimal constant)
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 70

Escape Sequence (commonly used)


\n \t \r \a \\ \ new line tab carriage return alert backslash double quote
Escape sequence is used in the printf() function to do something to the output.
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 71

scanf()
Read data from the standard input device (usually keyboard) and store it in a variable. General format:
scanf(Control string, &variable);

The general format is pretty much the same as printf() except that it passes the address of the variable (notice the & sign) instead of the variable itself to the second function argument. Example:
int age; printf(Enter your age: ); scanf(%d, &age);
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 72

getchar() and putchar()


getchar() - read a character from standard input putchar() - write a character to standard output Example:
#include <stdio.h>

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 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 73

Data Types ( pre defined )


Type Typical Size in Bits char 8 unsigned char 8 signed char 8 int 16 or 32 unsigned int 16 or 32 signed int 16 or 32 short int 16 unsigned short int 16 signed short int 16 long int 32 long long int 64 signed long int 32 unsigned long int 32 unsigned long long int 64 float 32 double 64 long double 80 void -12/10/2013

Minimal Range 127 to 127 0 to 255 127 to 127 32,767 to 32,767 0 to 65,535 Same as int 32,767 to 32,767 0 to 65,535 Same as short int 2,147,483,647 to 2,147,483,647 (263) to 263 1 (Added by C99) Same as long int 0 to 4,294,967,295 264 1 (Added by C99) 3.4e-38 to 3.4e+38 1.7e-308 to 1.7e+308 3.4e-4932 to 1.1e+4932 data type that not return any value
74

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

8.0 Arithmetic in C
C op era tion Addition Subtraction Multiplication Division Modulus Arithmetic op era tor + * / % Algeb ra ic exp ression f+7 pc bm x/y r mod s C exp ression f p b x r + * / % 7 c m y s

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

75

Precedence order
Highest to lowest
Operator(s) () Operation(s) Parentheses Order of evaluation (precedence) Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. Multiplication,Divi Evaluated second. If there are several, they are sion, Modulus evaluated left to right. Addition Evaluated last. If there are several, they are Subtraction evaluated left to right.

*, /, or % + or -

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

76

Example
Algebra:
z = pr%q+w/x-y

C:
z = p * r % q + w / x y ;

Precedence: 1
12/10/2013

5
77

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

Example
Algebra:
a(b+c)+ c(d+e)

C:
a * ( b + c ) + c * ( d + e ) ;
1 2

Precedence:

5
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 78

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

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

79

Decision Making
Standard algebraic equality operator or relational operator
Equality Operators

C equality or relational operator == != > < >= <=

Example of C Meaning of C condition condition

= not =
Relational Operators

x == y x != y x > y x < y x >= y x <= y

x is equal to y x is not equal to y x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y

> < >= <=

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

80

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;
height = 8; length = 12; width = 10; volume = height * length * width; /* volume is now 960 */
12/10/2013 S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 81

Increment/ decrement operators


++ ++ --++a a++ --a a-prefix increment (++a) postfix increment (a++) prefix decrement(- -a) postfix decrement (a- -)

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

82

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);

Increment/ decrement operators


5 5 6

return 0;

5 6 6

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

83

/* prefix operators */
#include<stdio.h> 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)
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 84

12/10/2013

/* prefix and postfix operators */


#include<stdio.h> 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)
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 85

12/10/2013

/* prefix and postfix operators */


#include<stdio.h> 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)
S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org 86

12/10/2013

12/10/2013

S.S.Sadat Lect Malik Sandal Polytechnic Bijapur,Mobile No +91 9448179626, E-mail sssadat@secab.org

87

Você também pode gostar