C Notes by Kumar Harsha

August 24, 2017 | Author: Kumar Harsha | Category: C (Programming Language), Integer (Computer Science), Bit, Data Type, Variable (Computer Science)
Share Embed Donate


Short Description

Download C Notes by Kumar Harsha...

Description

The Concept Of C Programming

Page |1

The Concept Of

C

Programming Written By

Kumar Harsha

For Combined batches by Minds Academy and Net Indiana. www.mindsacademy.in, www.netindiana.co.in

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |2

“Computing is not about computers anymore. It is about living. . . . We have seen computers move out of giant air-conditioned rooms into closets, then onto desktops, and now into our laps and pockets. But this is not the end. . . . Like a force of nature, the digital age cannot be denied or stopped. . . . The information superhighway may be mostly hype today, but it is an understatement about tomorrow. It will exist beyond people’s wildest predictions. . . . We are not waiting on any invention. It is here. It is now. It is almost genetic in its nature, in that each generation will become more digital than the preceding one.” — Nicholas Negroponte, professor of media technology at MIT

1 Introduction Chapter covered 

History of C language



About Dennis M. Ritchie



What is C?



Why C is a popular programming language?



Today’s life with C



Why C is called mother language?



Hello world as a program



Compiling and linking



Executing

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |3

History of C Language In 1969 – 1972, Murray Hill, New Jersey, a language developed that change the computer programming world, makes a revolution in programming. Before the evolution of C language computer programming language is a tough skill that is come in highly skilled programmer but the birth of C language change the criteria of programmers. A novice people have power to build general PC application. The credit for developing such revolutionary language goes to Mr. Dennis M. Ritchie at Dennis M. Ritchie AT&T Bell lab. C is a general purpose programming language. The story started of C with CPL (combined programming language) by Martin Richards and BCPL (Basic Combined Programming Language), which is used this time for system programming language. In this time Ken Thompson also wrote a language B (B for Bell) In AT&T Labs, but need a powerful language in general understanding syntax, so Dennis M. Ritchie work on a Language named C, because its many features derived from earlier programming language B. Much confusion in year of introducing C language, somebody say 1970, 1972 and many other date. By Dennis M. Ritchie the developing period of C language is 1969 to 1972. The origin of C is closely tied to the development of the UNIX system and its code created in assembly language by Dennis Ritchie, Ken Thompson and its several colleagues. Firstly the C implemented in UNIX operating system named DEC PDP – 11, after that the most part of UNIX kernel are rewritten in C language, this show the power of C. During the period 1970 to 1980, it knows as very popular programming language. In 1983, the ANSI (American National Standard Institute) formed a committee X3J11 to verify the standard of C language. After six years ANSI publish report “Programming Language C”. In report C has many standard features for general purpose programming. This time C referred as ANSI C, Standard C and sometimes C89. In 1990, ISO (International Organization for Standardization) adopt the ANSI C and it also certifies the ANSI/ISO C, this referred as ISO C or C90. C89 and ISO C or C90 is just same version only difference is C89 verified by ANSI and C90 is adopted by ANSI/ISO C gain parallel popularity in several years approx 1990 to 1995. ANSI/ISO suggests Dennis Ritchie to some amendment in C and adds more details in C language. Dennis Ritchie implements such suggestion and release a new version of C in 1999, here C known as C99. Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |4

Today’s C99 widely used for system programming and any other areas of programming. No any amendment made several years in C, nearly 2007 another revision of C standard introduce named “C1X”, but it not gain more popularity.

Point at a glance  C was developed at AT&T Bell lab during 1969 to 1972. At this time Bell lab has a programming language named B (B for Bell). The next language of this lab is C.  C influenced both languages CPL (Common Programming Language), BCPL (Basic Combined programming Language) and B.  ANSI (American National Standard Institute) Standardized C from 1983 to 1989 referred as ANSI Standard C.  In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language. This book, known to C programmers as "K&R"  In 2007, work began in anticipation of another revision of the C standard, informally called "C1X"

About Dennis M. Ritchie and C

Ken Thompson and Dennis M. Ritchie with American President [Fig. Source – Internet]

Dennis Ritchie is a great name in the computer programming. At present C is a very popular language used for programming. By the invention of C language Dennis Ritchie has gone on the top of

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |5

popularity. Dennis Ritchie’s C has proved to be a milestone in the computer field. Today world is developing very fastly and technically in which Dennis Ritchie contribution can’t be forgotten.

What Is C? C language is a general purpose and structured programming language developed by ‘Dennis Ritchie' at AT&T's Bell Laboratories in the 1972s in USA. It is also called as 'Procedure oriented programming language. 'C is not specially designed for specific applications areas like COBOL (Common Business-Oriented Language) or FORTRAN (Formula Translation). It is well suited for business and scientific applications. It has some various features like control structures, looking statements, arrays, macros required for any applications.

Why C is a popular programming language? Any other languages other than C are the languages for a specified work but C gives the programmer the power to do what programmer wants. Use of C gives the ability to achieve the efficiency of assembly language or it can be used in the place of assembly language. C became the universal language of programmers around the world. C’s popularity grew due to its portability and its efficiency. During its creation C was too long for dramatic improvement in programming languages. In few years C has proved that it can be used to perform any task. Now C is popular programming language, possibly say C’s popularity comes to its reliability, simple and easy to use. It is true that C will always be remembered as one of the greatest programming language of its own.

UNIX Kernel

Computer language

PC Application

Combined Batches By Minds Academy and Net Indiana

Embedded System Prog.

Computer Game

Robotics

The Concept Of C Programming

Page |6

Figure shown C has Wide application areas

C is a mid level programming language that consider as high and low level purpose so C is used in many areas like compiler designing, game programming, operating system programming, virus and antivirus making, device driver and other application. Its wide area is an important cause to make it very popular programming language.

Today’s Life with C Just few years ago, C is very popular programming language, today’s also, but in market many language comes to full fill the general requirement. Those are Java, C++, Visual Basic, .Net Packages and many other languages. Today the use of C is dramatically decreased. Rarely a project handle with C (Here project means general purpose PC application).This decade the development of System Software, Database Tools, Graphics Programming, Robotics Programming and many other more, C is widely used. We never think C’s market goes to off. It is always parallel with coming technology because it has tremendous features. UNIX operating system kernel is written in C, dbase is a database tool is also written in C. C is heart of robotics programming.

Why C is called mother language? C is very common language in the world today. Most high level language follow the concept and syntactical way of C like C++, Java, PHP and many other more. Most programmers opinion that C is easily understandable language because less number of keywords and structural concept another helpful statement about C, it is fulfill the requirement of programmer’s need, means programmers that wants do here. I think if you want to red C++, Java directly it not a good decision, firstly know what its base means C, after that more easily understand C++, not only C++, java, PHP and other languages also. C easily clarifies the processing of data and memory organization. C deal the part of actual language object, hence first learn all the language element very thoroughly in C language before reading of C++, Java and others.

Point at a glance  C is a quick language. A well written C program is likely to be as quick as or quicker than a well – written in any other high level languages.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |7

 C is core language. That is to say, a number of common and popular computer language are based in C.  C is very small language. It has only 32 keywords.  C++, Java, PHP, Perl are C based language.

Six step to developing C program      

Define what output you need means come with an idea of program’s output. Analyze process to achieve output. Write source code in your decidable editor and compiler. Compile it. If found error than debug it. (optional) Finally execute the source code and found your desire result.

Hello World as a program The best way to knowing any concept, you go to deal it. Now we start programming in C language and take a popular program code in every language which is “Hello World” program. For writing any C program, we require the component namely an operating system, a text editor and a C compiler. The editor use to write source code and compiler compile the code. Turbo C IDE is very popular IDE is very popular IDE of C programming. In turbo C IDE inbuilt compiler gives ease of write code, compile and run it. Now we take a simple structure of C program to understand the C programming methodology.

Compiling and linking Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |8

The compiler is a special program that reads the instructions stored in the source code file, verified each instruction, and then translates the information into the machine code understood only by the computer’s microprocessor. After translating the source file compiler creates a object code file with extension .obj. this object code file not completely machine code file, but we say it is very nearly to machine code. Compiler is simply a translator that translate source code file and produce object code file. In our source code program many library function be used to performing specific task and the definition of these function found in library files. So, when we use these function in our program the linker combines the object code of standard library function and the compiler produce object file. Finally, the compiler links the object code file, which creates a real, live computer program. If either the compiler or the linker doesn’t understand something, an error message is displayed.

Point at a glance  Compiler is a simple program that translates the source code to object code file.  The object code file has extension .obj or may .o.  Object code file name is the same as the source code filename.

Executing Firstly we save our source code and compile it, in time of compilation not found any error then object file generated, after that we run the program. The running methodology of program depends on your IDE. In turbo C compiler ctrl + F9 is shortcut command for running the program. Otherwise go to run menu and choose run option. The output of the hello.c program is Hello World This is the final output of our hello world program, but this is not displayed so you press another shortcut command (ctrl+F5) to see the output.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Page |9

If you born poor it’s not your mistake but if you die poor it’s your mistake.

Bill gates, Microsoft

2 Starting With C Chapter covered 

What is keyword?



What is variable?



Naming Convention of variable



Validate variable through data type



Qualifier



Modifier



Memory mapping of a C program



Scope of variable



Declaration of variable



Assigning value in variable (Direct and Indirect)

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Combined Batches By Minds Academy and Net Indiana

P a g e | 10

The Concept Of C Programming

P a g e | 11

What is Keyword? A word has specified meaning in its language called keyword. These have predefined used and can’t be used for any other purpose in our C program. Keyword is always written in small case. There are 32 keywords in C language, these are

C Language Keywords auto break case char const continue default Do

Double Else Enum Extern Float For Goto If

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

In C99 three new data type introduces. These are _bool, _complex, _imaginary

What is variable? Variable is named memory location that is used to hold data. It can be utilized to various computations in program. All variable must be providing a data type before it defines. Once the type of variable is defined it can’t be changed. Only its value can be changed in our program for manipulation. In c language a variable must be declared before it use, because compiler notice that how much memory will be need to program execution. Now we consider the data type to validating this variable,

Validate variable through data type In C programming create a variable to hold data, but in time of variable declaration we can’t decide its type. This gives complexity in programming. For ease of programming come a concept to validate variable referred as data type. In C89 defines five basic data type. Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 12

int: define for store integer value may be positive or negative. char: define to store character, symbol that come in general use of programming. float: for storing floating-point value. double: also use to store decimal value but high in number of decimal point value. void: is valueless data type, used as returntype and declaring a generic pointer.

Default declaration range of data type Data type

Storage size in bit

Value range

int

16 bits

-32,768 to +32,767

char

8 bits

-128 to + 127

float

32 bits

3.4 E -38 to 3.4 E +38

double

64 bits

1.7 E -308 to 1.7 E +308

What is MSB and LSB In computing MSB (most significant bit) is high order bit in the allocated bit position. It also says left most bit that notify the greatest value of allocated block (bit). If you allocate N bit in memory. The most significant bit is N – 1 bit.

7

6

5

4

3

2

1

0

MSB (most significant bit) We allocate 8 bits in memory. Now we know that N is 8. So the most significant bit is 8-1 i.e. 7th bit. For better understanding its name and working of MSB most significant bit fill, we 1 above allocated block like .

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

0 7

P a g e | 13

1

1

1

1

1

1

1

6

5

4

3

2

1

0

If you fill 0 in MSB then the value is 127 in decimal system, now change the MSB value as 0 to 1. . 1 1 1 1 1 1 1 1

7

6

5

4

3

2

1

0

The value become 128 + 127 = 255. This is larger change in original value (127). According to its usability it’s named as most significant bit and in binary system it is used represent the sign of number. In computing LSB (least significant bit) is low order bit, come in right first. The least significant bit value determines the value is even or odd, it come from right so it also called right most bit. If you allocate N bit in memory. The most significant bit is N – N bit. Means the right first bit (0th bit) is least significant bit.

7

6

5

4

3

2

1

0

LSB (least significant bit) We allocate 8 bits in memory. Now we know that N is 8. So the least significant bit is 8-8 i.e. 0th bit. For better understanding its name and working of LSB least significant bit, we fill 1 above allocated block like . 0 0 0 0 0 0 0 1

7

6

5

4

3

2

1

0

If you fill 0 in LSB then the value is 128 in decimal system, now change the LSB value as 0 to 1. . 0 0 0 0 0 0 1 1

7

6

5

4

3

2

1

0

The value become 128 + 1 = 129. This is least changing in original value (128). According to its usability it’s named as least significant bit.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 14

Sign bit concept The concept of sing bit in binary number system is given by IEEE (Institute of Electrical and Electronics Engineers). in computing sign bit is MSB (most significant bit) means the value of MSB represent the sign of number. So the MSB is called sign bit. sign bit contain 1 then the number is negative and 0 for positive value.

0/1 7

6

5

4

3

2

1

0

Sign bit Negative binary value Positive binary value Professional Organization Founded January 1, 1963 Location New York City Origins Merger of the American Institute of Electrical Engineers and the Institute of Radio Engineers Key people Moshe Kam, Current President Area served Worldwide Focus Electrical, Electronics, Communications, Computer Engineering, Computer Science and Information Technology Method Industry standards, Conferences, Publications Revenue US$330 million Members 400,000+ Website ieee.org

Integer type The whole number is called integer either positive or negative. C provide a data type int for handle these integer. The size of integer is dependent on the word length of machine. Generally int is 2 byte in size. C99 support 4 byte of integer. When we declare a variable of type int this allocate two consecutive bytes in memory means one byte allocate than 2nd byte of integer variable is allocated and these two byte has own separate address but it identify its first byte address. Default int variable declaration take signed modifier. It give facility to hold either positive or negative value.

Default declared int capacity A. Minimum capacity of int When we declare int type variable it takes 16 bit in memory that shown in figure below.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 15

1I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

I Its left first bit is referred as sing bit other 15 bit be used to hold user inputted data. In other words default declaration int be only 15 bit capacity to hold data. For minimum value of int the integer value be negative means large negative value for this the sign bit contain 1. In sign bit has 1 represent negative value. Other left bit fill by 0 to obtain minimum value.

The minimum capacity of int = 0 x 20 + 0 x 21 + 0 x 22 + 0 x 2 3 + 0 x 24 + 0 x 25 + 0 x 26 + 0 x 27 + 0 x 28 + 0 x 29 + 0 x 210 + 0 x 211 + 0 x 212 + 0 x 213 + 0 x 214 + 1 x 215 = - (0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 32768) = - 32768 Here negative sign come according to sign bit has 1. The minimum capacity of int is – 32768 B. Maximum capacity of int Declare a int variable it occupied 16 bit in memory for maximum capacity of int the larger positive value be hold by int variable. So, the sign bit contain 0 for positive integer and other 15 bit hold 1 for larger positive integer value.

0I 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 15

I

14

13

12

11

10

9

8

7

6

5

4

3

2

1

The maximum capacity of int = 1 x 20 + 1 x 21 + 1 x 22 + 1 x 2 3 + 1 x 24 + 1 x 25 + 1 x 26 + 1 x 27 + 1 x 28 + 1 x 29 + 1 x 210 + 1 x 211 + 1 x 212 + 1 x 213 + 1 x 214 + 0 x 215 = + (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024 + 2048 + 4096 + 8192 + 16384 + 0) = + 32767 Here positive sign come according to sign bit has 0. The maximum capacity of int is + 32767

Character type Combined Batches By Minds Academy and Net Indiana

0

The Concept Of C Programming

P a g e | 16

A charter has its own palace value defined as ASCII code and ASCII code range 0 to 255. Each place value is define for a character say digits 0 – 48 , 1 – 49 alphabets A – 65, B – 66, a – 97 and symbol also, For storing these all value need 8 byte space in memory that hold 0 to 255 ASCII value. A character variable occupied single byte that contains the code for the character. The code is a numeric value depends on the character coding system being used. i.e. it is machine independent. One of these coding systems is ASCII (American standard code for information interchange). We declare a variable of type char it occupied 8 bit (1 byte) in memory.

char c;

0XAAA After allocating memory it’s a unique address. Above char c means allocate one byte memory area and its address is 0XAAA.

Default declared char capacity Default declared char has not a sing bit concept. The size of char 8 it is used for user data storage. If sign bit concept remove in any data type than it hold only a categorized value and in case of char its 8 bits is use to store only positive value.

A. Minimum capacity of char When we declare char type variable it takes 8 bit in memory that shown in figure below.

I I

1

0

0

0

0

0

0

0

7

6

5

4

3

2

1

0

For minimum value of char the sign bit contain 1. Other left bit fill by 0 to obtain minimum value.

The minimum capacity of char = 0 x 20 + 0 x 21 + 0 x 22 + 0 x 2 3 + 0 x 24 + 0 x 25 + 0 x 26 + 1 x 27 = - (0 + 0 + 0 + 0 + 0 + 0 + 0 + 128) = - 128 The minimum capacity of char is – 128

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 17

B. Maximum capacity of char I I

0

1

1

1

1

1

1

1

7

6

5

4

3

2

1

0

For minimum value of char the sign bit contain 1. Other left bit fill by 0 to obtain minimum value.

The minimum capacity of char = 1 x 20 + 1 x 21 + 1 x 22 + 1 x 2 3 + 1 x 24 + 1 x 25 + 1 x 26 + 0 x 27 = + (1 + 2 + 4 + 8 + 16 + 32 + 64 + 0) = +127 The maximum capacity of char is + 127

Floating-Point Value Firstly the floating-point binary storage formats concept given by Intel and all Intel processors used this concept after that it standardized by the IEEE organization:

IEEE Short Real: 32 bits

1 bit for the sign, 8 bits for the exponent, and 23 bits for the mantissa. Also called single precision.

IEEE Long Real: 64 bits

1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa. Also called double precision.

In C language IEEE standard 32 bit storage floating-point value referred as float and IEEE standard 64 bit referred as double. Both float and double have same methodology to storing the decimal value. Before storing floating point value it must be normalized.

Normalizing the floating point value Normalizing a floating point valued is just same as the decimal value, for example, decimal 1234.567 is normalized as 1.234567 x 103 by moving the decimal point so that only one digit appears before the decimal. Similarly, the floating-point binary value 1101.101 is normalized as 1.101101 x 23

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 18

by moving the decimal point 3 positions to the left, and multiplying by 2 3. Here multiplying by 2 because the base of binary number system is 2. Here are some examples of normalizations: Binary Value

Normalized As

1101.101

1.101101

.00101

1.01

1.0001

1.0001

10000011.0

1.0000011

Here all normalized binary value we see that, the digit 1 always appears to the left of the decimal point.

What is mantissa and exponent? It is useful to consider the way decimal floating-point numbers represent their mantissa. Using 3.154 x 105 as an example, the sign is negative, the mantissa is 3.154, and the exponent is 5. The fractional portion of the mantissa is the sum of each digit multiplied by a power of 10:

A binary floating-point number is just same as the decimal floating point representation. For example, in the number +1.11011 x 24, the sign is positive, the mantissa is 1.11011, and the exponent is 4.

How to store floating-point value in type float In C language we declare a variable of type float it take 4 byte (32 bit) in memory. From 32 bit IEEE standard 1 bit is reserved for sign bit means MSB (most significant bit) is sign bit and 8 bit hold exCombined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 19

ponent and left 23 bit for mantissa.

IEEE Bit Representation of floating-point value Before creating IEEE bit representation all floating-point value must in normalized. The standard IEEE 32 bit representation methodology is same in float variable to storing floating-point value. In 32 bit standard IEEE representation the MSB is used as sing bit means 31st bit is represent the sign of the floating value, and the next 8 bit (23 to 30) from MSB is used to store exponent, last 23 bit (from 0 to 22) used to hold the mantissa of floating-point value. Now we take some normalized floating-point binary value to categories mantissa and exponent. Binary Value 1101.101 .00101 1.0001 10000011.0

Normalized As 1.101101 x 23 1.01 x 2-3 1.0001 x 20 1.0000011 x 27

Mantissa 1.101101 1.01 1.0001 1.0000011

Exponent 3 -3 0 7

We can now combine the sign, exponent, and normalized mantissa into the binary IEEE short real representation. Using Figure 1 as a reference, the value 1.101 x 20 is stored as sign = 0 (positive), mantissa = 101, and exponent = 01111111 (the exponent value is added to 127). The "1" to the left of the decimal point is dropped from the mantissa. This ensures that stored exponent is always positive. Binary Value

Normalized As

1101.101 .00101 1.0001 10000011.0

1.101101 x 23 1.01 x 2-3 1.0001 x 20 1.0000011 x 27

Biased Exponent 130 124 127 134

Sign Exponent Mantissa 0 10000010 10110100000000000000000 1 01111100 01000000000000000000000 0 01111111 01000000000000000000000 1 10000110 00111000000000000000000

Float store exponent in 8 bit and mantissa in 23 bit. The valid rage for float is 10-38 to 10+38. But how is such an odd range used? We know that floating-point concept arrange in IEEE representation. Since the exponent of a float in IEEE format is stored positive bias of 127, the smallest positive value that can be stored in

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 20

a float variable is 2-127, which is approximately 1.175 x 10-38. The largest positive value is 2128, which is about 3.14 x 1038. In case of double same storage concept of float but it differ in capacity. Here exponent store in 10 bit and mantissa store 52 and MSB is reserved for sign bit.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 21

Naming Convention of variable C compiler provides wide range character to create variable name, the provided character is alphabet (A to Z) and (a to z), digit (0 to 9) and special character (‘_'). In c language is case sensitive means it is sensitive in case of alphabets like you write a variable name salary and SALARY. C understands two variable salary and SALARY because first variable name is written in lower case and other is writing in uppercase. Now come how to construct variable name for constructing variable name c gives certain rule there are

Point at a glance    

The variable name start with alphabet or underscore not a digit (0 -9). Variable name length just differs along version and Brand of c compiler. In variable name only one special character is used named underscore (‘_ ’). Variable must be start with alphabet (A to Z) and (a to z) or underscore but may be followed by digit.  Variable name never a keyword that are provided by C compiler. Some Example of proper variable name is salary, _salary, _salary2000, _salary_, salary_2000, and any more. In C compiler prevent the identifier name start with digit why? Generally we create a identifier to hold a value and assign to the variable to other variable for Example: int a =10; int b=20; a=b;

// Create a variable a and hold value 10 // Create variable b and hold value 20. // Here assign the value of b to a.

In above case we take identifier name as digit than the program code like. int a = 10; int 9 = 20; a = 9; a = 9 in the line compiler can’t understand that the decimal value 9 assign in a Otherwise 9 variable assign to a. This is not a proper way because 9 be value or variable, it is confusion for compiler and compiler not gives facility to identifier name start with digit.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 22

Variable declaration space Local variable For understanding the space of variable declaration we write a simple C program structure

In above program structure, notice that two categorized variable declaration place, first above and below the function and second is in the function. Variable that declared inside the function is called local variable. Local variable is inside declaration of variable when block is started and it destroyed when the blocks is end. In other words local variables are accessed in their own block not outside of block. Local variable declared within one block is not related to any other declared block means. In C each created block has a memory region that is separated the variable. For better understanding we write a program

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 23

In above program we declare two blocks, for understanding we give its name reggion1 and region2. Region1 is not related to region2 because C compiler gives two separate memory area for this two declared region. Hence we declare two variable have same name a. the name of variable not conflict because it is known in own region (Block) and C compiler give two distinct addresses for those two variables. These all declared inside the block variable is known as local variable here declared variable in region1 and in region2 both are local variable.

Global Variable A variable that declared outside of function is known as global variable. In other word variable declared outside the function like above the function or below the function but not in any block is called global variable. In the following program the variable salary will create outside of block so, salary is global variable and end with end of program. The best way to declaration of global variable is top of program.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 24

In above program notice that the variable salary not declared inside the block. It is declared outside of block, so it’s accessing scope in whole program.

Modify the basic data type C gives four data type that are used in general purpose programming but used want to some amendment in these basic data type. It is happen through the help of modifier. C gives four modifier that are derived in two category these are  Sign modifier  Signed  Unsigned  Size modifier  Long  short sing modifier alter the variable sign of hold data either negative or positive value, size modifier alter size of basic data type either increase or decrease the size of basic data type.

Modifier with int Sign and size all modifier supported int data type when declare a int type variable than it default take sing modifier that say declaration of int variable. sign modifier give authority to take positive or negative value to hold positive or negative value MSB is reserved as sing bit and other left bit is used to store data. It give facility to hold positive and negative value but in other situation, need only store positive value. If int variable take always positive value than the concept of sign bit is not useful because MSB (most significant bit) used as data storage bit. C provides unsigned modifier that hold only positive value means MSB is also used as data storage, result int type capacity increase just double of default holding positive value. Default int hold signed modifier. The capacity of default declaration of int capacity calculated in data type section. Here calculated the int with unsigned modifier. unsigned int salary; above declare variable int with unsigned modifier, unsigned modifier only store positive data value because it free from sign bit concept.

Minimum capacity of unsigned int

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15

14

13

12

11

10

9

8

Combined Batches By Minds Academy and Net Indiana

7

6

5

4

3

2

1

0

The Concept Of C Programming

P a g e | 25

For minimum value of int each bit fill by 0.

The minimum capacity of int = 0 x 20 + 0 x 21 + 0 x 22 + 0 x 2 3 + 0 x 24 + 0 x 25 + 0 x 26 + 0 x 27 + 0 x 28 + 0 x 29 + 0 x 210 + 0 x 211 + 0 x 212 + 0 x 213 + 0 x 214 + 0 x 215 =0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0 =0 The minimum capacity of unsigned int is 0.

Maximum value of unsigned int Fill each bit by 1 to obtain maximum value.

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

The maximum capacity of int = 1 x 20 + 1 x 21 + 1 x 22 + 1 x 2 3 + 1 x 24 + 1 x 25 + 1 x 26 + 1 x 27 + 1 x 28 + 1 x 29 + 1 x 210 + 1 x 211 + 1 x 212 + 1 x 213 + 1 x 214 + 1 x 215 =1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024 + 2048 + 4096 + 8192 + 16384 + 32768 = 65535 The maximum capacity of unsigned int is 65535. This is the maximum capacity of int. int type write signed modifier hold only 32767 possible values but with unsigned modifier hold 65535 approx double. Size modifier short and long change the size of int. first take short modifier with int. In most of dos based computer that work on 16 bit computer. The size of short int is 16 bit, generally the size of short int is 2 byte. In 32 bit machine compiler such as GNUC (gcc) default int is 4 but short byte int 2 byte. So, short modifier used to less data capacity be used, when need of 2 byte capacity of int. Other size modifier long alter the capacity of int, It alter just greater than the capacity of default int. In 16 bit computer long int occupies 32 bit long modifier modify capacity of int either maximum of int or equal to int GNUC ( gcc compiler ) has 32 bit default int and long int also 32 bit capacity.

Modifier with char char type supported only sign modifier, default char take unsigned modifier, means default char take only positive value but C compiler give facility to modify as signed char. We familiar with char as take unsigned modifier, now we take signed modifier with char. unsigned char prevent the negative value because unsigned modifier removes the sign bit concept.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 26

Minimum capacity of signed char

I I

1

0

0

0

0

0

0

0

7

6

5

4

3

2

1

0

For minimum value of char the sign bit contain 1. Other left bit fill by 0 to obtain minimum value.

The minimum capacity of char = 0 x 20 + 0 x 21 + 0 x 22 + 0 x 2 3 + 0 x 24 + 0 x 25 + 0 x 26 + 1 x 27 = - (0 + 0 + 0 + 0 + 0 + 0 + 0 + 128) = - 128 The minimum capacity of char is – 128

Maximum capacity of signed char I

0

1

1

1

1

1

1

1

7

6

5

4

3

2

1

0

I For minimum value of char the sign bit contain 1. Other left bit fill by 0 to obtain minimum value.

The minimum capacity of char = 1 x 20 + 1 x 21 + 1 x 22 + 1 x 2 3 + 1 x 24 + 1 x 25 + 1 x 26 + 0 x 27 = + (1 + 2 + 4 + 8 + 16 + 32 + 64 + 0) = +127 The maximum capacity of char is + 127

Modifier with float In older compiler support long modifier with float, long float has 8 byte in size, similar to double type. Long float just similar in capacity and behavior of double, so its uses are not recommended.

Modifier with double Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 27

The type double support only size modifier and in size modifier only long is supported. Declare a variable of type double with long modifier then the size of double variable is 10 byte.

Capacity of basic data type with modifier char unsigned char signed char int unsigned int signed int short int unsigned short int signed short int long int signed long int float double long double

-128 to 127 0 to 255 -128 to 127 -32768 to 32767 0 to 65535 -32768 to 32767 -32768 to 32767 0 to 65535 -32768 to 32767 -2147483648 to 2147483647 -2147483648 to 2147483647 -3.4E+38 to 3.4E+38 -1.7E+308 to 1.7E+308 -1.7E+308 to 1.7E+308

C program memory mapping A C program compile and go to execution it creates four region of memory. The first region is program code that hold program statement which are go to execute. The second region is global region, all declared global variable stored here. In global region has declared variable has default 0 value. In other words if a variable create in global region compiler automatically store a value in this variable say default value of variable is zero (0). Third region is heap region, in this region’s space is reserved for dynamically allocated space. If a C program runtime allocate some memory area than this allocated area place here. This region gives facility to allocate runtime memory according to need and after use free it. Next and last region of the C program memory area is stack region which is used to allocate local variable that has default garbage value. All declared inside function variable place here. A value in your allocated memory area that is not given by you, it comes by another program that uses this area before your allocation. This unpredictable value is called garbage value for your program.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

Combined Batches By Minds Academy and Net Indiana

P a g e | 28

The Concept Of C Programming

P a g e | 29

“Computing is not about computers anymore. It is about living. . . . We have seen computers move out of giant air-conditioned rooms into closets, then onto desktops, and now into our laps and pockets. But this is not the end. . . . Like a force of nature, the digital age cannot be denied or stopped. . . . The information superhighway may be mostly hype today, but it is an understatement about tomorrow. It will exist beyond people’s wildest predictions. . . . We are not waiting on any invention. It is here. It is now. It is almost genetic in its nature, in that each generation will become more digital than the preceding one.” — Nicholas Negroponte, professor of media technology at MIT

3 Input and output Chapter covered 

History of C language



About Dennis M. Ritchie



What is C?



Why C is a popular programming language?



Today’s life with C



Why C is called mother language?



Hello world as a program



Compiling and linking



Executing

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 30

Mr. Dennis M. Ritchie create a platform independent languages, that has no any concept to depend on any particulars platform each operating system has own Input/output mechanism to handle input and output. Each operating system has own set of instruction provided for input/output. If Dennis M. Ritchie mention input and output concept in the C languages, it create for only one platform, but donnish M. Ritchie wants to create a platform independent languages. Many C developers build its own standard library for input and output. C compiler provided by vendor like, Borland, Dev C, Microsoft and any other he build a standard library to operate input output. These libraries are available in all C compilers. ANSI committee standardized these libraries as standard input/output library. These Input/output functions libraries written in particular operating system dependent like dos base input/output library is not supported in UNIX operating system. This standard library contain function for display output on standard output device named printf() and other function scanf() used for input from standard input device in any operating system but its internal mechanism be charged according to platform.

Basic function for console I/O The basic entity in system for input say keyboard also consider as standard input device and for output say screen (VDU) are called console. C standard provides many input output functions for performing input/output operation and his prototype defined in stdio.h header file. It supported two types of consoled inputting and outputting. First one is none formatted console input/output and second is formatted input/output.

None formatted Input/output. None formatted input/output deals with single input and output as well as string (bunch of character). None formatted means take input from console and implement it. In time of taking input it gives facility to take formatted data and display formatted output according to user need. But other case of none formatted input/output user has not power to display output and take input according to need. It just happens according to C standard.

Reading and writing a single character from console C standard provide getchar() for single character input, which read a character form keyboard and return its ASCII value with echo. getchar() read a character with echo, here echo refers to print inputted character on screen that read from the keyboard. The putchar() function writes a character on screen in the current cursor position. The prototypes (declaration) of these are int getchar (void); //character input int putchar (int); //character output

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 31

getchar() input function reads single unsigned char from the keyboard and return its ASCII value. Value like char_variable = getchar(); Char_variable generally used unsigned char. This is perfect because getchar() reads unsigned char, so it may return larger value that not accommodate in signed char variable. getchar() read a character from input data stream and store it in input buffer but does not release the input stream when the ‘\n’ ( or ) is pressed. After pressing line feed character than it release input data stream and send first inputted character’s ASCII value means it successful than return ASCII value of character. Sometimes gatchar() has occur problem in input. getchar() read a character form keyboard after pressing (‘\n’, ) it returns first character’s ASCII value. getchar() wants to take input from standard input device (keyboard) but user can’t give any character to input and press enter key than getchar() return unpredictable value that not accommodate the Lvalue result unspecified problem occur . If in time of inputting a problem occur than it return EOF (EOF is a micro that define in stdio.h) which is equal to -1. The good practice to Lvalue always declared as int variable that accommodates any larger value. int ch; ch = getchar (); Single character output, putchar() is used. putchar() take int parameter, In above putchar() echo single character in screen, but int parameter has larger value parameter in sense of printing character, generally putchar() take int parameter but it print only low order byte of int means only one byte that similar to unsigned char. putchar() print successfully it return the written character and case of errors return EOF. The following example better illustrate getchar() and putchar(). In the example read a character from keyboard, stored it in a variable and display it on screen. #include #include void main () { Int ch; ch = getchar(); putchar(ch); getch (); }

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 32

In above program getchr() read a character from keyboard, return its ASCII value which is stored in int variable, after that putchar(ch)echo the inputted characters that stored in low order byte of ch. Here a question occurs what is low order byte and how print a character by putchar(); First we declare ch variable of byte (int ch hold two byte in memory and receive a character by getchar(). getchar() returns ASCII value inputted character which is stored in low order byte of ch. ch = getchar (); getchar() read as character ‘A’ it return 65 i.e. 1000001 Low order byte of int means last byte of int. putchar() read these low order byte and print the low order byte of ch i.e. ‘A’.

Alternatives to getchar () getch() and getche() these two additional function that read single character. getch() read a character from keyboard but not echo on screen and it release input stream only one key stroke. To execute next line of program. The declaration of these function are char ch = getch(); getche() read a character from keyboard with echo on screen and return on program for next line execution. Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 33

char ch = getche(); These console input function defined in conio.h header file. The header file name conio refers console input output refers input and output. For better understanding of getch() and getche() taken the following example. #include #include void main () { int

ch;

ch =getch (); putchar (ch); // again take input with getche (); ch = getche (); putchar (ch); getch (); }

getch() read a character from keyboard without echo and return its ASCII value that which is hold in how order byte of ch. Next input character by getche(). getche() just same as getch(), only difference in getche(), it echo inputted character. putch() is another one single character output function, if putch() print character successfully on screen it return printed character. In case found error return EOF. The declaration of putch() is int putch (char); putch() also define in conio.h header file.

Some illustration of single input/output character 1. Write a program in C language to accept a character and display it without any variable. #include #include void main ()

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 34

{ // take input by getchar(); putchar (getchar()); getch (); } 2. Write a program in C language to accept a character and display it without any variable. #include #include void main () { char ch; // Display Message printf(“ Enter a Character: “); ch = getch(); printf(“\n Your Given Charcter is: ”) putchar(ch); getch (); } 3. Write a program in C language to accept a character and display it without any variable. #include #include void main () { char ch; // Display Message printf(“ Enter a Character: “); ch = getche(); printf(“\n Your Given Charcter is: ”) putchar(ch); getch(); }

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 35

Formatted input and output from console Formatted input output gives facility to users that manage and control input and output according requirement. In formatted category library function printf() and scanf() be used for output and input respectively. printf() function display formatted output on standard output device default keyboard and scanf() function read formatted data from standard input device(by default keyboard). These functions have power to take input and output according to basic data type. User has facility to perform input and output in won created data type variable, result the output and input more reliable and ease in understanding.

printf( ):-formatted output printf() responsible to display formatted output, i.e. output under control of user. In other words printf() has a mechanism to take user control string under own syntax. printf() function take parameter from user, according to gives parameter printf() work on defined rule. The prototype of printf() is printf(“const char * control_string”,variable1,variable2,

…);

Or printf(“control_string”); printf() is a standard library function that take argument, printf() take any number of argument, because in prototype syntax ,consider “. . .” i.e. it is responsible to take any number of argument. In parameter control_string has combination of sequence of character that is directly printed on screen and other is format specifier. Format specifier always starts with % character. printf() read control_string, it found only general printable character then simply print on screen but if found “%” character then it understand, user want output from variable according to conversion code. For each format specifier need a compatible variable as argument. printf() has reach in format specifier these are printf() supported conversion code ( format specifier) %a %A %c %d %i %e

Hexadecimal output in the form 0xh.hhhhp+d (C99 only). Hexadecimal output in the form 0Xh.hhhhP+d (C99 only). Character output that are define in ASCII code. decimal integers of signed modifier with int. Signed decimal integers. Floating point value in scientific notation (lowercase e).

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

%E %f %F

%g %G %o %s %u %x %X %p %%

P a g e | 36

Floating point value in scientific notation (uppercase E). Decimal floating point value. Decimal floating point (C99 only; produces uppercase INF, INFINITY, or NAN when applied to infinity or a value that is not a number. The %f specifier produces lowercase equivalents.) Uses %e or %f, whichever is shorter. Uses %E or %F, whichever is shorter. Unsigned octal. String of characters. Unsigned decimal integers. (not display negative value) Unsigned hexadecimal value in lowercase case alphabetical symbol. Unsigned hexadecimal value in upper case alphabetical symbol. Directly display the value as address for pointer variable. Prints a percent sign.

printf() successfully print then it returns number of character that print on screen, othewise in case failure it return –ve value. printf() take first parameter control_string. In time of processing this control string place in memory and printf treat as string and take its starting address than perform operation of printing. prntf() directly work on address in place of control string. printf() working mechanism clear in following program #include #include void main() { clrscr(); printf(“Hello world”); getch(); } Execute this program. printf() function has first parameter control_string contain “Hello world“. Firstly the string “Hello world“ store on primary memory, after than it read the starting address of storage area where store “Hello world“ control_string,it read byte by byte and print on screen till found of string terminater character’\o’. In time of reading control_string found any format specifier than goes on write side and use value of variable that are given by user as parameter of printf(). According to format specifier conversion code, print it on standard output device. Take an example

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 37

It display The character is: A Here, printf() function read control string is time of reading it found ‘%c’ format specifier than it collect compatible variable which is the second argument variable. i.e c, variable c contain a character ‘A’. %c format specifier wants a character type data and it found similar i.e value as character ‘A’ is printed on screen.

Data type operated by printf() Character with printf() C language supports char data type to operate character. Character output printf() function has %c format specifier. #include #include void main() { char c; clrscr(); printf(“Enter a character from Standard Input Device: “); c = getchar(); printf(“Character Display In Standard Output Device: %c”, c); getch(); }

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 38

Integer with printf() Format specifier that support integers formatted output %i, %d, %u, %ld are used to print formatted integer output these specifier has own categories. Generally %d widely used to print integer value, but %d only print signed integer. It is not specify unsigned, long and short, hence a question arise that why, He can’t understand the modifier of int data type? C provides many format specifier that perform formatted output. Each format specifier has own rule define in library file. He can’t understand the modifier and storage specifier of any type variable, for better illustration we take a program. #include #include void main() { //Here assign an unsigned value unsigned int a=52002; clrscr(); printf(“The value of unsigned int a=%u”, a); printf(“\n The value of unsigned int a =%d”, a); getch(); } In both printf() function statement print same variable value i.e. value of a. The output is just different to one other. Here just a concept hit in mind that, the outputted value not directly depends on variable, it directly depend on format specifier. This reason C is give many format specifier for each modified type variable value printing. In detail, formate specifier has own rule and it not depend on type, modifier and storage specifier of variable. For illustration take %d format specifier that always prints signed integer. An unsigned int variable has value 32770. The value of unsigned int variable print by %d modifier that %d check the value of variable found 32770 but %d capacity is maximum 32767. %d format speccifier understand overflow occur but it not consider the unsigned modifier of variable. It only consider own rule. According to rule it notice overflow occur and print overflowed value.

Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 39

#include #include void main() { unsigned int a = 32770; clrscr(); printf(“Display value of unsigned int = %d”,a); getch(); } The output of program is Display value of unsigned int = -32776 Here we notice that variable a contain 32770 in unsigned modifier variable which is easily accommodate by unsigned variable a. to display the value of a with %d format specifier it understand a case of overflow and display negative value. Type int support many modifier, so need many format specifier to print it, l and h are two format specifier modifier that print long and short value respectively. #include #include void main() { signed int signedi =-3200; unsigned int unsignedi =36770; long int longi=55024; short int shorti=2204; clrscr(); printf(“Display value of signed int = printf(“\nDisplay value of signed int printf(“\nDisplay value of signed int printf(“\nDisplay value of signed int getch(); } Output: Display value of singned int=-3200 Display value of unsingned int=367770 Display value of long int=55204 Display value of short int=3204

Combined Batches By Minds Academy and Net Indiana

%d”, signedi); = %u”, unsignedi); = %ld”, longi); = %hd”, shorti);

The Concept Of C Programming

P a g e | 40

Many other combination be happen like %lu nad %hu for unsigned long int and unsigned short int respectively char with printf() formatted output for char type variable %c format specifier be used. format specifier %c also known as conversion code that convert unsigned integer value respective ASCII character symbol. %c take integer value in printf() to in respective place and print character of related ASCII value respective integer value.

If %c has respective argument is int variable than firstly it convert integer value to ASCII equivalent character than print it. Another case %c has respective argument is compatible variable than it perform print variable containing character in two phase. #include #include void main() { char ch = „A‟; clrscr(); printf(“%c”,ch); getch(); } The output is A Compiler execute the printf() statement than it found %c format specifier than it collect respective variable as ch that contain character „A‟, internally variable ch contain ASCII equivalent of „A‟ is 65. %c format specifier has value 65 as integer to covert respective ASCII equivalent character and prints it. char type has only one format specifier supported by C language. It also not support and data type modifier equivalent conversion code specifier, because char type variable stored signed and unsigned value not a long and short type modified value so it has not need to support any other type conversion code specifier. One error arise here that char type variable support two type modifier signed and unsigned but only one conversion code %c is used for unsigned char. For signed modifier Combined Batches By Minds Academy and Net Indiana

The Concept Of C Programming

P a g e | 41

not any conversion code given in C language because char data type formed for only handle ASCII equivalent character and 256 ASCII character that accommodate in only unsigned char. So, %c directly handles only unsigned char.

#include #include void main() { char ch = „A‟; clrscr(); printf(“%c”,65); printf(“%c”,ch); getch(); }

// one phase processing // two phase processing

Combined Batches By Minds Academy and Net Indiana

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF