Introduction to C Programming (PDF)

Summary

This document introduces C programming, a fundamental language in computer science. It details the basics, different types of programming languages and how C programs work. It also includes examples and an explanation of the different steps involved.

Full Transcript

INTRODUCTION TO C PROGRAMMING Dr. SONALI CHAKRABORTY DEPARTMENT OF MATHEMATICALAND COMPUTATIONAL SCIENCES NATIONAL INSTITUTE OF TECHNOLOGY KARNATAKA SURATHKAL, MANGALORE – 575025, INDIA Dr. Sonali Chakraborty Email: [email protected]...

INTRODUCTION TO C PROGRAMMING Dr. SONALI CHAKRABORTY DEPARTMENT OF MATHEMATICALAND COMPUTATIONAL SCIENCES NATIONAL INSTITUTE OF TECHNOLOGY KARNATAKA SURATHKAL, MANGALORE – 575025, INDIA Dr. Sonali Chakraborty Email: [email protected] 1 INTRODUCTION C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972 It was designed and written by Dennis Ritchie Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed, and finally how are these combined to form an instruction A group of instructions would be combined later on to form a program A computer program is just a collection of the instructions necessary to solve a specific problem The basic operations of a computer system form what is known as the computer’s instruction set And the approach or method that is used to solve the problem is known as an algorithm Dr. Sonali Chakraborty Email: [email protected] 2 INTRODUCTION contd… Programming language are of two types. 1) Low level language 2) High level language ❑ Low level language: Low level languages are machine level and assembly level language In machine level language computer only understand digital numbers i.e. in the form of 0 and 1. So, instruction given to the computer is in the form binary digit, which is difficult to implement instruction in binary code This type of program is not portable, difficult to maintain and also error prone The assembly language is on other hand modified version of machine level language Instructions are given in English like word as ADD, SUM, MOV etc. It is easy to write and understand but not understand by the machine So the translator used here is assembler to translate into machine level. Although language is bit easier, programmer has to know low level details related to low level language INTRODUCTION contd… In the assembly level language the data are stored in the computer register, which varies for different computer Hence it is not portable ❑ High level language: These languages are machine independent, means it is portable The language in this category is Pascal, Cobol, Fortran etc. High level languages are understood by the machine So it need to translate by the translator into machine level A translator is software which is used to translate high level language as well as low level language in to machine level language. INTRODUCTION contd… ❑ Three types of translator are there: ✓ Compiler ✓ Interpreter ✓ Assembler Compiler and interpreter are used to convert the high level language into machine level language The program written in high level language is known as source program and the corresponding machine level language program is called as object program Both compiler and interpreter perform the same task but there working is different Compiler read the program at-a-time and searches the error and lists them If the program is error free then it is converted into object program When program size is large then compiler is preferred Whereas interpreter read only one line of the source code and convert it to object code If it check error, statement by statement and hence of take more time IMPORTANCE OF C It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with the features of a high-level language and therefore it is well suited for writing both system software and business packages. Programs written in C are efficient and fast. C is highly portable. C language is well suited for structured programming. Ability to extend itself. A C program is basically a collection of functions that are supported by the C library SAMPLE PROGRAM 1 The first line informs the system that the name of the program is main and the execution begins at this line main() is a special function used by C to tell the computer where the program starts Every program must have exactly one main function If more than one main function is used then the compiler cannot understand which is the beginning of the program The empty pair of parenthesis in main indicates that it has no arguments or parameters The { opening bracket in second line marks beginning of function and } at the end marks end of the function All statements between { and } is the function body SAMPLE PROGRAM 1 contd… The function body contains a set of instructions to perform a given task Here, the function body contains three statements: The printf statement is executable The lines beginning with are known as comment lines They are used in the program to enhance understandability printf is a predefined standard C function for printing output Predefined means it is a function that has already been written and compiled and linked together with the program printf line ends with semicolon Every statement in C should end with a semicolon THE MAIN FUNCTION SAMPLE PROGRAM 2 The words number and amount are variable names main() that are used to store numeric data { The numeric data can either be integer or real int number; In C, all variables should be declared to tell the float amount; compiler what variable names are and what type number = 100; of data they hold amount = 30.75 + 75.35; The variables must be declared before they are printf (number); used printf (amount); In this example, the declarations tell the compiler } that number is an integer and amount is floating point number Declaration statements appear in beginning of function and must end with a semicolon int and float are keywords and cannot be used as variable names LIBRARY FILES C programs are divided into modules of functions Some functions are written by users and others are stored in C library Library functions are grouped category wise and stored in different files known as header files If we want to access the functions stored in library it is necessary to tell the compiler about the files to be accessed This is done by using preprocessor directive #include as follows: #include filename is the name of the library file that contains the required function definition Preprocessor directives are placed at the beginning of the program stdio.h refers to the standard input (scanf) and output (printf) functions (#include ) math.h includes mathematical functions such as cos, sin, exp etc (#include) EXECUTING A C PROGRAM Executing a program written in C involves series of steps : ✓ Creating the program ✓ Compiling the program ✓ Linking the program with functions that are needed from C library ✓ Executing the program Dr. Sonali Chakraborty Email: [email protected] 23

Use Quizgecko on...
Browser
Browser