Top Banner
APTER 1: INTORDUCTION TO C LANGUA Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer to execute. A programming language is a formal language designed to communicate instructio ns to a machine, particularly a computer. Programming languages can be used to create programs that
28

Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Dec 23, 2015

Download

Documents

Kelly Mason
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

CHAPTER 1: INTORDUCTION TO C LANGUAGE

Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer to execute.

A programming language is a formal language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely.

Page 2: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

ASSEMBLY LANGUAGEMACHINE LANGUAGE

Page 3: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.
Page 4: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

 CATEGORIES OF PROGRAMMING LANGUAGES

A HIGH LEVEL LANGUAGE is a language for programming computers which does not require detailed knowledge of a specific computer. High-level languages do not have to be written for a particular computer, but must be compiled for the computer they will work with. High-level languages are closer to human language than low-level languages, and include statements like GOTO or FOR which are regular words. On the other hand, a LOW LEVEL LANGUAGE is a computer programming language that is close to machine language. MACHINE LANGUAGE is at the lowest level, because it is the actual binary code of 1s and 0s that the computer understands. ASSEMBLY LANGUAGES are low- level languages which are translated into machine code by an assembler. Each assembly language instruction corresponds to one machine language instruction, but assembly language is easier notation for the programmer to use than machine code.

Page 5: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.
Page 6: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

High Level Language Assembly Language Machine Language

Page 7: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

HIGH LEVEL LANGUAGEPROGRAM

MACHINE LANGUAGEPROGRAM

ASSEMBLY LANGUAGEPROGRAM

COMPILEROR

INTERPRETER ASSEMBLER

SOURCE CODE OBJECT CODE

Page 8: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Assembler: A computer will not understand any program written in a language, other than its machine language. The programs written in other languages must be translated into the machine language. Such translation is performed with the help of software. A program which translates an assembly language program into a machine language program is called an assembler.

Compiler: It is a program which translates a high level language program into a machine language program. A compiler goes through the entire program and then translates the entire program into machine codes. Interpreter: An interpreter is a program which translates statements of a program into machine code. It translates only one statement of the program at a time. It reads only one statement of program, translates it & executes it. Then it reads the next statement of the program again translates it & executes it. In this way it proceeds further till all the statements are translated and executed. On the other hand, a compiler goes through the entire program and then translates the entire program into machine codes. A compiler is 5 to 25 times faster than an interpreter.

Page 9: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

High level Languages: Very early in the development of computers attempts were made to make programming easier by reducing the amount of knowledge of the internal workings of the computer that was needed to write programs. If programs could be presented in a language that was more familiar to the person solving the problem, then fewer mistakes would be made. 

• High-level programming languages allow the specification of a problem solution in terms closer to those used by human beings.

• These languages were designed to make programming far easier, less error-prone and to remove the programmer from having to know the details of the internal structure of a particular computer.

• These high-level languages were much closer to human language.

• Many high level languages have appeared (and many have also disappeared!). 

Page 10: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

COBOL Business applicationsFORTRAN Engineering & Scientific ApplicationsPASCAL General use and as a teaching toolC & C++ General Purpose - currently most popularPROLOG Artificial IntelligenceJAVA General Purpose - gaining popularity rapidly

COBOL COmmon Business-Oriented Language

FORTRAN  Formula Translation

BASIC Beginner's All-purpose Symbolic Instruction Code

Page 11: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Become A Better ProgrammerLearn C Programming Language

Page 12: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

A Brief History of C• C is a general-purpose language.• The C programming language was devised in the early

1970s by Dennis M. Ritchie at Bell Labs, to implement the UNIX operating system.Characteristics of C Language

• C is an All purpose programming language. It is equally suitable for generating high level application and low level application. Sometimes it is also called the mid-level language.

• C language encourages the users to write their own library function so that they can extend the features of the language.C program can be executed on any type of computers.

• The program written in C language can be converted into machine language more efficiently.

• Computer games can be generated in C language which is accomplished through the usage of sound graphics.

• C is close to hardware.• Both low and high level programming can be done in C

language.• It is a highly structured language.

Page 13: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

A C program basically has the following form:• Preprocessor

Commands• Functions• Variables• Statements &

Expressions• Comments• Braces and

semicolon

To print “I Love Pakistan” on screen following simple C program can be used:

# include <stdio.h>void main(void){

printf(“I Love Pakistan”);}

Preprocessor Commands

• # sign indicates that this is an instruction for the compiler

• <stdio.h> stands for standard input-output header• printf( ) is a library function used to print.• The left brace { indicates the start of the body of the

any function.• The right brace } indicates the end of the body of the

any function.• A statement in C is terminated with a semicolon. :

Preprocessor Commands: These commands tells the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation.

Page 14: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

A C program can basically has the following form:• Preprocessor Commands• Functions• Variables• Statements &

Expressions• Comments• Braces and semicolonTo print “I Love Pakistan” on screen following simple C program can be used:

# include <stdio.h>void main(void){

printf(“I Love Pakistan”);}

Page 15: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

• # sign indicates that this is an instruction for the compiler

• <stdio.h> stands for standard input-output header

• printf( ) is a library function used to print.• The left brace { indicates the start of the body

of the any function.• The right brace } indicates the end of the

body of the any function.• A statement in C is terminated with a

semicolon:Preprocessor Commands: These commands tells the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation.

Page 16: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Note the followings• C is a case sensitive programming language. It

means in C printf and Printf will have different meanings.

• C has a free-form line structure. End of each C statement must be marked with a semicolon.

• Multiple statements can be one the same line.• Statements can continue over multiple lines.

Page 17: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

What is a C Program?

A collection of functions which when compiled, assembled, linked, loaded, and executed performs some task.                                   

Page 18: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

• Compile: Translate a program from source code to assembly code.  

• Assemble: Translate a program from assembly code to object code (sometimes these two steps are combined and called compiling).  

• Link: Pull together all of the functions needed for the program (both user defined objects and system libraries) and arrange that locations of functions and variables are known. This step creates the executable program.  

• Load: Move the executable program into computer memory.

• Execute: Run the program, that means now the computer is going to act upon the instructions given in the program.

Page 19: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

C Program Compilation Creating, Editing, Saving, Compiling, Debugging, Linking, Executing• Creating C Program• Editing C Program• Saving C ProgramYou can write, edit and save a C program in any text editor program. But Turbo C provides a complete IDE for this purpose. You can easily write, edit and save C program in TC editor.• The process of writing a new C program by following

the rules of C language is called creating a C program. To write a new C program, load the TC editor and type the source code of program.

• You can also make changes during writing new programs as well as in the exiting C program. Therefore, the process of writing, changing and revising the source code is called editing program.

• After writing or editing the source program, you should save it on the disk as text file with an extension " .c ". This process is called saving the program.

Page 20: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

The Edit-Compile-Link-Execute Process: Developing a program in a compiled language such as C requires at least four steps:• Editing (Or Writing) The Program• Compiling It• Linking It• Executing It

Editing: You write a computer program with words and symbols that are understandable to human beings. This is the editing part of the development cycle. You type the program directly into a window on the screen and save the resulting text as a separate file. This is often referred to as the source file . The custom is that the text of a C program is stored in a file with the extension .c for C programming language.

Compiling: You cannot directly execute the source file. To run on any computer system, the source file must be translated into binary numbers understandable to the computer's Central Processing Unit. This process produces an intermediate object file - with the extension .obj, the .obj stands for Object.

Page 21: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Linking: The first question that comes to most peoples minds is Why is linking necessary? The main reason is that many compiled languages come with library routines which can be added to your program. Theses routines are written by the manufacturer of the compiler to perform a variety of tasks, from input/output to complicated mathematical functions. In the case of C the standard input and output functions are contained in a library (stdio.h) so even the most basic program will require a library function. After linking the file extension is .exe which are executable files.

Executable files: Thus the text editor produces .c source files, which go to the compiler, which produces .obj object files, which go to the linker, which produces .exe executable file. You can then run .exe files as you can other applications, simply by typing their names at the DOS prompt or run using windows menu.

Page 22: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Structure of C Programs• C's character set• C's keywords• the general structure of a C program• that all C statement must end in a ;• that C is a free format language• all C programs use header files that contain

standard library functions.

Before writing any kind of program, one should have the concept of Flowcharts, which is the graphical representation of program.

Page 23: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.
Page 24: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.
Page 25: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

Example: Write a C program to find the factorial of any number n.

Page 26: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.
Page 27: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.

# include <stdio.h># include <conio.h>void main(void){  int n, j,fact;

  printf(“\nEnter a number: ");  scanf("%d", &n); fact=1;  for(j=1; j<=n; j=j+1)      fact=fact*j;

  printf(“\nThe Factorial of %d is %d", n, fact);  return 0;}

Write a C program to find the factorial of any number n.

Page 28: Programming languages: A programming language is a computer language programmers use to develop applications or other set of instructions for a computer.