×
Listen to your teachers when they tell you WHAT to do. But more importantly, think about it later and ask yourself WHY they told you to do it.
--Your friends at LectureNotes
Close

Programming in C

by Exam Branch
Type: NoteSpecialization: Computer Science EngineeringViews: 31Uploaded: 10 months agoAdd to Favourite

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Exam Branch
Exam Branch
CHAPTER 1 INTRODUCTION 1.1 ORIGIN OF C 1.2 THE “HELLO WORLD” PROGRAM 1.3 THE C PROGRAMMING ENVIRONMENT CHAPTER 2 VARIABLES, DATA TYPES, I/O AND OPERATORS 2.1 BASIC DATA TYPES 2.2 VARIABLES 2.3 CONSOLE INPUT / OUTPUT 2.4 OPERATORS 2.5 TYPE OVERFLOW & UNDERFLOW 2.6 EXERCISES CHAPTER 3 STATEMENTS 3.1 EXPRESSIONS AND STATEMENTS 3.2 ITERATION STATEMENTS 3.3 DECISION STATEMENTS 3.4 EFFICIENCY CONSIDERATIONS 3.5 EXERCISES CHAPTER 4 FUNCTIONS 4.1 FUNCTION PROTOTYPE ( DECLARATION) 4.2 FUNCTION DEFINITION & LOCAL VARIABLES 4.3 SCOPE RULES 4.4 RETURNING A VALUE 4.5 FUNCTION ARGUMENTS 4.6 RECURSION 4.7 #DEFINE DIRECTIVE 4.8 EFFICIENCY CONSIDERATIONS 4.9 EXERCISES CHAPTER 5 ARRAYS & STRINGS 5.1 SINGLE DIMENSION ARRAYS 5.2 STRINGS 5.3 MULTIDIMENSIONAL ARRAYS 5.4 ARRAYS OF STRINGS 5.5 ARRAYS AS ARGUMENTS TO FUNCTIONS ( 1D ) 5.6 PASSING MULTIDIMENSIONAL ARRAYS 3 3 3 4 5 6 6 6 7 9 12 20 20 22 22 22 23 28 32 35 37 37 37 38 39 39 40 42 43 45 46 48 48 48 49 51 52 53 55 6th Revision - 20/11/2017
5.7 EXERCISES 57 CHAPTER 6 60 POINTERS 6.1 POINTER VARIABLES 6.2 POINTER OPERATORS * AND & 6.3 CALL BY REFERENCE 6.4 POINTERS AND ARRAYS 6.5 POINTER ARITHMETIC 6.6 ARRAYS OF POINTERS 6.7 COMMAND LINE ARGUMENTS 6.8 DYNAMIC MEMORY ALLOCATION 6.9 MULTIPLE INDIRECTION -- POINTERS TO POINTERS 6.10 POINTERS TO FUNCTIONS 6.11 EFFICIENCY CONSIDERATIONS 6.12 EXERCISES CHAPTER 7 60 60 61 62 63 63 66 67 68 69 72 73 75 81 STRUCTURES & UNIONS 7.1 STRUCTURES 7.2 BIT--FIELDS 7.3 UNIONS 7.4 ENUMERATIONS 7.5 THE TYPEDEF KEYWORD 7.6 LINKED LISTS 7.7 EFFICIENCY CONSIDERATIONS 7.8 EXERCISES 81 81 85 86 87 87 88 93 94 CHAPTER 8 95 STANDARD FILE I/O 8.1 STREAM I/O 8.2 LOW -- LEVEL I/O 8.3 EXERCISES 95 95 101 103 APPENDIX A : ASCII CHARACTER SET 105 2
Chapter 1 Introduction 1.1 Origin of C The C Programming Language was initially developed by Denis Ritchie using a Unix system in 1972. This was varied and modified until a standard was defined by Brian Kernighan and Dennis Ritchie in 1978 in "The C Programming Language". By the early 80's many versions of C were available which were inconsistent with each other in many aspects. This led to a standard being defined by ANSI in 1983. It is this standard this set of notes primarily addresses. Why use C ? Industry Presence : Over the last decade C has become one of the most widely used development languages in the software industry. Its importance is not entirely derived from its use as a primary development language but also because of its use as an interface language to some of the newer “visual” languages and of course because of its relationship with C++. Middle Level : Being a Middle level language it combines elements of high level languages with the functionality of assembly language. C supports data types and operations on data types in much the same way as higher level languages as well as allowing direct manipulation of bits, bytes, words and addresses as is possible with low level languages. Portability : With the availability of compilers for almost all operating systems and hardware platforms it is easy to write code on one system which can be easily ported to another as long as a few simple guidelines are followed. Flexibility : Supporting its position as the mainstream development language C can be interfaced readily to other programming languages. Malleable : C, unlike some other languages, offers little restriction to the programmer with regard to data types -- one type may be coerced to another type as the situation dictates. However this feature can lead to sloppy coding unless the programmer is fully aware of what rules are being bent and why. Speed : The availability of various optimising compilers allow extremely efficient code to be generated automatically. 3
1.2 The “Hello World” Program A C program consists of one or more functions or code modules. These are essentially groups of instructions that are to be executed as a unit in a given order and that can be referenced by a unique name. Each C program must contain a main() function. This is the first function called when the program starts to run. Note that while "main" is not a C keyword and hence not reserved it should be used only in this context. A C program is traditionally arranged in the following order but not strictly as a rule. Function prototypes and global data declarations The main() function Function definitions Consider first a simple C program which simply prints a line of text to the computer screen. This is traditionally the first C program you will see and is commonly called the “Hello World” program for obvious reasons. #include <stdio.h> void main() { /* This is how comments are implemented in C to comment out a block of text */ // or like this for a single line comment printf( "Hello World\n" ) ; } As you can see this program consists of just one function the mandatory main function. The parentheses, ( ), after the word main indicate a function while the curly braces, { }, are used to denote a block of code -- in this case the sequence of instructions that make up the function. Comments are contained within a /* ... */ pair in the case of a block comment or a double forward slash, //, may be used to comment out the remains of a single line of test. The line printf("Hello World\n " ) ; is the only C statement in the program and must be terminated by a semi-colon. The statement calls a function called printf which causes its argument, the string of text within the quotation marks, to be printed to the screen. The characters \n are not printed as these characters are interpreted as special characters by the printf function in this case printing out a newline on the screen. These characters are called escape sequences in C and cause special actions to occur and are preceded always by the backslash character, \ . All C compiler include a library of standard C functions such as printf which allow the programmer to carry out routine tasks such as I/O, maths operations, etc. but which are not part of the C language, the compiled C code merely being provided with the compiler in a standard form. 4

Lecture Notes