• 470 likes • 979 Views
Fundamental of C programming. - Ompal Singh. HISTORY OF C LANGUAGE. IN 1960 ALGOL BY INTERNATIONAL COMMITTEE. IT WAS TOO GENERAL AND ABSTRUCT. IN 1963 CPL(COMBINED PROGRAMMING LANGUAGE) WAS DEVELOPED AT CAMBRIDGE UNIVERSITY. IT WAS TOO TURNED OUT TO BE VERY BIG AND DIFFICULT TO LEARN.
E N D
Fundamental of C programming - Ompal Singh
HISTORY OF C LANGUAGE • IN 1960 ALGOL BY INTERNATIONAL COMMITTEE. • IT WAS TOO GENERAL AND ABSTRUCT. • IN 1963 CPL(COMBINED PROGRAMMING LANGUAGE) WAS DEVELOPED AT CAMBRIDGE UNIVERSITY. • IT WAS TOO TURNED OUT TO BE VERY BIG AND DIFFICULT TO LEARN. • IN 1967 BCPL(BASIC CPL) AT CAMBRIDGE UNIVERSITY IT WAS TOO SPECIFIC AND MUCH TOO LESS POWERFUL.
IN 1970 B LANGUAGE SIMPLIFICATION TO CPL BY KEN THOMPSON AT AT&T LABS IT WAS TOO SPECIFIC AND LIMITED IN APPLICATION. • IN 1972 C DEVELOPED BY RITCHIE AT AT&T LABS IT WAS TRULY A GENERAL LANGUAGE EASY TO LEARN AND VERY POWERFUL. • IN 1972 THE NEXT PHASE OF EVALUATION TO C BY INCORPORATING FEATURES OF OOP REINCARNATING C INTO ITS NEW AVATAR C++.
The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s • Influenced by • ALGOL 60 (1960), • CPL (Cambridge, 1963), • BCPL (Martin Richard, 1967), • B (Ken Thompson, 1970) • Traditionally used for systems programming, though this may be changing in favor of C++ • Traditional C: • The C Programming Language, by Brian Kernighan and Dennis Ritchie, 2nd Edition, Prentice Hall
Characteristics of C We briefly list some of C's characteristics that define the language and also have lead to its popularity as a programming language. Naturally we will be studying many of these aspects throughout the course. • Small size • Extensive use of function calls • Loose typing -- unlike PASCAL • Structured language • Low level (BitWise) programming readily available • Pointer implementation - extensive use of pointers for memory, array, structures and functions.
C has now become a widely used professional language for various reasons. • It has high-level constructs. • It can handle low-level activities. • It produces efficient programs. • It can be compiled on a variety of computers. • Structured language
FEATURES OF C LANGUAGE • C is highly portable language . • C has only as few as 32 keywords • It has a comprehensive set of operators . • Users can create their own functions . • C is low level & high level support. • It has a large library of functions. • C is case sensitive language
Translators are system software used to convert high-level language program into machine-language code. Compiler : Coverts the entire source program at a time into object code file, and saves it in secondary storage permanently. The same object machine code file will be executed several times, whenever needed. Interpreter : Each statement of source program is translated into machine code and executed immediately. Translation and execution of each and every statement is repeated till the end of the program. No object code is saved. Translation is repeated for every execution of the source program. Executing a C program #include<stdio.h> int main() { ……. Text Editor prog1.c compiles C-compiler Syntax Errors? Yes 010110 100 ……………. 01011 101 No Object machine code adds prog1.obj Linker 00101010 …………. 01010101 Executable machine code prog1.exe machine code of library file Executes C-Runtime Feeds Runtime or Logic Errors ? Input Yes Output
The Preprocessor • The Preprocessor accepts source code as input and is responsible for • removing comments • interpreting special preprocessor directives denoted by #. • For example • #include -- includes contents of a named file. Files usually called header files. e.g • #include <math.h> -- standard library maths file. • #include <stdio.h> -- standard library I/O file • #define -- defines a symbolic name or constant. Macro substitution. • #define MAX_ARRAY_SIZE 100 • C Compiler • The C compiler translates source to assembly code. The source code is received from the preprocessor. • Assembler • The assembler creates object code. On a UNIX system you may see files with a .o suffix (.OBJ on MSDOS) to indicate object code files. • Link Editor • If a source file references library functions or functions defined in other source files the link editor combines these functions (with main()) to create an executable file.
Structure of C program Documentation Section Linkage Section Definition Section Global Declaration Section Main Function Section Local Declaration Part Executable Code Part Sub Program Section Function1() Function2() …………… FunctionN()
Structure of a C Program Syntax of basic structure of a C program is /* Documentation section */ /* Link section */ /* Definition section */ /* Global declaration section */ /* Function section */ (return type) (function name) (arguments...) void main () { Declaration part Executable part (statements) } /* Sub-program section */ (return type) (function name 1) (arguments...) (return type) (function name 2) (arguments...) . . . (return type) (function name n) (arguments...)
How to run a simple c program • 1. Copy Turbo c/c++ in computer • 2. Open c:\tc\bin\tc.exe • 3. A window appears • 4. Select File->new to open a new file
Program to print hello 5. Type the following program on editor #include <stdio.h> void main() { printf(“hello”); } 6. compile the program by pressing ALT+F9 7. Run the program by pressing CTRL +F9
CHARACTER SET • Letters A – Z or a – z. • Digits 0 – 9. • Special Symbols (all the special symbols present on the keyboard) • White spaces Blanks space , horizontal tab, carriage return , new line, from feed.
Keywords • Keywords are words, which have special meaning for the C compiler. Keywords are also sometimes known as reserved words. • Following is a list of keywords C
Variable • Variable is a name of memory location where we can store any data. It can store only single data (Latest data) at a time. In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. • A declaration begins with the type, followed by the name of one or more variables. For example, • DataType Name_of_Variable_Name; • int a,b,c;
Variable names- Rules • Should not be a reserved word like int etc.. • Should start with a letter or an underscore(_) • Can contain letters, numbers or underscore. • No other special characters are allowed including space • Variable names are case sensitive • A and a are different.
Local Variables • Local variables are declared within the body of a function, and can only be used within that function only. Global Variable • A global variable declaration looks normal, but is located outside any of the program's functions. This is usually done at the beginning of the program file, but after preprocessor directives. The variable is not declared again in the body of the functions which access it. • Syntex of local variable: Syntax of global variable: #include<stdio.h> int a,b; void main( ) void main() { { int a,b,c; } } fun() Fun() { { } }
Constant • Constant is a special types of variable which can not be changed at the time of execution. Syntax: syntax: const int a=20; Types of constant: • Character Constant. • Integer Constant. • Real Constant. • String Constant.
CHARACTER CONSTANTS • The maximum length of a character constant is one character. • Ex : ‘a’ is a character constant.
INTEGER CONSTANT • An integer constant refers to a sequence of digits. There are three types of integers in C language : • Decimal. • Octal. • Hexadecimal. • EX : 1,56,7657, - 34 etc .
Real Or Floating Point Constants • A number with a decimal point and an optional preceding sign represents a real constant. • Ex : 34.8 , -655.33 , .2 , -.56, 7.
String Constants • A string constant is a sequence of one or more characters enclosed within a pair of double quotes ( “ “) if a single character is enclosed within a pair E. G. “Welcome to c programming \n”.
Input and Output • Input • scanf(“%d”,&a); • Gets an integer value from the user and stores it under the name “a” • Output • printf(“%d”,a) • Prints the value present in variable a on the screen
Data Types • A C language programmer has to tell the system before-hand, the type of numbers or characters he is using in his program. These are data types. There are many data types in C language. A C programmer has to use appropriate data type as per his requirement. C language data types can be broadly classified as • Primary data type • User defined data type
Primary data type • All C Compilers accept the following fundamental data types • 1.Integer int • 2.Character char • 3.Floating Point float • 4.Double double
DATA TYPE RANGE OF VALUES • char -128 to 127 • Int -32768 to +32767 • Float 3.4 e-38 to 3.4 e+38 • double 1.7 e-308 to 1.7 e+308
TYPE SIZE (Bits) Range • Char or Signed Char 8 -128 to 127 • Unsigned Char 8 0 to 255 • Int or Signed int 16 -32768 to 32767 • Unsigned int 16 0 to 65535 • Short int or Signed short int 8 -128 to 127 • Unsigned short int 8 0 to 255 • Long int or signed long int 32 -2147483648 to 2147483647 • Unsigned long int 32 0 to 4294967295 • Float 32 3.4 e-38 to 3.4 e+38 • Double 64 1.7e-308 to 1.7e+308 • Long Double 80 3.4 e-4932 to 3.4 e+4932
Oprators • An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. C has a rich set of operators which can be classified as • 1. Arithmetic operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increments and Decrement Operators 6. Conditional Operators 7. Bitwise Operators 8. Special Operators
1. Arithmetic Operators • All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language.
Operator Meaning • + Addition or Unary Plus • – Subtraction or Unary Minus • * Multiplication • / Division • % Modulus Operator
2. Relational Operators • Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational operators.
Operator Meaning • < is less than • <= is less than or equal to • > is greater than • >= is greater than or equal to • == is equal to • != not equal to
3. Logical Operators C has the following logical operators, they compare or evaluate logical and relational expressions. • Operator Meaning • && Logical AND • || Logical OR • ! Logical NOT
4. Assignment Operators • The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression. Example • x = a + b
5. Increment and Decrement Operators • The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below • 1. ++ variable name 2. variable name++ 3. – –variable name 4. variable name– – The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of operand. ++variable name and variable name++ mean the same thing when they form statements independently, they behave differently when they are used in expression on the right hand side of an assignment statement.
6. Conditional or Ternary Operator • The conditional operator consists of 2 symbols the question mark (?) and the colon (:) The syntax for a ternary operator is as follows .exp1 ? exp2 : exp3 The ternary operator works as follows exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expression is evaluated
For example a = 10; b = 15; x = (a > b) ? a : b Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
7. Bitwise Operators • C has a distinction of supporting special operators known as bitwise operators for manipulation data at bit level. A bitwise operator operates on each bit of data. Those operators are used for testing, complementing or shifting bits to the right on left. Bitwise operators may not be applied to a float or double.
Operator Meaning • & Bitwise AND • | Bitwise OR • ^ Bitwise Exclusive • << Shift left • >> Shift right
Specifier Meaning • %c – Print a character %d – Print a Integer %i – Print a Integer %e – Print float value in exponential form. %f – Print float value %g – Print using %e or %f whichever is smaller %o – Print actual value %s – Print a string %x – Print a hexadecimal integer (Unsigned) using lower case a – F%X – Print a hexadecimal integer (Unsigned) using upper case A – F %a – Print a unsigned integer. %p – Print a pointer value %hx – hex short %lo – octal long %ld – long unsigned integer.
Evaluation of Expression • Expression refers to anything that evaluates to a numeric value. • Order of Precedence Example • () Highest z-(a*b/2)+w*y • !, unary +, - 5-(3*9/2)+2*-5 • *, /, % 5-(27/2)+-10 • binary +, - 5-13+-10 • <,<=,>,>= -8+-10 • ==,!= -18 • && • || Lowest