Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28
Lecture One:
Principles of Programming CSU 07209
Mr. Peter Kaaya (PhD*-JKUAT), Assistant Lecturer
Introduction to C programming
• C is a programming language developed at AT
& T’s Bell Laboratories of USA in 1972. • It was designed and written by a Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like ALGOL. • ANSI C standard emerged in the early 1980s cont • the software tool as well as the C compiler is written in C. Major parts of popular operating systems like Windows, UNIX, Linux is still written in C. • This is because even today when it comes to performance (speed of execution) nothing beats C. • C seems so popular is because it is reliable, simple and easy to use Stages in evolution of C language Year Language Developed by Remarks 1960 ALGOL International committee Too general, too abstract 1963 CPL Cambridge university Hard to learn, difficult to implement 1967 BCPL Martin Richards at Could deal only with specific Cambridge University problems 1970 B Ken Thompson at AT&T Could deal only with specific problems 1972 C Dennis Ritchie at AT&T Lost generality of BCPL and B restored
• C stand between Problem oriented languages (high level
languages) e.g. FOTRAN,COBOL, BASIC,PASCAL and machine oriented language (low level languages)- designed to better machine efficiency. • Its called middle level language cont • often heard today is – “C has been already superseded by languages like C++, C# and Java. • C is a language , every language starts from a Character set. • C has 256 Characters, it’s a big number. It is based on mathematics, english and Science Program • There is a close analogy between learning English language and learning C language. • The classical method of learning English is to: – first learn the alphabets used in the language, – learn to combine these alphabets to form word – alphabets are combined to form sentences and – sentences are combined to form paragraphs. Cont.. • Learning C is similar and easier. Instead of straight-away learning how to write programs, we must first know: • what alphabets, numbers and special symbols are used in C, • how constants, variables and keywords are constructed, and • how are these combined to form an instruction. • A group of instructions would be combined later on to form a program • a computer program is just a collection of the instructions necessary to solve a specific problem. The basic operations of a computer system form what is known as the computer’s instruction set. – And the approach/method used to solve the problem is known as an algorithm. cont • So for as programming language concern these are of two types. • 1) Low level language • 2) High level language Low level language: • Low level languages are machine level and assembly level language. • In machine level language computer only understand digital numbers i.e. 0 and 1 • instruction given to the computer is in the form binary digit, which is difficult to implement instruction in binary code 0 & 1. Cont.. • The assembly language is on other hand modified version of machine level language. • Where instructions are given in English like word as ADD, SUM, DIV etc. It is easy to write and understand but not understood by the machine. • So the translator used here is assembler to translate into machine level. High level language: • These languages are machine independent, means it is portable. • The language in this category is Pascal, Cobol, Fortran etc. • High level languages are understood by the machine. So it needs to be translated by the translator into machine level. • A translator is software which is used to translate high level language as well as low level language in to machine level language. Types of translators • Compiler • Interpreter • Assembler • Compiler and interpreter are used to convert the high level language into machine level language. • The program written in high level language is known as source program and the corresponding machine level language program is called as object program. • Both compiler and interpreter perform the same task but their working is different. • Compiler read the program at-a-time and searches the error and lists them. • If the program is error free then it is converted into object program. – When program size is large then compiler is preferred. • Interpreter read only one line of the source code and convert it to object code. – It checks error, statement by statement and hence of take more time. – An assembler is a program that converts assembly language into machine code. – Assemblers are similar to compilers in that they produce executable code C Characters • C programming has 256 Characters from: • a-z (26) • A-Z (26) • a‡A (lowercase not equal to uppercases) • Digits 0-9 (10) • Special symbols on the keyboard & that are not on the keyboard Constants in C • Constant is a any value that cannot be changed during program execution. • In C, any number, single character, or character string is known as a constant. • A constant is an entity that doesn’t change whereas a variable is an entity that may change. • C constants can be divided into two major categories: – Primary Constants – Secondary Constants Constants in C contd… Types of Constants: a) Integer Constants: Whole numbers with no decimal points e.g. 5 -5 Rules for constructing Integer constants: b)An integer constant must have at least one digit c)It must not have a decimal point d)It could be either positive/negative e)If no sign precedes, an integer constant is assumed to be positive f) No comas/blanks are allowed within an integer constant g)The allowable range for integer constant is -32768 to 32768 Real/Floating point constants a) They could be written in two forms: fractional and exponential form b) Rules: a) A real constant must have at least one digit b) It must have a decimal point c) It could be either positive/negative d) Default sign is positive e) No commas/blanks are allowed within a real constant e.g. 426.0, -32.76,-48.5792 Character Constants Are single alphabet,single digit or a single special symbol enclosed within single inverted commas. E.g. ‘a’ is a valid constant while a is not Are any single character from 256 characters and enclosed with ‘ ’ (single quotes) E.g. Valid char (‘a’, ‘%’, ‘5’) Invalid char (B,’atc15$’) – The maximum length of a character constant can be 1 character e.g. ‘A’,’I’,’5’,’=‘ String Constants Are any single/multiple character enclosed from 256 characters and are enclosed with “ “ (double quotes) e.g. “atc12@” NB: char is not equal to String i.e ‘5’(char) is not equal to “5”(string), 5(int) is not equal to 5.0 (float) e) Octal constants They are base 8 numbers Their valid characters 0-7 You have to prefix 0 to present an octal constant e.g. a valid octal- (0756) invalid octal (756)
f) Hexadecimal constants: it is base 16 number, valid
characters are 0-9 & A-F. in C, to present hexadecimal constant you have to prefix 0X e.g. valid hexadecimal (0X2345D4DMK), invalid hexa KEYWORDS • All predefined words of C language are called keywords. • Are the words whose meaning has already been explained to C compiler • There are only 32 keywords in C • E.g. int, char, if, else, switch, for, break etc. • Not everything is predefined, sometimes a programmer can define his/her own keyword • There are certain words reserved for doing specific task, these words are known as reserved word or keywords. • These words are predefined and always written in lower case or small letter. Auto Double Union If Default Static Typedef
Break Else far Int For signed Struct
Case Enum goto Long Short while Switch Char Exert unsigned Near Void Continue Const Float Register Do Return Identifiers • All programmers defined keywords are called identifiers. • user defined word used to name entities like variables, arrays, functions, structures etc. • Rules for naming identifiers: – name should only consists of alphabets (both upper and lower case), digits and underscore (_) sign. – first characters should be alphabet or underscore – name should not be a keyword – since C is a case sensitive, the upper case and lower case considered differently, for example code, Code, CODE etc. are different identifiers. – identifiers are generally given in some meaningful name such Identifier naming rules 1. An identifier cant be a keyword 2. The allowed character in an identifier are a-z, A-Z,0-9 and _. No other special symbols are allowed. 3. The first character of an identifier cant be a number. Valid identifier Invalid identifier Age_19 Age 19 AGE_19 AGE &19 _AGE19 19age summary • Historical background of C • Types of programming language • Types of translators (Compiler, interpreter, assembler) • C characters • Constants • Keywords • identifiers