Fundamentals of Programming Language 'C' PDF
Document Details
Smt. Chandanben S.S. Shah BCA College, Deesa
Tags
Summary
This document presents an overview of the C programming language. It describes different types of programming languages, concepts of algorithms and flowcharts, and provides examples to illustrate these concepts. The document also discusses characteristics of higher-level languages and how to execute a C program.
Full Transcript
Fundamentals of Programming Language ‘C’ Bachelor Of Computer Application Paper No - BCA-101 Subject - Fundamentals of Programming Language ‘C’ SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 1 Fundamentals of Programming Language ‘C’...
Fundamentals of Programming Language ‘C’ Bachelor Of Computer Application Paper No - BCA-101 Subject - Fundamentals of Programming Language ‘C’ SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 1 Fundamentals of Programming Language ‘C’ UNIT-I Introduction to Programming: Concepts of Algorithm and Flowcharts Algorithm: Any problem/program is represented in the form of text, it is called Algorithm. Flowcharts: Any problem/program is represented in the form of picture, it is called Flowchart. Symbols to draw Flowcharts: (1) Start/End: The above symbol show start or end the process in program. (2) Process: The above symbol of flowchart shows processing in program. (3) Input / Output: The above symbol of flowchart shows input output in the program. (4) Decision Box: The above symbol of flowchart shows Decision Box processing in the program. (5) Flow Lines: The above symbol of flowchart show Flow Lines like left, right, up, down in the program. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 2 Fundamentals of Programming Language ‘C’ Example: Draw Flowchart for find larger of two numbers. Algorithm for find larger of two numbers. Step 1: Start Step 2: Read X, Y. Step 3: If X>Y then Display “X is the largest number”. Else Display “Y is the largest number”. Step 4: Exit. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 3 Fundamentals of Programming Language ‘C’ Types of Programming Languages There are two types of programming languages: 1. Low level language a) Machine language (1GL) b) Assembly language (2GL) 2. High level language a) Procedural-Oriented language (3GL) b) Problem-Oriented language (4GL) c) Natural language (5GL) 1. Low level language This language is the most understandable language used by computer to perform its operations. It can be further categorized into: a) Machine Language (1GL) Machine language consists of strings of binary numbers (0 and 1) and it is the only one language, the processor directly understands. Machine language has very fast execution speed and efficient use of primary memory. b) Assembly Language (2GL) Assembly language is also known as low-level language because programmer requires detailed knowledge of hardware. This language uses symbolic operation code like ‘ADD’ for addition in place of 0 and 1. The program is converted into machine code by assembler. 2. High level language Instructions of this language like human language or English words. The high level language is easier to learn. It requires less time to write and is easier to maintain the errors. The high level language is converted into machine language by interpreter or compiler. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 4 Fundamentals of Programming Language ‘C’ a) Procedural-Oriented language (3GL) Procedural Programming determines the steps and the order of those steps in order to reach a desired output. It includes languages such as Pascal, COBOL, C, FORTAN, etc. b) Problem-Oriented language (4GL) It allows the users to specify what the output should be, without describing all the details of how the data should be manipulated to produce the result. This is one step ahead from 3GL. These are result oriented and include languages such as Visual Basic, C#, PHP, etc. c) Natural language (5GL) Natural languages are still in developing stage where we could write statements that would look like normal sentences. The programs designed using 5GL have artificial intelligence (AI). Characteristics of higher level language 1. Easy to learn: The high Level Languages are close to human languages like English. This makes the high level languages easy to learn and use. 2. Easy to detect errors: The logic of the program written in high level languages is very simple and easy. In case of errors, it is very easy to detect (or find) and remove errors in the Program. 3. Machine Independent: The program written in High Level Language is machine independent. It means that a program written or compiled on one type of Computer can be executed on another Computer. 4. Availability of Library Functions: Every High Level Language provides a large number of Built in functions (or library functions) that can be used to perform specific tasks during designing of new Programs. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 5 Fundamentals of Programming Language ‘C’ 5. Shorter Program: The Program written in high level language is shorter than the program written in low level language. 6. Well defined Syntax and standard: Every high level language has a standard syntax. The standard is approved by international organization. The most popular organization is ANSI (American National Standard Institute). Therefore, high level languages describe a well defined way of writing programs. 7. Source code is understandable by another programmer: The instructions of the program written in high level language are like English language statements. These are written according to the standard syntax of the language. Therefore, a computer programmer can easily understand a program written by another programmer. Compiler and Interpreter To convert source code into machine code, we use either a compiler or an interpreter. Interpreter Compiler Translates program one statement Scans the entire program and at a time. translates it as a whole into machine code. Interpreters usually take less Compilers usually take a large amount of time to analyze the amount of time to analyze the source code. source code. No intermediate object code is Generates intermediate object code, generated, hence are memory hence requires more memory. efficient. Programming languages like Programming languages like C, JavaScript, Python use interpreters. C++, and Java use compilers. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 6 Fundamentals of Programming Language ‘C’ Overview of “C” Introduction “C” Language is most popular computer language today because it is structured, high-level and machine independent language. The “C” developed by Dennis Ritchie in 1972. American National Standards Institute [ANSI] approved “C” language in 1989. The International Standards Organization [ISO] approved “C” language in 1990. Importance of “C” “C” is robust language whose rich set of built-in functions and operators perform complex programs. Programs written in “C” are efficient and fast. There are only 32 keywords in “C”. “C” language is well suited for structure programming. In “C” program, it is ability to extend the program itself. Sample “C” Programs Program to display “Hello Computer”. int main() { clrscr(); printf(“Hello Computer”); getch(); return 0; } Output Hello Computer SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 7 Fundamentals of Programming Language ‘C’ Program to display Roll No, Name and Address. int main() { clrscr(); printf(“My Roll No. : 21”); printf(“\n My Name : Akash”); printf(“\n My Roll Address : Akash Villa”); getch(); return 0; } Output My Roll No. : 21 My Name : Akash My Roll Address : Akash Villa Program to display sum of two values. int main() { int total; clrscr(); total = 10 + 20; printf(“Sum : %d”,total); getch(); return 0; } Output Sum : 30 SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 8 Fundamentals of Programming Language ‘C’ Basic Structure of “C” programs A “C” program may contain one or more sections as shown in figure. Documentation Section Link Section Definition Section Global Declaration Section main() function section { Declaration Part Executable Part } User Defined Functions Function 1 Function 2 - - Function N Documentation Section: This section consists of a set of comment lines giving the name of program, author and other details which programmer like to use later. Link Section: The link section provides instructions to the compiler to link functions from the system library. Definition Section: The definition section defines all symbolic constants. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 9 Fundamentals of Programming Language ‘C’ Global Declaration Section: There are some variables that are used in more than one function such variables declared outside of all function. main() function section: The main function contains two parts declaration and executable part. In every “C” program must have one main() function section. These two parts must appear between opening and closing braces. All statements in the declaration and executable parts end with a semicolon (;). Subprogram Section: This part contains all the user defined functions that are called in the main function. Programming Style “C” is free-form language. It means the “C” compiler does not where on the line we begin typing. We should try to use this fact to our advantage in developing readable program. We must develop the habit of writing programs in lower case letters, upper case letters are used only for symbolic constants. Braces are used to group program statements together and mark the beginning and the end of functions. A proper indentation of braces and statements would make a program easier to read and debug. “C” is free-form language; we can group statements together. So it makes the program easier to understand. Executing “C” Program Type the program in “C” editor and save program. Press Alt + F9 to compile the program. If there are errors, correct the errors and recompile the program. Then press Ctrl + F9 to execute/run the program. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 10 Fundamentals of Programming Language ‘C’ Constants, Variables and Data Types Character Set The character set that can be used to form words, numbers and expressions depend upon the computer on which the program is run. 1. Letters :- ▪ Uppercase -> A-Z ▪ Lowercase -> a-z 2. Digits :- ▪ 0-9 3. Special Characters: - Some special characters are as below. , Comma ) Right Parenthesis. Period $ Dollar Sign ; Semicolon % Percentage : Colon * Asterisk ? Question Mark - Minus “ Quotation Mark + Plus ( Left Parenthesis { Left Brace 4. White Spaces :- ▪ Blank Space ▪ Horizontal space ▪ Carriage Return ▪ New Line ▪ Form Feed SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 11 Fundamentals of Programming Language ‘C’ “C” Tokens In “C” program smallest individual unit is known as Token. Keywords: All keywords have fixed meaning and these meaning cannot be changed. Keywords serve basic building blocks of “C” program. There are 32 keywords in “C” language. All keywords must write in small letters. Ex. int, float etc. Identifier: Refers to name of variables, functions and arrays; these are user-defined names and consist of a sequence of letters and digits. Constants: Constants are refers to fixed value that does not change during the execution of program. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 12 Fundamentals of Programming Language ‘C’ Integer Constants: Integer constants refer to sequence of digits. There are three types of Integer constants. (1) Decimal Constant (2) Octal Constant (3) Hexadecimal Constant Ex. 0, 123, 210 Real Constants (Float Constant): The numbers such as distances, height, prices are display by fractional parts. Example 17.50 Such numbers are called real or floating point constants. Single Character Constants: A single character constant (or simply character constant) contains a single character alphabet, a digit, or a special symbol enclosed within a pair of single quote marks. Example ‘A’ NOTE: The character constant ‘5’ is not the same as number 5. String Character Constants: A string constant is a sequence of characters enclosed in double- quotes. Example “WELL DONE” Backslash character constant: “C” supports some backslash character constants as below. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 13 Fundamentals of Programming Language ‘C’ Variables A variable is used to store a value. Rules for naming variables: First character must be an alphabet or underscore. A variable name covers only letters, underscore (_) and digits. Only first 31 characters are allowed in variable name. A variable name cannot use a keyword. A variable name must not contain space. Data Types “C” language is rich in its data types. Data Type means type of data. “C” supports three classes of data types. 1. Primary data types 2. Derived data types 3. User-Defined data types SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 14 Fundamentals of Programming Language ‘C’ Integer Type: Integers are whole number (-2, -1, 0, 1, 2) a range of supported by “C”. Integer value is limited in range -32768 to 32767 and uses 2 bytes size. Short integer is similar to integer, it contains 1 byte size and range is -128 to 127. Long integer same as integer type but it contains 4 bytes size. Unsigned short integer, Unsigned long integer, Unsigned integer are always positive. Floating Point Type: Floating point numbers are stored 4 bytes (32 bits) with 6 digits of precisions when accuracy provided by a float number is not sufficient, the type double can be use. Double type use 8 bytes (64 bits) with 14 digits precisions. Long double size use 10 bytes (80 bits), precision extend further. Character Type: A single character can be defined as a character type data. Size of character is 1 byte (8 bits). Void Type: Void means nothing not even 0 (zero). Void type has no value; this is usually used to specify the type of function. SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 15 Fundamentals of Programming Language ‘C’ Declaration of Variable After designing suitable variable name, declaration does two things. (1) It tells compiler variable name (2) It specifies type of data variable hold Syntax Data type v1, v2, v3 … vN; Example int total, marks; Assigning value to variable After declaring the variable next step is to assign value to the variable. Value can be assign by assignment operator “=”. Example marks = 100; Defining Symbolic Constants The constants may appear many times at number of places in a program. One example of such constant is 3.14, representing value of “PI”. Symbolic names hold constants identity. ----- SMT. C.S.S.SHAH BCA COLLEGE, DEESA Page 16