The smartpath information systems c pro

29
C Programming language Basic Concepts Prepared By The Smartpath Information systems www.thesmartpath.in

Transcript of The smartpath information systems c pro

Page 1: The smartpath information systems c pro

C Programming languageBasic Concepts

Prepared By The Smartpath Information systemswww.thesmartpath.in

Page 2: The smartpath information systems c pro

Index

1. Brief History of C Language 2. About C Programming Language 3. Characteristics of C programming language 4 C Programming Environment 5. Installing C in Windows 6. C Program Structure 7. C Program Structure – Explained 8. C programming – Syntax 9. C Character Set10 . Keywords 11. Operators in C12. Operators in C ( continue…..)

Page 3: The smartpath information systems c pro

Index

13. Data Types in C14. Data Types in C 15. Data Types in C 16. Variables17. Rules for Variable Declaration18. Variables Scope19. Input and Output 20 . Decision Making 21. Looping22. Looping23. C Libraries24. Uses of C

Page 4: The smartpath information systems c pro

Brief History of C Language

The origin of C is closely tied to the development of the UNIX operating system, originally implemented in assembly language on a PDP-7 by Ritchie and Thompson. The development of C started in 1972 on the PDP-11 Unix system and first appeared in Version 2 Unix. In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language. This book, known to C programmers as "K&R“. ANSI –C - In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90. Therefore, the terms "C89" and "C90" refer to the same programming language

Page 5: The smartpath information systems c pro

About C Programming language The C programming language is a structure oriented programming

language, developed at Bell Laboratories in 1972 by Dennis Ritchie

C programming language features were derived from an earlier language called “B” (Basic Combined Programming Language – BCPL)

C language was invented for implementing UNIX operating system

In 1978, Dennis Ritchie and Brian Kernighan published the first edition  “The C Programming Language” and commonly known as K&R C

In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard

or “ANSI C”, was completed late 1988.

Page 6: The smartpath information systems c pro

Characteristics of C programming language

There is a small, fixed number of keywords, including a full set o flow of control and primitive .

There are a large number of arithmetical and logical operators, such as +, +=, ++, &, ~, etc.

Typing is static but weakly enforced all data has a type, but implicit conversions can be performed; for instance, characters can be used as integers.

Strings are not a separate data type, but are conventionally implementedAs null terminated array of characters

Enumerated are possible with the enum keyword. They are not tagged,and are freely interconvertible with integers.

Page 7: The smartpath information systems c pro

C programming Environment

The C programming environment consists of - (1) Text Editor - Programs are typed in Text Editor . By default a new file Noname.c is opened. Type your programs here , save it , compile and run. (2) Compiler – Compiler is a System software that is installed on computer to compile and run programs. Most widely Turbo C compiler is used

Page 8: The smartpath information systems c pro

Installing C in Windows

To install C in windows first download Turbo C compiler. . 1. unzip the downloaded file. And run the install.exe file 2. Press ‘Enter’ to continue 3. Enter Source drive where C-compiler and editor will be saved. 4. Enter the source path to locate the exe file. Press ‘Enter’ key. 5. using arrow key , scroll down. Select start installation 6. A message is displayed after successful installation. 7. Go to C:\TC\BIN right click TC icon select create shortcut.

Page 9: The smartpath information systems c pro

C Program Structure

A C program contains the following – (a) preprocessor commands (b) main( ) function ( c ) variables (d) input and output functions For example : #include< stdio.h> /* this is comment line */ #include< conio.h> Void main() { printf(“hello”); getch(); }

Page 10: The smartpath information systems c pro

C Program structure Explained

1. #include - The first two lines are preprocessor commands that tell the compiler that the program is using functions in it.

2. Void main() - This is the main function. Program execution begins with the this function only.

3. Variables - variables hold input and output values.4. printf() - this is function to give output on the screen. The word “hello” is displayed on output screen.

5. The getch() – It is a function that ends program and returns to program window.

Page 11: The smartpath information systems c pro

C Programming - Syntax for writing programs

1. C language is case sensitive language. Hello and hello are two different words.

2. Programs are written in small caps only ie.. In small letters .Capital words are treated differently.

3. Inside the main() function , each statement is terminated with semicolon.

4. Comments are used for explanation. /* ….. */ format is used to give comments in program. They are not executed.

Page 12: The smartpath information systems c pro

C Character Set

The basic C source character set includes the following characters

• Lowercase and uppercase letters: a–z , A–Z• Decimal digits: 0–9• Graphic characters: ! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~• Whitespace characters – space , horizontal tab, vertical tab new line , form feed

Page 13: The smartpath information systems c pro

Keywords

Page 14: The smartpath information systems c pro

Operators in C

C supports a rich set of operators, which are symbols used within an expression to specify the manipulations to be performed while evaluating that expression. C has operators for

1. Arithmetic operators 2. Relational operators addition + Equals == subtraction - Not Equal != Multiplication * Greater than > Division / Greater than equal to >= Modulus % Less than < Less than equal to <=

Page 15: The smartpath information systems c pro

Operators in C

3 . Logical operators logical AND && 8. function call ( )

logical OR || 9. object size sizeof( ) logical NOT ! 10. Bitwise 4. increment ++ left shift << decrement -- right shift >> 5. Assignment operator =

6. Member selection . ->

Page 16: The smartpath information systems c pro

Data Types in C

1. Basic data types Data Type Format Specified Space occupied char %c 1 byte signed char   %c 1 byte int   %d 2 bytes signed short int %hi 2 bytes long int %ld 4 bytes float %f 4 bytes double %lf 8 bytes long double %Lf 10 bytes

Page 17: The smartpath information systems c pro

Data Types in C 2. Derived data types -

The derived data types are (i) Pointer (ii) Array (iii) Structure (iv) Union (v) Function

Page 18: The smartpath information systems c pro

Data Types in C

3. User defined data types -

Typedef is used to create user defined data type in C for example structures are declared with typedef to create linked list data structures in C . For Example : typedef struct node { int n; node * p; }

Page 19: The smartpath information systems c pro

Variables Variables in C is valid name in the memory where input given by user is stored and the result of program is stored. When a variable is declared in program , it occupies space in memory according to its size. Such as integer type takes 2 bytes of memory, char type takes 1 byte of memory Example: int sum; float average; char ch; Here int ,float , char are basic data types. And Sum , average and ch are variables..

Page 20: The smartpath information systems c pro

Rules for declaring variables

1. A variable name should start with letter. 2. It should not be a keyword 3. It can contains underscore character. 4. It should not contain white spaces and special characters. Example: sum - valid 1sum - invalid emp_id - valid emp id - invalid

Page 21: The smartpath information systems c pro

Variables scope

In C language variables have the following scopes

1. Local Scope - Variable declared inside main() , and variable declared inside a function has local scope that is it is recognized inside it local variable can be used inside that block of code.

2. Global Scope - variables declared outside the main() function are global variable. They are recognized throughout the program. They can be used by any function .

Page 22: The smartpath information systems c pro

Input and output

C has functions predefined for taking user input in a program and giving output in the screen

scanf( ) - the scanf() function takes variable number of arguments from user in predefined format format - scanf(“%d%c%f”, &a, &b, &c);

printf() - the printf() function gives output of program on the output screen

format - printf(“%d%c%f ” , a , b , c );

Page 23: The smartpath information systems c pro

Decision making Decision making requires that the programmer specify one or more

conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and other statements to be executed if the condition is determined to be false

Statements used are - 1. simple if 2. if – else 3. if-else ladder 4. nested if – else 5. conditional operator

Page 24: The smartpath information systems c pro

Decision making Examples : 1. simple if if( condition1) { statements; } if(condition 2) {statements ; } 2. if – else if(condition true) { statements; } else { condition false}

Page 25: The smartpath information systems c pro

Looping There may be a situation, when you need to execute a block of code severalnumber of times. In general, statements are executed sequentially: The firststatement in a function is executed first, followed by the second, and so on.Statements inside the loop are executed till condition is true or condition getsFalse. There are three types of loops in C 1. For Loop 2. While Loop 3. Do -While Loop

Page 26: The smartpath information systems c pro

Looping Examples :

1. For Loop for(initialization ; condition; increm / decrem) { statements; } 2. While Loop while(condition) { statements ; }

3. Do -While Loop do { statements } while( condition );

Page 27: The smartpath information systems c pro

C Libraries

C language contains set of files header files called library. These header files Have ‘.h’ extension. They contain functions for various types of operationsPerformed in a program. To use them we have to include them in a programThrough include statement. Some of C Library header files are – stdio.h conio.h stdlib.h graphics.h

Page 28: The smartpath information systems c pro

Uses of C C is widely used for "system programming", including implementing

operating systems and embedded system applications, due to a combination of desirable characteristics such as code portability and efficiency, ability to access specific hardware addresses, ability to match externally imposed data access requirements, and low run-time demand on system resources. C can also be used for website programming using CGI as a "gateway" for information between the Web application, the server, and the browser Some reasons for choosing C over interpreted languages are its speed, stability, and near-universal availability C has also been widely used to implement end-user applications, but much of that development has shifted to newer, higher-level languages.

Page 29: The smartpath information systems c pro