38
C Programming Day 3

C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

Embed Size (px)

Citation preview

Page 1: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

C Programming

Day 3

Page 2: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

2Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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 is accessible

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

• Four storage class specifiers available in C– automatic– static– register– extern

Page 3: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

3Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

4Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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. static variables 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

5Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

6Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Storage Class Specifiers…

Storage Area

Scope Life TimeInitial value

automatic StackWithin the function

Within the function

Garbage

externData section

Across translation units

Through out the program

zero

registerCPU register

Within the function Within the

functionGarbage

staticData section

Within the function

Through out the program

zero

Storage class

Page 8: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

8Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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 clarifying the structure of the program

Exampletypedef enum _Days{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}eDays;

Page 9: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Enumerated Data Types…

• Declaring a enumerated data type

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

– TS1 is a variable of type eToggleSwitch and can have either of the values OFF (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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

Preprocessor Directives

Page 11: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Program Life Cycle

Editor

C COMPILER

Linker

Pre-Processor

Compiler

IntermediateCode

stdio.h

Source Code

1011010101

110

110101101Object Files

10110110101010101 010

101111

0111011010101

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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

12Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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 not declared

• EXE not CREATED

Example

File: test.c

Page 13: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

13Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

14Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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 function definition for sum() is missing

• EXE NOT CREATED

Example

File: test.c

Page 15: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

15Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

16Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

17Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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

longer remains 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 macro

expansion

Page 18: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

18Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

19Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

20Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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 #defined appears in its own definitionWrong: #define INFINITY (INFINITY +1)

Page 21: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

• Popular Usage– #define AND &&

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

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

Macro Expansions…

Page 22: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

• Difference between Functions and Macros

– A macro • is be more generic than a function since it will accept

different types of data as arguments• Macros execute faster than their equivalent functions

Macro Expansions…

Page 23: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

File Inclusion

• Some functions are frequently required in a programExample: – 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 header file

Page 24: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

File Inclusion…

• Preprocessor directive to include a header file – #include

• 2 ways to use #include1. #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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

25Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Conditional Compilation

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

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

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

#ifdef macronamestatement1;statement2;

#endif

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

Page 27: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Example:

Conditional Compilation…

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

#ifdef iMAX printf(“iMAX defined ");

#elseprintf(“iMAX not defined ");

#endif

return 0;}

Page 28: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

28Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version 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 different platforms.

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

Page 29: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

• #if, #elif, #else, #endifConditions are based on system variables, apart from the usual ones

• #ifdef, #ifndefConditions test whether a symbol is defined or not

• #undef Undefine a symbol that was already defined• #error

Conditional Compilation…

Page 30: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Example:

Conditional Compilation…

#define iMAX 10

int main (int argc, char *argv) {

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

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

#endif

return 0;}

Page 31: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Makefiles

• Why do we need them?– Tedious to compile each C file and then link them,

especially if you 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

32Copyright © 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 finaladd.o: add.c common.hsub.o: sub.c common.h

Dependency Line

Command Line

MacroDefinition

Page 33: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

33Copyright © 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

34Copyright © 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.c gcc 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

35Copyright © 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.hsub.o: sub.c common.h

Page 36: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

36Copyright © 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 be used by different programs

Page 37: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

37Copyright © 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: C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C

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

Thank You!