cprogramming_la07_day3

38
8/23/2019 CProgramming_LA07_Day3 http://slidepdf.com/reader/full/cprogrammingla07day3 1/38 C Programming Day 3

Upload: tanujkarnik49

Post on 08-Aug-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 1/38

C Programming

Day 3

Page 2: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 2/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Storage Class Specifiers

• Every variable in a C program has a storage class specifier

– specifies how the variable is stored in the memory

– governs the scope & lifetime of a variable

• Scope of a variable is the area within a program in which it isaccessible

• Lifetime is the time span for which the variable is alive when theprogram is in execution

• Four storage class specifiers available in C

– automatic

– static

– register– extern

Page 3: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 3/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Storage Class Specifiers… 

• automatic

– automatic is the default storage class specifier.

– Scope & Lifetime

• is within the functions in which they are declared.

– auto int iCount;• declares iCount as an automatic integer variable

• auto is a keyword and its optional

Page 4: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 4/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Storage Class Specifiers… 

• static

– Scope of a static variable is within the function in which it is declares

– Lifetime of a static variable is throughout the program i.e. staticvariables retain their values throughout the program

– static int siCount;• declares siCount as a static integer variable

• static is a keyword

– static variables are automatically initialized to zero

Page 5: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 5/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Storage Class Specifiers… 

• register 

– Variables are stored in registers if• They are frequently referenced

• they need to be accessed at a faster speed

– Use of register variables

• The loop counter variables are the best candidates

– Scope & Lifetime of register variables

• within the functions in which they are declared

Page 6: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 6/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Storage Class Specifiers… 

• extern

– The scope of global variables is• within the file in which it is declared

– extern is used

• if a global variable defined in file1.c needs to be made available in file2.c

Page 7: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 7/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Storage Class Specifiers… 

Storage Area Scope Life Time Initial value

automatic StackWithin thefunction

Within thefunction

Garbage

extern Data section

Across

translationunits

Through out

the program zero

register CPU register

Within thefunction Within the

functionGarbage

static Data sectionWithin thefunction Through out

the programzero

Page 8: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 8/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Enumerated Data Types

• Enumeration is a list of named integer constants

• These constants can be used instead of the associated integer

• Enumerations provide self-documenting code and help in clarifyingthe structure of the program

Exampletypedef enum _Days{

Sunday,Monday,

Tuesday,Wednesday,Thursday,Friday,Saturday

}eDays;

Page 9: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 9/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Enumerated Data Types… 

• Declaring a enumerated data type

– eToggleSwitch is an enumeration i.e. it’s a list of 2 integer constantsOFF (0) and ON(1)

– TS1 is a variable of type eToggleSwitch and can have either of the valuesOFF (0) or ON(1)

– TS1=ON will toggle the switch ON hence printf (“%d”, TS1) will print 1 

– TS1=OFF will toggle the switch OFF hence printf (“%d”, TS1) will print 0 

enum eToggleSwitch{

OFF,ON

} TS1;

Page 10: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 10/38

Preprocessor Directives

Page 11: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 11/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Program Life Cycle

Editor 

C COMPILER

Linker 

Pre-Processor 

Compiler 

Intermediate

Code

stdio.h

Source Code

1011

010101

110

110101

101Object Files

101101

101010

10101010

101

111

011101

1010101

10011

RuntimeLibraries

Executable File(prog.exe)

func.hprog.c help.c strtest.c

Header Files

prog.obj help.obj strtest.obj

Software Developer 

Page 12: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 12/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

#include <stdio.h>

int main(void )

{

int iResult;

scanf(“%d%d”, &a,&b); iResult = a + b;

printf(“%d”, iResult); 

return 1;}

Life Cycle

• Edit test.c

• Compile test.c

• Gives error, as a and b are notdeclared

• EXE not CREATED

Example

File: test.c 

Page 13: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 13/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

#include <stdio.h>

int main(void)

{

int iResult,iN1,iN2;

scanf(“%d%d”, &iN1,&iN2); iResult = diff (iN1,iN2);

printf(“%d”, iResult); 

return 1;

}

Life Cycle

• Edit test.c

• Compile test.c

• Gives error as function diff()is not declared

• EXE NOT CREATED

Example

File: test.c 

Page 14: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 14/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

#include <stdio.h>int sum(int a, int b);

int main(void)

{

int iResult,iN1,iN2;scanf(“%d%d”, &iN1,&iN2); 

iResult = sum (iN1,iN2);

printf(“%d”, iResult); 

}

Life Cycle

• Edit test.c

• Compile test.c

– create object file test.o

• Link test.o

• Gives Link Error as functiondefinition for sum() is missing

• EXE NOT CREATED

Example

File: test.c 

Page 15: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 15/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

The C Preprocessor

• The C Preprocessor is a program that processes the source code before

it is passed to the compiler

• Each of these preprocessor directives begin with a # symbol

• The directives provided by the C Preprocessor are for

– macro expansion

– conditional compilation

– file inclusion

Page 16: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 16/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Macro Expansions

 /* file1.c */

# define PI 3.1415

int main (int argc, char *argv)

{

float fRadius, fArea;

printf("\nEnter the radius of the circle:- ");

scanf("%f",& fRadius);

fArea =PI * fRadius * fRadius;

printf("\n Area of the circle = %f", fArea);

retrun 0;

}

 /* file1.i */

# define PI 3.1415

int main (int argc, char *argv)

{

float fRadius, fArea;

printf("\nEnter the radius of the circle:- ");

scanf("%f",& fRadius);

fArea =3.1415 * fRadius * fRadius;

printf("\n Area of the circle = %f", fArea);

return 0;

}

Preprocessor 

Page 17: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 17/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Macro Expansions… 

• Why Macros if the same can be achieved through variables?

– Treating a constant as a variable is principally not correct– A variable may get altered somewhere in the program hence it no longerremains constant

• Advantages of Macro Expansion

– The program become more readable– If the macro expansion has to be changed

• change only the macro definition

• preprocessor will replace all occurrences with the new macroexpansion

Page 18: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 18/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Macro Expansions… 

• Macros with Arguments.

# define AREA(x) (3.14 * x * x)

int main (int argc, char *argv)

{

float fRadius, fArea;

printf("\nEnter the radius of the circle:- ");

scanf("%f",&fRadius);

fArea =AREA(fRadius);

printf("\n Area of the circle = %f", fArea);

return 0;

}

# define AREA(x) (3.14 * x * x)

int main (int argc, char *argv)

{

float fRadius, fArea;

printf("\nEnter the radius of the circle:- ");

scanf("%f",&fRadius);

fArea = 3.14 * fRadius * fRadius;

printf("\n Area of the circle = %f", fArea);

return 0;

}

Preprocessor 

Page 19: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 19/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Macro Expansions… 

• Precautions while writing macros with arguments

– Never leave a blank space between the macro template and its argument

Wrong: #define ABS (iIntVarx) (3.14*iIntVarx *iIntVarx)

Correct: #define ABS(iIntVarx) (3.14*iIntVarx *iIntVarx)

– Parenthesize macro parameters

Wrong: #define ABS(iIntVarx) iIntVarx>=0? iIntVarx :-iIntVarx

Correct: #define ABS(iIntVarx) (iIntVarx)>=0? (iIntVarx) :-(iIntVarx)

– Example

• Usage: ABS(iIntVara-1)

• Translated to:  iIntVara-1>=0? iIntVara-1: -iIntVara-1

• Required:  iIntVara-1>=0? (iIntVara-1): -(iIntVara-1)

Page 20: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 20/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Macro Expansions… 

• Precautions while writing macros with arguments

– Avoid using expressions with side effects as arguments

Careful: #define ABS(i) ((i) >=0 ? (i) : (-(i))

– Example

Usage: ABS(i++)

Translates to: ((i++) >=0 ? (i++) : (-(i++))

– Avoid circular definitions in which the symbol being #definedappears in its own definition

Wrong: #define INFINITY (INFINITY +1)

Page 21: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 21/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

• Popular Usage

– #define AND &&

– #define ARANGE (a>25 && a<50)

– #define FOUND printf(“lost property”); 

Macro Expansions… 

Page 22: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 22/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

• Difference between Functions and Macros

– A macro

• is be more generic than a function since it will acceptdifferent types of data as arguments

• Macros execute faster than their equivalent functions

Macro Expansions… 

Page 23: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 23/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

File Inclusion

• Some functions are frequently required in a program

Example:– printf()

– scanf()

• These function’s prototypes can be stored in a file and it can be

included in every program

• Such files are called as header files

– “.h” extension for the files 

• The prototypes of all related library functions are stored in a headerfile

Page 24: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 24/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

File Inclusion… 

• Preprocessor directive to include a header file

– #include

• 2 ways to use #include

1. #include “abc.h” 

Preprocessor would look for the file abc.h in

- the current directory

- list of directories specified in the system path

2. #include <abc.h>

Preprocessor would look for the file abc.h in- list of directories specified in the system path

Page 25: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 25/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Difference between header files and library files

• A header file is a text file

• A typical header file contains– prototypes,

– definitions

– preprocessor directives

– by convention it contains no program statements

• Header files are used by the preprocessor

• A library file is an object file

• A library file contains a

– collection of compiled modules for frequently used functions

• Library files are used by the linker

Page 26: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 26/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Conditional Compilation

• Certain circumstances might require the compiler to skip over a part ofsource code

• This can be done by the #ifdef and #endif preprocessor directives

• Syntax: #ifdef and #endif preprocessor directives is as follows:

#ifdef macroname

statement1;

statement2;

#endif

Note: If macroname has been #defined, then the block of code between#ifdef and #endif will get compiled

Page 27: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 27/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Example:

Conditional Compilation… 

#define iMAX 10int main (int argc, char *argv){

#ifdef iMAXprintf(“iMAX defined ");

#elseprintf(“iMAX not defined ");

#endif

return 0;}

Page 28: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 28/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Conditional Compilation… 

• Situations where we go for conditional compilation

– To “comment out” lines of code

– To make programs portable

• We might need to write a program which should run on differentplatforms.

• In that case certain piece of code will be platform dependent and itshould be placed between a #ifdef and #endif directives

Page 29: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 29/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

• #if, #elif, #else, #endif  

Conditions are based on system variables, apart from the usual ones• #ifdef, #ifndef  

Conditions test whether a symbol is defined or not

• #undef 

Undefine a symbol that was already defined

• #error

Conditional Compilation… 

Page 30: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 30/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Example:

Conditional Compilation… 

#define iMAX 10

int main (int argc, char *argv){

#if iMAX==10printf(“iiMAX defined as 10");

#elseprintf(“iMAX not defined as 10");

#endif

return 0;

}

Page 31: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 31/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0

Makefiles

• Why do we need them?

– Tedious to compile each C file and then link them, especially ifyou have many source files

• What is a makefile?

– Makefiles are special files used with the make utility– Helps to automatically build and manage projects with many source and

header files

Page 32: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 32/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Makefiles… 

A Makefile contains

• Dependency lines• Executable command lines

• Macro definitions

# A makefile that compiles three C filesOBJECTS = try.o add.o sub.ofinal: $(OBJECTS)

gcc $(OBJECTS) -o final

add.o: add.c common.hsub.o: sub.c common.h

Dependency

Line

Command

Line

Macro

Definition

Page 33: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 33/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Makefiles… 

/* mainfile.c */#include<stdio.h>

main(){

int iSum,iDiff;

iSum=add(1,2);printf("Sum is %d\n",iSum);

iDiff=sub(10,5);printf("Difference is

%d\n",iDiff);}

/* add.c */#include"common.h"int add(int a, int b)

{return(a+b);

}

/* sub.c */

#include"common.h"int sub(int a, int b){

return(a-b);}

/* Common.h */#include<stdio.h>int add(int,int);int sub(int,int);

Page 34: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 34/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Makefiles… 

# makefile to compile and execute three files# mainfile.c add.c and sub.c

final: mainfile.c add.c sub.cgcc mainfile.c add.c sub.c –o final

# makefile to compile and execute three files# mainfile.c add.c and sub.c

OBJECTS=mainfile.o add.o sub.o

final: $(OBJECTS)gcc $(OBJECTS) -o final

TAB space is mandatory

Page 35: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 35/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Makefiles… 

# makefile to compile and execute three files

# mainfile.c add.c and sub.c

OBJECTS=mainfile.o add.o sub.o

final: $(OBJECTS)

gcc $(OBJECTS) -o final

add.o: add.c common.h

sub.o: sub.c common.h

Page 36: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 36/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Makefiles… 

• Libraries

– Compiled objects modules can be stored in a common library

– Using “ar” command object modules can be grouped under a

library with a common name

• Example: ar -r mather.lib add.o sub.o

– These libraries can be used by other programs. One Library can beused by different programs

Page 37: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 37/38

Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Makefiles… 

# makefile to compile and execute three files

# mainfile.c add.c and sub.c

OBJECTS=mainfile.o

final: $(OBJECTS) mather.lib

gcc $(OBJECTS) mather.lib -o final

mather.lib:add.o sub.o

ar -r mather.lib add.o sub.o

add.o: add.c common.hsub.o: sub.c common.h

Page 38: CProgramming_LA07_Day3

8/23/2019 CProgramming_LA07_Day3

http://slidepdf.com/reader/full/cprogrammingla07day3 38/38

C i ht © 2005 I f T h l i Ltd ER/CORP/CRS/LA07/003

Thank You!