Cmatter.docx

  • Uploaded by: Burhan Pasha
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cmatter.docx as PDF for free.

More details

  • Words: 2,588
  • Pages: 17
Getting started with C C is a general-purpose language that was originally developed by Dennis M. Ritchie in 1972 to develop the UNIX operating system at Bell Labs. The UNIX operating system, the C compiler, and essentially all UNIX applications programs have been written in C. The C has now become a widely used professional language for various reasons.  Easy to learn  Structured language  It produces efficient programs.  It can handle low-level activities.  It can be compiled on a variety of computer platforms. Facts about C  C was invented to write an operating system called UNIX.  C is a successor of B language which was introduced around 1970  The language was formalized in 1988 by the American National Standard Institute (ANSI).  The UNIX OS was totally written in C by 1973.  Today C is the most widely used and popular System Programming Language.  Today's most popular Linux OS and RBDMS MySQL have been written in C. Why to use C? a . C is quick. b . C is a core language : In computing, C is a general purpose, cross-platform, block structured procedural, imperative computer programming language. c . C is a small language: C has only thirty-two keywords. This makes it relatively easy to learn compared to bulkier languages. d . C is portable. Some examples of the use of C might be:   

Operating Systems Language Compilers Assemblers

     

Text Editors Print Spoolers Network Drivers Modern Programs Databases Language Interpreters

 PROGRAM & PROGRAMMING LANGUAGE:  A program is a set of logically related instructions that is arranged in a sequence that directs the computer in solving a problem.  The process of writing a program is called programming.  Software is a collection of computer programs and related data that provides the instructions for telling a computer what to do and how to do it.  Computer software can be broadly classified into two categories : (a) system software & (b) application software  System software is a collection of programs that interfaces with the hardware.  Categories of system software :

 Application software is written to enable the computer to solve a specific data processing task.  Categories of application software :

 PROGRAMMING LANGUAGE:  A programming language is composed of a set of instructions in a language understandable to the programmer and recognizable by a computer.

 C is called middle-level language because it is actually bind the gap between a machine level language and high-level languages. User can use c language to do System Programming (for writing operating system) as well asApplication Programming (for generate menu driven customer billing system ). That's why it is called middle level language.  High level - Ada , Modula-2 , Pascal, COBOL, FORTRAN, BASIC  Middle level - Java, C++, C, FORTH, Macro-assemble  Low level – Assembler  CLASSIFICATION OF PROGRAMMING LANGUAGES:

 DEVELOPING PROGRAM IN C: 1. Writing the C program 2. Compiling the program and 3. Executing it.

 PARTS OF PROGRAM: HEADER FILE:  The header files, usually incorporate data types, function declarations and macros, resolves this issue. The file with .h extension is called header file, because it’s usually included at the head of a program.  The header files primarily contain declarations relating to standard library functions and macros that are available with C.

MAIN FUNCTION: • main() is a user defined function. main() is the first function in the program which gets called when the program executes. The start up code c calls main() function. We can’t change the name of the main() function.

• main() is must.  STRUCTURE OF C PROGRAM:

 EXECUTION STEPS OF A PROGRAM:

 Steps : 1. Translation of the program resulting in the object program. 2. Linking of the translated program with other object programs needed for execution, thereby resulting in a binary program.

3. Relocation of the program to execute from the specific memory area allocated to it. 4. Loading of the program in the memory for the purpose of execution. LINKER  Linker is a function which links up the files that an present in the operating system, it also links the files for the hardware and makes the system ready for executing.  Linking makes the addresses of programs known to each other so that transfer of control from one subprogram to another or a main program takes place during execution. LOADER  Loading means physically placing the machine instructions and data into main memory, also known as primary storage area.  The functions performed by the loader are : a. Assignment of load-time storage area to the program b. Loading of program into assigned area c. Relocation of program to execute properly from its load time storage area d. Linking of programs with one another COMPILER  For executing a program written in high level language, it should be first translated into a form that machine can understand. This is done by a software called Compiler.  The compiling process consists of two steps: a . The analysis of the source program and b . The synthesis of the object program in the machine language of the specified machine.  During the process of translation There is another type of software that also does translation. This is called an interpreter. INTERPRETER  It reads only one line of a source program at a time and converts it into an object code. In case of errors/same will be indicated instantly.

e. Differences between compiler and interpreter :

PREPROCESSOR This is a program, that processes the source program before it is passed on to the compiler. Preprocessor directories are always initialized at the beginning of the program. it begins with the symbol (#) hash. It place before the main() function Eg: # include <stdio.h> # define PI 3.14  C TOKENS: Individual words and punctuation marks are characters. In a “C” program the smallest individual units are known as “C” tokens. It has 6types of token‟s 

C tokens are of six types. They are,

1. 2. 3. 4. 5.

Keywords (eg: int, while), Identifiers (eg: main, total), Constants (eg: 10, 20), Strings (eg: “total”, “hello”), Special symbols (eg: (), {}),

6. Operators

(eg: +, /,-,*)

C tokens example program: int main() { int x, y, total; x = 10, y = 20; total = x + y; Printf (“Total = %d \n”, total); } Keywords : Keywords are reserved words by compiler. Keywords are assigned with fixed meaning and they cannot be used as variable name. No header file is needed to include the keywords. There are 32 keywords

Identifiers : These are the names of variables ,functions and arrays, these are the user defined names Eg : # define NUM 10 # define A 20 “NUM”, “A” are user – defined identifier Constants : constants in “C” are applicable to the values which not change during the execution of a program. Integer Constants : Sequence of numberr 0-9 without decimal points, fractional part or any other symbols. It requires two or four bytes, can be +ve, -ve or Zero the number without a sign is as positive.Eg: -10, +20, 40

Real Constants : Real constants are often known as floating constants. Eg: 2.5, 5.521, 3.14 etc. Character Constants Single character const : A single character constants are given within a pair of single quote mark. Eg : ‘a’,8 etc. String Constant : These are the sequence of character within double quote marks Eg : “Straight” “India”, “4” Variables : This is a data name used for storing a data, its value may be changed during the execution. The variables value keep‟s changing during the execution of the program. Eg : height, average, sum, etc.  All variables have three important attributes: 1. A data type that is established when the variable is defined, e.g., integer, real, character. Once defined , the type of a C variable cannot be changed. 2. A name of the variable. 3. A value that can be changed by assigning a new value to the variable. The kind of values a variable can assume depends on its type. For example, an integer variable can only take integer values, e.g., 2, 100, –12.  DATA TYPES

Range of data types : Data type

Bytes in Ram

Range of data type

char

1 bytes

-128 to 127

int

2 bytes

-32, 768 to 32,767

float

4 bytes

double

8 bytes

3.4c-38 to 3.4 c+ 38 1.7C – 308 to 1.7c +308

CHARACTER The characters that can be used to form words and expressions depends upon the computer to which the program is run The Characters in C are 1. Letters A-X, a-z, both upper and lower 2. Digits 0-9 3. Special character, +,-,*,”,;,./, 4. which spaces newline, horizontal tab;,carriage return ,blank space INTEGER Integers are whole numbers with a range of variables supported by a particular machine. DATA TYPE SPECIFIERS The specifiers and qualifiers for the data types can be broadly classified into three types:  Size specifiers— short and long  Sign specifiers— signed and unsigned  %hd /* short integer */  %ld /* long integer */  %Lf /* long double */

Type

Storage size

Value range

char

1 byte

-128 to 127 or 0 to 255

unsigned char

1 byte

0 to 255

signed char

1 byte

-128 to 127

int

2 or 4 bytes

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int

2 or 4 bytes

0 to 65,535 or 0 to 4,294,967,295

short

2 bytes

-32,768 to 32,767

unsigned short

2 bytes

0 to 65,535

long

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned long

4 bytes

0 to 4,294,967,295

FLOAT Floating Point numbers are stored with 6 digits of precision. Those are defined with keyword float. When the accuracy is not sufficient then the datatype double can be used. double gives a precesion of 14 digits these known as double precesion numbers. Still for a better process we can use long double which uses 80 bits.

type

Storage size

Value range

Precision

float

4 byte

1.2E-38 to 3.4E+38

6 decimal places

double

8 byte

2.3E-308 to 1.7E+308

15 decimal places

long double

10 byte

3.4E-4932 to 1.1E+4932

19 decimal places

VOID A void type has no value this is usually used to specify the return type of function , this function does not return any value to calling function.  STRUCTURED PROGRAMMING  Structured programming involves top–down analysis for program solving, modularization of program structure and organizing structured code for individual module.  Top-down analysis breaks the whole problem into smaller logical tasks and defines the hierarchical link between the tasks.  Modularization of program structure means making the small logical tasks into independent program modules that carries out the desired tasks.  Structured coding is structured programming which consists of writing a program that produces a well organized module.  ALGORITHM:  An algorithm is ‘an effective procedure for solving a problem in a finite number of steps’.  A well-designed algorithm has termination and correctness properties.  The four common ways of representing an algorithm are the Step-form, Pseudo-code, Flowchart and Nassi-Schneiderman .  algorithms show these three features: a. Sequence (also known as process) b. Decision (also known as selection) c. Repetition (also known as iteration or looping)  PSEUDOCODE & FLOWCHART:

 Like step-form, Pseudo-code is a written statement of an algorithm using a restricted and well-defined vocabulary.  A flowchart comprises of a set of standard shaped boxes that are interconnected by flow lines to represent an algorithm.  There should be a logical start and stop to the flowchart.  The usual direction of the flow of a procedure or system is from left to right or top to bottom.  The intersection of flow lines should be avoided.  Flowcharts facilitate communication between programmers and users. FLOWCHART: SYMBOLIC REPRESENTATION:

ADVANTAGES OF FLOWCHART:  Communication  Effective analysis  Proper documentation  Efficient coding  Proper debugging  Efficient program maintenance FLOWCHART LIMITATIONS:

   

Complex logic Alterations and modifications Reproduction Loss of objective

 Example 1:Prepare a flowchart to read the marks of a student and classify them into different grades. If the marks secured are greater than or equal to 90, the student is awarded Grade A; if they are greater than or equal to 80 but less than 90, Grade B is awarded; if they are greater than or equal to 65 but less than 80, Grade C is awarded; otherwise Grade D is awarded.  Example 2:Draw a flowchart to find the roots of a quadratic equation.

 PROGRAM STATEMENTS: A statement is a syntactic constructions that performs an action when a program is executed. All C program statements are termin ated with a semi-colon (;). • Declaration :It is a program statement that serves to communicate to the language translator information about the name and type of the data objects needed during program execution.

• Expression statement: It is the simplest kind of statement which is no more than an expression followed by a semicolon. An expression is a sequence of operators and operands that specifies computation of a value . Example :x = 4 • Compound statement is a sequence of statements that may be treated as a single statement in the construction of larger statements. • Labelled statements can be used to mark any statement so that control may be transferred to the statement by switch statement.

 Control statement is a statement whose execution results in a choice being made as to which of two or more paths should be followed. In other words, the control statements determine the ‘flow of control’ in a program.

 What is a C Program? A computer program is a set of instructions for a computer to perform a specific task.A collection of functions which when compiled, assembled, linked, loaded, and executed performs some task. 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. Load: Move the executable into computer memory. Execute:Run the program

Operator - precedence & Associativity Precedence is nothing but priority that indicates which operator has to be evaluated first when there are more than one operator. Associativity : when there are more than one operator with same precedence [ priority ] then we consider associativity , which indicated the order in‟ which the expression has to be evaluated. It may be either from Left to Right or Right to Left.

Category

Operator

Associativity

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

*/%

Left to right

Additive

+-

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %=>>= <<= &= ^= |=

Right to left

Comma

,

Left to right

More Documents from "Burhan Pasha"

Structures_b10.docx
November 2019 22
November 2019 8
Cmatter.docx
November 2019 12
Kashif Resume 2.docx
November 2019 19
Function (1).docx
November 2019 17