In order to succeed, we must first believe that we can.
--Your friends at LectureNotes

Note for Computer Programming - CP By Sandeep Bhardwaj

  • Computer Programming - CP
  • Note
  • Board of technical education delhi - Crrit
  • 15 Topics
  • 2 Offline Downloads
  • Uploaded 9 months ago
Sandeep Bhardwaj
Sandeep Bhardwaj
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

Part A : The C Programming Language 12 Section 1 : Introduction and Overview 1.1 'C' History and Background • C was developed at Bell Laboratories as a general-purpose systems programming language. • It has been used in the development of the UNIX™ operating system and has grown in importance with the growth of UNIX™. • It is a third generation, 'high level' programming language, that is particularly good at producing fast, efficient code. • It is sometimes termed a "low-level high-level language" or "high level assembly language". This is because it has: • (1) The control constructs (eg. if, while) and structured data types (eg. arrays, records) found in high level languages, (2) Facilities normally only found in low level languages (eg. bit manipulation and use of register variables) . Like other high-level languages, it is more portable and maintainable than assembly language It is better than most other high level languages in this respect. • Unfortunately, its rather cryptic syntax does not make the code as 'readable' as most other high level languages.

Text from page-2

Part A : The C Programming Language 13 1.2 Example 'C' Program /* This is a comment and can be written anywhere and on more than one line if necessary */ /* The next statements are preprocessor controls */ #include <stdio.h> #define ONE 1 int globalnum; /* This is an example of a global data definition */ /* The main program now follows the { } mark the beginning and end */ main() { int localnum, sum; /* local data definitions */ globalnum=ONE; /* code statements */ localnum=ONE; sum=globalnum+localnum; printf("answer is %d\n", sum); /* printf is a library function used for outputting information to the screen */ return 0; /* this stops the program */ } Notes: 1. Every C statement ends in a semi-colon, newlines are not significant except in preprocessor controls. Blank lines are ignored. 2. A function name, including main, is always followed by () brackets. 3. Braces {} group statements together and are equivalent to the words "begin" and "end" in other languages such as Pascal. 1.3 C Program Structure In general, a C program will consist of: 1. Comments These can appear anywhere in a program between the symbols /* and */ , except of course, a comment cannot appear in the middle of a variable or function name.

Text from page-3

Part A : The C Programming Language 2. 14 Pre-processor Controls (optional) The pre-processor is the first part of the compiler to run. It takes control instructions from the code such as include another file, or define a macro. These occur on separate lines from other C language statements and always start with a "#". 3. Global Data Definitions (optional) These define external (global) data items (variables) that are to be widely available for use in the main program and program functions. 4. Function Definitions (at least one) These will contain both data definitions and code instructions to be executed while the program runs. All program executable statements are enclosed within function definitions. Every C program contains one function named main. When the program runs it starts with the first code statement in main. 1.4 C Exercise 1 Examine any C program (for example, there are some in Part E) and answer the following: 1. Are there any comments? If so, where? What would happen to the program if the comments were removed? 2. Where does the program start? Where does it finish? 3. Which are the pre-processor statements? 4. Statements starting with the keyword int are data definition statements for integer variables. Which of these are global data definitions and which are local data definitions? 5. You will probably observe that some of the statements start with a number of spaces. Why might this be? Does it help you understand the program?

Text from page-4

15 Part A : The C Programming Language Section 2 : Constants and Variables 2.1 Declaring Data Variables In C all variables are declared before they are used. This is so that: 1. A memory location is given a name. 2. A suitable number of bytes can be allocated. 3. The compiler knows how to treat the data. There are several data types in 'C': Variable type Number of bits char int short int short long int long float double long float long double 8 16 or 32 16 16 32 32 about 32 about 64 about 64 > 64 (usually) (usually) (usually) (usually) (usually) 2.2 Notes on Variable Types • The types short int and short are identical. Similarly long int and long are identical. • The char, int, short and long types can be preceded by the qualifiers signed or unsigned. The default is signed. If used on their own the type signed refers to type signed int and unsigned refers to type unsigned int. • The type char is so called as it is suitable for storing a character. . . . . . but the 'C' compiler will also let it be used to store numbers (unlike a Pascal compiler). Similarly int, short or long variables, either signed or unsigned may be used for storing characters.

Lecture Notes