Introduction to C Programming Lecture 2
Today’s Lecture Software Categories System Software Application Software Introduction to ‘C’ Language History Evolution Justification Development Environment of ‘C’
There are two main categories of software System software Application Software
Technology Without An Interesting Name TWAIN Technology Without An Interesting Name
Evolution of programming languages The lack of portability between different computers led to the development of high-level languages—so called because they permitted a programmer to ignore many low-level details of the computer's hardware Details of procedural, non-procedural will follow in the lectures
How people used to program Machine Language….. Damn! It was difficult Assembly Language…. Remember ADD? Required too much user involvement To much to remember Less semantic C Language B Language.. Bell Labs Improved to C Language Is a compiled language
ANSI C
Tools of the trade Editor Interpreter and Compilers Debuggers
Integrated Development Environment (IDE) It contains Editor Compilers Debugger Linkers Loaders
Loader puts program in memory. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler creates object code and stores it on disk. Linker links the object code with the libraries Loader Primary Memory Compiler Editor Preprocessor Linker . Disk CPU Program is created in the editor and stored on disk.
#include <iostream.h> main ( ) { cout << “ Welcome to NIIT “; }
Variable X Variable
Variable Pic of the memory 25 10323 name of the variable
Variable Variable starts with Character Underscore _ (Not Recommended)
Variable Small post box X
Variable Variable is the name of a location in the memory e.g. x= 2;
Variable In a program a variable has: Name Type Size Value
Assignment Operator = x = 2 X 2
Assignment Operator L.H.S = R.H.S. X+ 3 = y + 4 Wrong Z = x +4 x +4 = Z Wrong
10 X X = 10 ; X = 30 ; 30 X
X = X + 1; X 10 + 1 = 11 X
Data type i int i ; //Declaration line
Writing C program
Examples of C/C++ compilers of today: Compiler converts human readable language to a language which is understandable by the operating system/hardware Examples of C/C++ compilers of today: Visual C++ GCC/G++ DJGPP (open source for windows like GCC) Borland C Turbo (obsolete and not recommended)
3 Stages of Compilation Stage 1: Preprocessing Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code The source code as stored on disk is not modified.
3 Stages of Compilation (con’t) Stage 2: Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings Saves the object code to a disk file, if instructed to do so (we will not do this). If any compiler errors are received, no object code file will be generated. An object code file will be generated if only warnings, not errors, are received.
Object code: It is machine language code containing various calls specific to operating system… e.g the object code written by compiler is not only hardware dependent but also operating system dependent. So if you have linux and windows both operating systems then object file of compiled by one Operating System (OS) will not get executed on the other OS
3 Stages of Compilation (con’t) Stage 3: Linking Combines the program object code with other object code to produce the executable file. The other object code can come from the Run-Time Library, other libraries, or object files that you have created. Saves the executable code to a disk file. On the Linux system, that file is called a.out. If any linker errors are received, no executable file will be generated.
Program Development Using gcc Editor Source File pgm.c Preprocessor Modified Source Code in RAM Compiler Program Object Code File pgm.o Other Object Code Files (if any) Linker Executable File a.out
#include <iostream.h> //This is pre-processor directive void main ( ) //this tells the starting point of your program { int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ; cout << " x = " ; //print the text on monitor cout << x ; cout << " y = " ; cout << y ; cout << " z =x + y = " ; cout << z ; }
int x, y, z ; int x; int y; int z ;
Data Types int short long float double char
Arithmetic operators Plus + Minus - Multiply * Divide / Modulus %
Arithmetic operators i + j x * y a / b a % b
% = Remainder 5 % 2 = 1 2 % 2 = 0
4 / 2 = 2 5 / 2 = ?
Precedence Highest: ( ) Next: * , / , % Lowest: + , -
Another C++ Program /***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: 123-45-6789 ** Section: 0304 ** E-mail: jstudent22@umbc.edu.pk ** ** This program prompts the user for two integer values then displays ** their product. ***********************************************/
Another C Program (con’t) #include <stdio.h> int main( void ) { int value1, value2, product ; cout<<“Enter two integer values: ; cin>>value1>>value2; product = value1 * value2 ; cout<<“Product = ”<<product ; return 0 ; }
Tokens The smallest element in the C language is the token. It may be a single character or a sequence of characters to form a single item.
Tokens are: Tokens can be: Numeric constants Character constants String constants Keywords Names (identifiers) Punctuation Operators
Numeric Constants Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: 123 98.6 1000000
Character Constants Singular! One character defined character set. Surrounded on the single quotation mark. Examples: ‘A’ ‘a’ ‘$’ ‘4’
String Constants A sequence characters surrounded by double quotation marks. Considered a single item. Examples: “UMBC” “I like ice cream.” “123” “CAR” “car”
Keywords Sometimes called reserved words. Are defined as a part of the C language. Can not be used for anything else! Examples: int while for
Names Sometimes called identifiers. Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores. Must follow the standards for this course!
Punctuation Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. ; : , ‘ “ [ ] { } ( )
Operators There are operators for: assignments mathematical operations relational operations Boolean operations bitwise operations shifting values calling functions subscripting obtaining the size of an object obtaining the address of an object referencing an object through its address choosing between alternate subexpressions