Learn C Programming Step by Step

April 3, 2017 | Author: arvinvit | Category: N/A
Share Embed Donate


Short Description

C Programming Step by Step...

Description

LEARN C PROGRAMMING STEP BY STEP -Gopal Raha Many people are really interested in learning and implementing C programs on their favorite platforms like DOS/Windows or Linux. If you are the one looking for a step-by-step guide to get started, this tutorial is for you.

Page |1

LEARN C PROGRAMMING STEP BY STEP -Gopal Raha

Page |2

Dedicated to My Dad,Mom and Gore Sir, Who inspire me to write this book.

Page |3

Preface What is a programming language?--Human beings need language to communicate between them. Similarly to communicate with computers we need a programming language. All the events in life can be communicated to computers with the help of programming language. C is the basic of all programming language. Before the introduction of C we had only Machine level languages. C is just like any other communication Language. If you learn C programming thoroughly you can easily learn other programming languages. To learn C we just need to know the syntax of C program only. i.e order in which we have to write the contents.This book is intended to introduce C programming to beginning programmers. Over several years of studying, I have noted a large number of students appear to have difficulty in grasping the fundamentals of C Language. So I am trying to explain them with the help of simple examples. This book will help you get a quick start with C programming. This book is a guide for the beginners and also for those who have little knowledge on C Programming fundamentals. There are different types of programming languages such as Structure oriented, Object oriented etc. C is the most commonly used Structure oriented programming language. Compiler for some languages are developed using C only. With the help of this tutorial I am trying to explain all the basics of C.In order to successfully complete this tutorial, you won't need any prior knowledge of the C programming language. First of all I will start with the most basic concepts and take you up to the highest level of C programming including the usually intimidating concepts of structures, union,file, pointers and dynamic memory allocation,etc. Once you have mastered C, you will find Object Oriented Language such as c++,java, etc are easy to learn. So let's start our Journey......... -AUTHOR

Page |4

Contents 1. Introduction

5

2. Turbo C++ IDE

6

3. A Basic C Program

8

4. Fundamentals

12

5. Input/Output Functions

37

6. Control Statements

44

7. Arrays

58

8. Strings

65

9. Functions

92

10. Storage Classes

98

11. Structure

102

12. Union

107

13. Pointers

110

14. Dynamic memory allocation

114

15. File

126

16. Graphics

138

17. The C Preprocessor

183

18. Standard Library Functions

185

19. Examples

187

20. Reference

191

Page |5

Introduction To C Programming Language The C language was invented at Bell Labs by Dennis Ritchie in 1972 to allow the writing of the UNIX operating system, then developed by Ken Thompson and Dennis Ritchie. The UNIX operating system, born in the late 1960s – early 1970s, was written directly in assembly language for the machines which it was intended. Born in the late 1960s – early 1970s, was written directly in assembly language for the machines which it was intended. If the assembly language for writing such a system, it was nevertheless not easy to use. Such language is in fact a particular type of processor, which makes the whole system should be rewritten to make it work on a new architecture. This is its main creator, Ken Thompson, wanted to use a language more quickly evolved to rewrite UNIX. Among the languages available at the time, BCPL (Basic Combined Programming Language for, which is a simplification of CPL), created by Martin Richards in 1966, was interesting. Without going into detailed descriptions, BCPL is a simple language, procedural, and not typed. Its simplicity makes it easy to create BCPL compilers on machines of the time, when resources were very limited (the first computer used by Keith Thompson was to launch a Unix PDP-7, which had a memory of 4000 words 18 bits, or less than 9 KB). Ken Thompson has evolved to design the B language, which he implemented the first UNIX machines. However, some limitations of language B were UNIX could be rewritten in this language. From 1971, Dennis Ritchie B did evolve to address these issues. Like the programmers increment versions of their programs, Ritchie “Nudge” the letter B to call the new language C. This evolution is “stabilized” to 1973, from which UNIX and UNIX system utilities have been rewritten successfully in C. Subsequently in 1978, Brian W. Kernighan documentation very active language, and finally publish the book with reference Ritchie The C Programming Language. Often called K & R C language as specified in the first edition of this book. In the years that followed, the C language was carried on many other machines. These ports were often made at the beginning, from the pcc compiler Steve Johnson, but then the original compilers were developed independently. During these years, every C compiler was written in accordance with the specifications of K & R, but some added extensions, such as data types or additional features, or different interpretations of certain parts of the book (not necessarily very precise). Because of this, it was less easy to write C programs that can run unchanged on many architectures.

Page |6

Turbo C++ Environment

Integrated

Development

IDE is nothing but Integrated Development Environment in which one can develop, run, test and debug the application. The Turbo C++ IDE appears as shown in figure.

The C Developing Environment is a screen display with windows and pull-down menus. The program listing, error messages and other information are displayed in separate windows. The menus may be used to invoke all the operations necessary to develop the program, including editing, compiling, linking, and debugging and program execution. If the menu bar is inactive, it may be invoked by pressing the [F10] function key. To select different menu, move the highlight left or right with cursor (arrow) keys. You can also revoke the selection by pressing the key combination for the specific menu. Invoking the Turbo C IDE The default directory of Turbo C compiler is c:\tc\bin. So to invoke the IDE from the windows you need to double click the TC icon in the directory c:\tc\bin.

The alternate approach is that we can make a shortcut of tc.exe on the desktop. Opening New Window in Turbo C To type a program, you need to open an Edit Window. For this, open file menu and click “new”. A window will appear on the screen where the program may be typed. Writing a Program in Turbo C

Page |7

When the Edit window is active, the program may be typed. Use the certain key combinations to perform specific edit functions. Saving a Program in Turbo C To save the program, select save command from the file menu. This function can also be performed by pressing the [F2] button. A dialog box will appear asking for the path and name of the file.Provide an appropriate and unique file name. You can save the program after compiling too but saving it before compilation is more appropriate. Making an Executable File in Turbo C The source file is required to be turned into an executable file. This is called “Making” of the .exe file. The steps required to create an executable file are: 1. Create a source file, with a .c extension. 2. Compile the source code into a file with the .obj extension. 3. Link your .obj file with any needed libraries to produce an executable program All the above steps can be done by using Run option from the menu bar or using key combination Ctrl+F9 (By this linking & compiling is done in one step). Compiling and linking in the Turbo C IDE In the Turbo C IDE, compiling and linking can be performed together in one step. There are two ways to do this: you can select Make EXE from the compile menu, or you can press the [F9] key Correcting Errors in Turbo C If the compiler recognizes some error, it will let you know through the Compiler window. You’ll see that the number of errors is not listed as 0, and the word “Error” appears instead of the word “Success” at the bottom of the window. The errors are to be removed by returning to the edit window. Usually these errors are a result of a typing mistake. The compiler will not only tell you what you did wrong, they’ll point you to the exact place in your code where you made the mistake. Executing a Programs in Turbo C If the program is compiled and linked without errors, the program is executed by selecting Run from the Run Menu or by pressing the [Ctrl+F9] key combination. Exiting Turbo C IDE An Edit window may be closed in a number of different ways. You can click on the small square in the upper left corner, you can select close from the window menu, or you can press the Alt+F3 combination. To exit from the IDE, select Exit from the File Menu or press Alt+X Combination.

Page |8

A Simple C Program Every C program must have one special function main (). This is the point where execution begins when the program is running. We will see later that this does not have to be the first statement in the program, but it must exist as the entry point. The group of statements defining the main () enclosed in a pair of braces ({}) are executed sequentially. Each expression statement must end with a semicolon. The closing brace of the main function signals the end of the program. The main function can be located anywhere in the program but the general practice is to place it as the first function. Here is an elementary C program. Main () { } There is no way to simplify this program, or to leave anything out. Unluckily, the program does not do anything. Following the “main” program name is a couple of parentheses, which are an indication to the compiler that this is a function. The 2 curly brackets { }, properly called braces, are used to specify the limits of the program itself. The actual program statements go between the 2 braces and in this case, there are no statements because the program does absolutely nothing. You will be able to compile and run this program, but since it has no executable statements, it does nothing. Keep in mind however, that it is a legal C program. Main ( ) function should return zero or one.Turbo C accepts both int and void main ( ) and Turbo C coders use both int and void main ( ) in their programs.But in my belief, void main ( ) is not a standard usage. The reason is, whenever a program gets executed it returns an integer to the OS. If it returns ‘zero’, the program is executed successfully. Otherwise it means the program has been ended with error. So main ( ) shouldn’t be declared as void.d as void. Main( ) should be declared as int main( ) { …………….. …………….. return 0 ; } .For a much more interesting program, load the program

Page |9

int main () { printf (“Welcome to C language”); return 0; } and display it on your monitor. It is same as the previous program except that it has one executable statement between the braces. The executable statement is another function. Again, we won’t care about what a function is, but only how to use this one. In order to output text to the monitor, it’s placed within the function parentheses and bounded by quotes. The end result is that whatever is included between the quotes will be showed on the monitor when the program is run. Notice the semi-colon; at the end of the line. C uses a semi-colon as a statement terminator, so semi-colon is required as a signal to the compiler that this line is complete. This program is also executable, so you’ll be able to compile and run it to see if it does what you think it should. With some compilers, you may get an error message while compiling, indicating that the function printf () should have a prototype. #include #include int main () { printf (“Welcome to C language”); return 0; } Here you’ll be able to see #include at the beginning of the program. It is a preprocessor directive. It’s not a part of our program; it’s an instruction to the compiler to make it do something. It says the C compiler to include the contents of a file, in this case the system file stdio.h. This is the name of the standard library definition file for all Standard Input Output. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. Stdio.h is the name of the file in which the functions that we want to use are defined. A function is simply a group of related statements that we can use later. Here the function we used is printf . To use printf correctly C needs to know what it looks like, i.e. what things it can work on and what value it returns. The actual code which performs the printf will be tied in later by the linker. Note that without the definition of what printf looks like the compiler makes a guess when it sees the use of it. This can lead to the call failing when the program runs, a common cause of programs crashing.

P a g e | 10

The characters around the name tell C to look in the system area for the file stdio.h. If I had given the name “abc.h” instead it would tell the compiler to look in the current directory. This means that I can arrange libraries of my own routines and use them in my programs. Imagine you run above program and then change it and run it again you may find that the previous output is still stucked there itself, at this time clrscr(); would clear the previous screen. One more thing to remember while using clrscr() is that it should be called only after the variable declaration, like int p,q,r; clrscr() Here is an example of minimal C program that displays the string Welcome to C language (this famous example included in all languages moreover been done originally in C in 1978 from the creators of the language, Brian Kernighan and Dennis Ritchie) Example #include #include int main () { clrscr(); printf (“Welcome to C language”); return 0; } When you execute above program you won’t see ‘Welcome to C language’ on the console because the screen will just flash and go away .If you want to see the line you can use getch() function just below the printf() statement. Actually it waits until a key is pressed. Example:

P a g e | 11

P a g e | 12

Fundamentals 1. Identifiers Identifiers are the names that are given to various program elements such as variables, symbolic constants and functions.Variable or function identifier that is called a symbolic constant name. Identifier can be freely named, the following restrictions.    

Alphanumeric characters ( a ~ z , A~Z , 0~9 ) and half underscore ( _ ) can only be used. The first character of the first contain letters ( a ~ z , A~Z ) or half underscore ( _ ) can only be used. Case is distinguishable. That is, word and WORD is recognized as a separate identifier. Reserved words are not allowed. However, part of an identifier reserved words can be included.

Here are the rules you need to know: 1. Identifier name must be a sequence of letter and digits, and must begin with a letter. 2.

The underscore character (‘_’) is considered as letter.

3.

Names shouldn't be a keyword (such as int , float, if ,break, for etc)

4. Both upper-case letter and lower-case letter characters are allowed. However, they're not interchangeable. 5.

No identifier may be keyword.

6. No special characters, such as semicolon,period,blank space, slash or comma are permitted Examples of legal and illegal identifiers follow, first some legal identifiers: float _number; float a; int this_is_a_very_detailed_name_for_an_identifier; The following are illegal (it's your job to recognize why): float :e; float for;

P a g e | 13

float 9PI; float .3.14; float 7g; Example :

P a g e | 14

2. Keywords Keywords are standard identifiers that have standard predefined meaning in C. Keywords are all lowercase, since uppercase and lowercase characters are not equivalent it's possible to utilize an uppercase keyword as an identifier but it's not a good programming practice. Points to remember 1. Keywords can be used only for their intended purpose. 2. Keywords can't be used as programmer defined identifier. 3. The keywords can't be used as names for variables. The standard keywords are given below:

P a g e | 15

3. Data Types C offers a standard, minimal set of basic data types. Sometimes these are called "primitive" types. A lot of complex data structures can be developed from these basic data types. The C language defines 4 fundamental data types: character integer floating-point and double floating-point This data types are declared using the keywords char,int,float and double respectively. Typical memory requirements of the basic data types are given below

The size and range of these data types may vary among processor types and compilers. Data type qualifiers modify the behavior of variable type to which they are applied. Data type qualifiers can be classified into two types.two types. 1. size qualifiers 2. sign qualifiers Size qualifiers: Size qualifiers alter the size of the basic data types. There are two size qualifiers that can be applied to integer: short and long. The minimum size of short int is 16 bit. The size of int must be greater than or equal to that of a short int. The size of long int must be greater than or equal to a short int. The minimum size of a long int is 32 bits. Sign qualifiers: The keywords signed and unsigned are the two sign qualifiers that specify whether a variable can hold both –ve and +ve numbers, or only +ve numbers. These qualifiers can be applied to the data types int and char only. Example: unsigned int I; The following table shows all valid data type combinations supported by C, along with their minimal ranges and typical memory size.

P a g e | 16

P a g e | 17

4. int An integer is a whole number (a number without a fractional part). It can be positive or negative numbers like 1, -2, 3, etc., or zero. The sizes of the integer variables depend on the hardware and operating system of the computer. On a typical 16-bit system, the sizes of the integer types are as follows. TYPE

Bytes

Possible Values le Values

int

2 or 4

-32,767 to 32,767

unsigned int

2 or 4

0 to 65,535

signed int

2 or 4

-32,767 to 32,767

short int

2

-32,767 to 32,767

unsigned short int

2

0 to 65,535

signed short int

2

-32,767 to 32,767

long int

4

-2,147,483,647 to 2,147,483,647

signed long int

4

-2,147,483,647 to 2,147,483,647

unsigned long int

4

0 to 4,294,967,295

Example:

P a g e | 18

P a g e | 19

5. char char is a special integer type designed for storing single characters. The integer value of a char corresponds to an ASCII character. E.g., a value of 65 corresponds to the letter A, 66 corresponds to B, 67 to C, and so on. As in the table below, unsigned char permits values from 0 to 255, and signed char permits values from -127 (or -128) to 127. The char type is signed by default on some computers, but unsigned on others the sizes of the char types are as follows. Type

Bytes

char

1

Minimal range -127 to 127

unsigned char

1

0 to 255

signed char

1

-127 to 127

Example:

P a g e | 20

6. Float Floating point numbers are numbers with a decimal point. The float type can take large floating point numbers with a small degree of precision (Precision is simply the number of decimal places to which a number can be calculated with accuracy. If a number can be calculated to three decimal places, it is said to have three significant digits.) Memory size : 4 bytes Minimal range : IE-38 to IE+38 with six digit of precision Example:

P a g e | 21

7. Double Double-precision floating point numbers are also numbers with a decimal point. We know that the float type can take large floating point numbers with a small degree of precision but the double-precision double type can hold even larger numbers with a higher degree of precision. The sizes of the double types are as follows. Type

Bytes

doubleprecision 8 long double Example:

8

Minimal range IE-38 to IE+38 with 10 digit of precision IE-38 to IE+38 with 10 digit of precision

P a g e | 22

8. Constants The term constant means that it does not change during the execution of program. There are four basic types of constants in C. They are: 1.Integer constants 2.Floating-point constants 3.Character constants 4.String constants Integer and floating-point constants represent numbers. They are often referred to as numeric-type constants. The following rule applies to all numeric type constants:   

Comma and blank spaces cannot be included within the constants. Constants can be preceded by a – or + sign, if desired. If either sign does not precede the constant it is assumed to be positive. The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from one C compiler to another.

P a g e | 23

9. Integer constants Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits. Integer constants can be written in three different number systems: Decimal, Octal and Hexadecimal. A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants. 0

1

1234

-786

Example:

The following decimal integer constants are written incorrectly for the reasons stated. 12,001 103.0 10 20 30 045 Example:

Illegal character (,). Illegal decimal point (.). Illegal character (blank space). First digit cannot be zero.

P a g e | 24

An octal integer constant can consist any combination of digits taken from the set 0 through 7. However, the first digit must be 0, in order to identify the constant as an octal number. The following are valid octal integer constants. 0

01

0125

0555

Example :

The following decimal integer constants are written incorrectly for the reasons stated. 2010 2008 05.10

Does not begin with 0. Illegal digit (8). Illegal character (.).

P a g e | 25

A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lower-case). The following are valid hexadecimal integer constants. 0X0

0x1

0XAB125

-0x555

Example:

The following hexadecimal integer constants are written incorrectly for the reasons stated. 0x20.10 02008 0xabcdefg

Illegal character (.). Does not begins with 0x or 0X. Illegal character (g).

P a g e | 26

10.

Floating-point constants

A floating-point constant is a base-10 number that contains either a decimal point or an exponent or both. A floating-point constant can be written in two forms: Factorial form or Exponential form. A floating-point constant in a fractional form must have at least one digit each to the left and right of the decimal point. A floating-point in exponent form consists of a mantissa and an exponent. The mantissa itself is represented as a decimal integer constant or a decimal floating-point constant in fractional form. The letter E or e and the exponent follow the mantissa. The exponent must be a decimal integer. The actual number of digits in the mantissa and the exponent depends on the computer being used. The following are valid floating-point constants.
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF