Lecture 2. Introduction to C Programming PDF
Document Details
Uploaded by EruditeConceptualArt116
Tags
Summary
This document provides an introduction to C programming, describing its importance in various technologies. It highlights the language's history, robust features, and efficiency, demonstrates its wide-ranging applications in operating systems, databases, and embedded systems.
Full Transcript
Lecture No: 2 Introduction to ‘C’ – Importance of ‘C’ – Structure of ‘C’ Program Introduction In 1960 the language BCPL(Basic Combined Programming Language was developed at Cambridge University called B’ Language B’ Language was modified by Dennis Ritchie and was implemented at Be...
Lecture No: 2 Introduction to ‘C’ – Importance of ‘C’ – Structure of ‘C’ Program Introduction In 1960 the language BCPL(Basic Combined Programming Language was developed at Cambridge University called B’ Language B’ Language was modified by Dennis Ritchie and was implemented at Bell Laboratories in 1972 called C’ Language. Importance of C’ Language Robust language - we can write any complex program because it consists of built-in functions and operators Assembly Language – C act as an assembly language, so that we can write system software and application software Efficient and Fast – It supports variety of data types and powerful operators, so that it is many times faster than other languages. Portable – C programs written for one computer can be run on another with little or no modification. Structured Programming – A proper collection of function modules or blocks would make a complete program. This modular structure makes program debugging , testing and maintenance easier. Ability to extend itself - We can add our own functions to the C library Importance of ‘C’ After All These Years, the World is Still Powered by C Programming Many of the C projects that exist today were started decades ago. The UNIX operating system’s development started in 1969, and its code was rewritten in C in 1972. The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code. Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world. In 1985 Windows 1.0 was released. Although Windows source code is not publicly available, it’s been stated that its kernel is mostly written in C, with some parts in assembly. Linux kernel development started in 1991, and it is also written in C. The next year, it was released under the GNU license and was used as part of the GNU Operating System. The GNU operating system itself was started using C and Lisp programming languages, so many of its components are written in C. But C programming isn’t limited to projects that started decades ago, when there weren’t as many programming languages as today. Many C projects are still started today; there are some good reasons for that. How is the World Powered by C? Despite the prevalence of higher-level languages, C continues to empower the world. The following are some of the systems that are used by millions and are programmed in the C language. Microsoft Windows Microsoft’s Windows kernel is developed mostly in C, with some parts in assembly language. For decades, the world’s most used operating system, with about 90 percent of the market share, has been powered by a kernel written in C. Linux Linux is also written mostly in C, with some parts in assembly. About 97 percent of the world’s 500 most powerful supercomputers run the Linux kernel. It is also used in many personal computers. Mac Mac computers are also powered by C, since the OS X kernel is written mostly in C. Every program and driver in a Mac, as in Windows and Linux computers, is running on a C-powered kernel. Mobile iOS, Android and Windows Phone kernels are also written in C. They are just mobile adaptations of existing Mac OS, Linux and Windows kernels. So smartphones you use every day are running on a C kernel. Databases The world’s most popular databases, including Oracle Database, MySQL, MS SQL Server, and PostgreSQL, are coded in C (the first three of them actually both in C and C++). Databases are used in all kind of systems: financial, government, media, entertainment, telecommunications, health, education, retail, social networks, web, and the like. 3D Movies 3D movies are created with applications that are generally written in C and C++. Those applications need to be very efficient and fast, since they handle a huge amount of data and do many calculations per second. The more efficient they are, the less time it takes for the artists and animators to generate the movie shots, and the more money the company saves. Embedded Systems Imagine that you wake up one day and go shopping. The alarm clock that wakes you up is likely programmed in C. Then you use your microwave or coffee maker to make your breakfast. They are also embedded systems and therefore are probably programmed in C. You turn on your TV or radio while you eat your breakfast. Those are also embedded systems, powered by C. When you open your garage door with the remote control you are also using an embedded system that is most likely programmed in C. Then you get into your car. If it has the following features, also programmed in C: automatic transmission tire pressure detection systems sensors (oxygen, temperature, oil level, etc.) memory for seats and mirror settings. dashboard display anti-lock brakes automatic stability control cruise control Structure of C Programs A C program may contain one or more sections, Documentation Section Link Section Definition Section Global Declaration Section main () Function Section { Declaration Section Executable Section } Subprogram Section Function 1 Function 2 --- --- Function n Documentation Section - It consists of a set of comment lines giving the name of the program, author and other details. Link Section – It provides instructions to the compiler to link functions from system library. Definition Section – It defines all symbolic constants. Global Declaration Section - There are some variables that are used in more than one function, such variables are called global variables and are declared in the global declaration section ie outside of all the functions. main ( ) Function Section – It contains two parts i) Declaration part ii) Executable part - The declaration part declares all the variables used in the executable part. - At least one statement in the executable part. - These two parts must be covered by opening and closing braces ( { } ). - All statements in the declaration and executable parts end with a semicolon ( ; ) - The program execution begins at the opening brace and ends at the closing brace Subprogram Section – It contains all the user – defined functions that are called in the main function. It is placed immediately after the main function Example 1:- Write a C program to find area of Circle. #include #include void main () { float r, area; clrscr(); printf(“\n Enter Radius value:”); scanf(“%f”,&r); area = 3.14 * r * r; printf(“\n The area of Circle is %f “,area); getch(); } Example 2:- Write a C program to find area of Circle. #include #include #define PI 3.14 void main ( ) { float r,area; clrscr( ); printf(“\n Enter Radius value:”); scanf(“%f”,&r); area = PI * r * r; printf(“\n The area of Circle is %f “,area); getch( ); }