Example: barber

LECTURE NOTES ON PROGRAMMING IN C

LECTURE NOTES ON PROGRAMMING IN C Revision 1 December, 2014 L. V. NARASIMHA PRASAD Professor Department of Computer Science and Engineering E. KRISHNA RAO PATRO Associate Professor Department of Computer Science and Engineering INSTITUTE OF AERONAUTICAL ENGINEERING DUNDIGAL 500 043, HYDERABAD 2014-2015 CONTENTS The Condensed C Chapter-1: An Overview of C Quick History of C The Compile Process Guide lines on Structure Flow charting General Flow charting Guidelines Algorithm Using the Turbo C/C++ Editor Moving, Deleting and Copying Blocks of Text. Deleting Characters, Words and Lines. Cursor Movement Moving Blocks of Text to and from Disk files Information Representation Characteristics of C Compiling and executing C program Keywords in C Simple C program structure Fundamental/Primary DataTypes Identify Names (Declaring Variables) Declaring variables Global Variables Local variables Manifest Constants Constants Escape Sequences Operators Arithmetic Operators Relational Operators Logical operators Bit wise operators Conditional Operators Size of Operator Special operators.

LECTURE NOTES ON PROGRAMMING IN C Revision 3.0 1 December, 2014 L. V. NARASIMHA PRASAD Professor Department of Computer Science and Engineering E. KRISHNA RAO PATRO Associate Professor Department of Computer Science and Engineering INSTITUTE OF AERONAUTICAL ENGINEERING DUNDIGAL – 500 043, HYDERABAD 2014-2015

Tags:

  Notes, Programming

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of LECTURE NOTES ON PROGRAMMING IN C

1 LECTURE NOTES ON PROGRAMMING IN C Revision 1 December, 2014 L. V. NARASIMHA PRASAD Professor Department of Computer Science and Engineering E. KRISHNA RAO PATRO Associate Professor Department of Computer Science and Engineering INSTITUTE OF AERONAUTICAL ENGINEERING DUNDIGAL 500 043, HYDERABAD 2014-2015 CONTENTS The Condensed C Chapter-1: An Overview of C Quick History of C The Compile Process Guide lines on Structure Flow charting General Flow charting Guidelines Algorithm Using the Turbo C/C++ Editor Moving, Deleting and Copying Blocks of Text. Deleting Characters, Words and Lines. Cursor Movement Moving Blocks of Text to and from Disk files Information Representation Characteristics of C Compiling and executing C program Keywords in C Simple C program structure Fundamental/Primary DataTypes Identify Names (Declaring Variables) Declaring variables Global Variables Local variables Manifest Constants Constants Escape Sequences Operators Arithmetic Operators Relational Operators Logical operators Bit wise operators Conditional Operators Size of Operator Special operators.

2 Order of precedence of C operators Pair Matching Casting between types Console I /O Reading and writing characters Reading and Writing strings Reading character data in a C program Formatted Console I /O Displaying Prompts Carriage Returns Conversion Specifies Output field width and Rounding scanf () Function Chapter-2: Control Statements, Arrays and Strings Control statements Conditional statements If Statement If Else Statement Nested if Statement If-else-if Ladder The ? : Operator (ternary) The switch Case Statement Looping and Iteration The for Statement Nested for loop Infinite for loop for with no bodies The while statement The do-while statement Un-conditional (Jump) statements return statement goto statement break statement continue statement exit () function Arrays Strings Basic String Handling Functions Character conversion and testing Character conversion Chapter-3.

3 Storage Classes, Functions and User Defined Data types Storage Classes The auto storage class The static storage class The storage external class The register storage class The Type Qualifiers, const and volatile const storage class Volatile storage class Function Calling Function Defining functions Function declaration Function arguments/parameters Call by value Call by reference Category of functions Function with no arguments and no return value Function with arguments and no return value Function with no arguments and but return value Function with arguments and return value Important points to be noted while calling a function Nested function Local and global variables Calling functions with arrays The return statement User Defined Data types Structures Typedef Unions Enumerations Bit Field Chapter-4.

4 Pointer, files command line arguments and pre processor Pointer Pointers Pointers and strings Pointers and structures Pointers and functions Pointer as function arguments Pointer to function Arrays of pointers Multi-dimensional arrays and pointers Pointer to pointer Illegal indirection Dynamic memory allocation and dynamic structure files Strings File input and output functions File pointer Opening a file Closing a file Writing a character Reading a character Using feof() Working with strings- fputs() and fgets() rewind () ferror (() erasing files flushing a string fread() and fwrite () fseek () and randam access-i/o fprint() and f scanf() The standard streams Command line arguments Example programs on file i/o and command line arguments The C Preprocessor Conditional compilations The # and ## preprocessor operator Mathematics Character conversion and testing Memory operators String searching Sample programs on pointers 1 The Condensed C The aim of this quick reference guide is to give a well rounded overview of what a beginner to C needs to know without having to trek through reams of NOTES , or through several beginner's C books.)

5 Therefore, it gives simple examples of most basic concepts in C to give useful templates from which to base your own programs. Hope it works for you! L. V. Narasimha Prasad E. K. R. Patro The Condensed C 2 Quick History of C Developed by Bell Laboratories in the early seventies Borne of two other languages BCPL and B. C introduced such things as character types, floating point arithmetic, structures, the preprocessor, and portability. The principle objective was to devise a language that was easy enough to understand to be "high-level" understood by humans, but low-level enough to be applicable to the writing of systems-level software. The language should abstract the details of how the computer achieves its tasks in such a way as to ensure that C could be portable across different types of computers, thus allowing the Unix operating system to be compiled on other computers with a minimum of re-writing.

6 C as a language was in use by 1973, although extra functionality, such as new types, were introduced up until 1980. In 1978, Brian Kernighan and Dennis M. Ritchie wrote the seminal work The C PROGRAMMING Language, which is now the standard reference book for C. A formal ANSI standard for C was produced in 1989. In 1986, a descendant of C, called C++ was developed by Bjarne Stroustrup, which is in wide use today. Many modern languages such as C#, Java and Perl are based on C and C++. The Compile Process You type in your program as C source code, compile it (or try to run it from your PROGRAMMING editor, which will typically compile and build automatically) to turn it into object code, and then build the program using a linker program to create an executable program that contains instructions written in machine code, which can be understood and run on the computer that you are working on.

7 To compile a C program that you have typed in using C++ Editor, do one of the following: Click on the menu and choose the Compile menu item Hold down the Ctrl key and press F9 (this actually Makes the program) A Basic C Program #include < > #include < > void main() { printf( "Hello World!\n" ); getch(); } The Condensed C 3 This program does the following: First two lines take the two library files and (which contain various functions to perform tasks relating to putting data into and getting data out of the computer) and insert their contents into the C program at the point where they are seen (at the top of the program). This makes all these functions available to use in the program. The void main() line starts a new function, where void means that the function returns no value, and main() means that this function is the starting point when the program first executes.

8 All of the instructions inside the main() function appear between an opening and closing curly bracket a { and } this shows where the function (and therefore, the program) begins and ends. The first instruction uses a f unction found in called printf. This displays information to the black console window. Everything the brackets is what gets fed into the printf function the input. In C, stored text is distinguished from normal C PROGRAMMING instructions by putting double-quotes around the text "like this". So we could say printf("gets();"); and the phrase gets(); would appear on the console window, where as printf(gets()); would wait for the user to type a string (the gets() function) and the results of this would be output to the screen. So the printf function displays Hello World!

9 To the screen. The \n is a character constant which is interpreted as a new-line. the cursor moves to the next line down and to the left hand margin. The getch(); function waits until the user presses any single key on the keyboard. It is held in the library. The purpose of this is to keep the black console on the screen so we can see the results, and when a key is pressed, the program execution continues. As there is nothing left in the program, the program terminates, and the black console window disappears. Without the getch(); function, the answer would f lash up, the program would finish, and the console screen would disappear again so quickly that we would not have a chance to see what happened in the program! Guidelines on Structure When creating your programs, here's a few guidelines for getting a program that can be easily read and debugged by you (when you come back to it later) and others (who may have to change your program later).

10 A well-structured program should indent ( move to the right by pressing the tab key once) all instructions inside each block ( following an opening curly brace {). Therefore, a block inside another block will be indented twice etc. This helps to show which instructions will be executed, and also helps to line up opening and closing curly braces. It is often helpful to separate stages of a program by putting an extra blank line (and maybe a comment explaining what the next stage does) between each stage. Another useful technique that helps the readability inside each instruction is to put spaces after commas when listing parameters ( data) to be given to a function. This helps identify where each parameter starts and The Condensed C 4 makes the program easier to read.}


Related search queries