c notes

Share Embed Donate


Short Description

Download c notes...

Description

C Compiler A compiler is a software that translates high-level language in to machine language (source code into object code). A compiler translates the code into an intermediary form. This step is called compiling, and produces an (.obj) object file. The compiler then invokes a linker, which turns the object file into an (.exe file) executable program. Machine language is digital, or binary, instructions that the computer can understand. Because the computer can't understand C source code, a compiler translates source code to machine code, also called object code. The linker combines the object code from your program with the object code from the function library and creates an executable file. In olden day’s people were using different languages for different purposes. E.g.: FORTRAN for scientific purpose, COBOL for business purpose, so people are forced to study all the languages that are very risky task. So they want to switch to a language that can do all types of works. s.no

Year

Language

Developed by

1

1960

ALGOL

International committee

2

1963

CPL

Cambridge university

3

1966

BCPL

Martin Richards

4

1967

B

Ken Thompson

5

1972

C

Dennis Ritchie

1

History of 'C': Martin Richards developed the language BCPL (Basic Combined Programming Language) before 1970. Ken Thompson, a system engineer, from bell lab, did some modification in the BCPL language and for the modified version; he gave the name as B, the first character of BCPL. In 1972, another system engineer, called Dennis Ritchie, developed and first implemented the programming language C at AT & T bell laboratories of USA. C is a middle level programming language that allows you to program applications both using high level and low-level languages and used to develop both business application and system side applications. C is function oriented programming or we can say a C program is a collection of functions why because almost for every thing we have the built-in functions. C is highly structured programming or procedure oriented programming (it follows a particular procedure or structure to write the programs) and this structure makes its debugging, testing and maintenance easier.

Features of C 1. Portability or machine independent 2. Sound and versatile language 3. Fast program execution. 4. An extendible language. 5. Tends to be a structured language. Why C is popular? 1. It has a rich set of built-in functions, operators and data types, which is used to write any complex programs easily. 2

2. C is highly portable, which means programs written in one computer, can be run in other computer with little or no modifications. 3. Programs written in c are efficient and fast. Basic Structure of C Program Documentation section. Link section Definition section Global declaration section main() { Local Declaration part; Execution part; }

Sub function section Function 1 Function 2 Function 3 …………… Function n

//BASIC STRUCTURE OF C PROGRAM #include #define pi 3.1415 int x; void main()

3

{ int a,b; float y; printf (“Hello World”); } Documentation Section Documentation section is used to give the comments about the programs like the author name, program name etc. Two Types: Single Line Comment To make a single line as a comment.

//comment

Multi Line Comment To make more than one line as comment. /* comment */ Link section Link section is used to link the header files (the file that contain all the built in functions to be used within the program, they are with extension (.h)) Syntax: #include E.g.: #include #include An #include directive instructs the compiler to add the code from another file into your source code during the compilation process. Definition Section Definition Section allows you to define the symbolic constants in your program.

4

E.g.: #define PI 3.1415 An #define statement defines a symbolic name or symbolic constant to be a particular string of characters. Syntax: #define name replacement_list Global declaration Global declaration section is used to declare the variables globally. (i.e. before the main function) These variables can be accessed from any where (from any functions) in the program. These variables are called Global Variables. Variables Variables are temporary names given to the memory location where different values are stored. Limitations for variable names 1. Case sensitive. 2. First letter should be an alphabet. 3. It can contain numbers. 4. No white spaces or special symbols are allowed except an underscore (_) character. 5. Should not be a keyword. Data Types The data type of a variable is a keyword used to determine, that what kind (format) of data it can store. Type

Length

unsigned char

1 byte

char

1 bytes

enum

2 bytes

Range 0 to 255 -128 to 127 -32,768 to 32,767 5

unsigned int

2 bytes

0 to 65,535

short int

2 bytes

-32,768 to 32,767

int

2 bytes

-32,768 to 32,767

unsigned long

4 bytes

0 to 4,294,967,295

long int

4 bytes

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

float

4 bytes

3.4 * (10**-38) to 3.4 * (10**+38)

double

8 bytes

1.7 * (10**-308) to 1.7 * (10**+308)

long double

10 bytes

3.4 * (10**-4932) to 3.4 * (10**+4932)

Syntax for variable declaration datatype var1,var2,…; int x,y,x; float a,b; Format Specifiers They are used to specify the type of data displayed. int  %d long int  %ld float  %f double  %lf long double  %Lf char  %c String  %s 6

Hexadecimal  %x Unsigned Integer  unsigned int  %u Variable Declaration Declarations create space for variables. The Figure below illustrates a variable declaration for the variable answer. We have not yet assigned it a value so it is known as an uninitialized variable. The question mark indicates that the value of this variable is unknown. Declaration of answer and assigning it a value

printf Function The library function printf can be used to print the results. printf("Twice %d is %d\n", term, 2 * term); printf structure

7

Output: Twice 15 is 30 Main function section Main function section is the one where we are going to write the actual code of the program. Local declaration part is to declare local variables. (Inside the main function) These variables can be accessed only in the main function. Execution part is the actual coding part.

Operators: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment or decrement operators 6. Conditional operators 1.arithmetic operators Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division and remainder. Operator

Name

Meaning

Example

+

Plus

Addition

a+b

-

Minus

Subtraction

a-b

*

Asterisk

Multiplication

a*b

/

Slash

Division

a/b

%

Modulator

Remainder

a%b

Ex:- a=10, b=3 8

a+b = 10+3 =13 a-b = 10 - 3 = 7 a*b = 10 * 3 =30 a/b= 10 / 3 = 3 a%b=10%3=1

2.

Relational operators Relational operators are used to check the relation between two values. Using relational operators we write relational expressions, a relational expression returns a Boolean value either 1 or 0. 1 indicates true, 0 indicates false. a=10; b=3; Operator

Name

Example

Result

<

Less than

a

Greater than

a>b

True or 1

=b

True or 1

==

Equal to

a= =b

False or 0

!=

Not equal to

a!=b

True or 1

3.Logical operators: logical operators are used to combine two or more relational expressions. C language contains 3 logical operators. Operator

name

9

&&

and

||

or

!

not

&& : It is used to combine two or more relational expressions, it returns true when all conditions are true, other wise it returns false. Truth Table: Expression1

&&

expression2

result

T

T

T

T

F

F

F

T

F

F

F

F

|| : This operator is used to combine two or more relational expressions, it returns true when any one of the condition is true, else it returns false. Truth table: Expression1 ||

expression2

result

T

T

T

T

F

T

F

T

T

F

F

F

! (not): This is the negative operator, we can place the not operator before any one of the condition. It returns the opposite result of the condition. i.e. It converts true to false and false to true. Truth table:

10

! expression

result

T

F

F

T

1. Assignment operator: C language contains equal to (=) operator to assign a value or expression into a variable. Ex: a=10; a=a+10; C language has the following short cut assignment operators. +=

a=a+10

a+=10

-=

a=a-10

a-=10

*=

a=a*10

a*=10

/=

a=a/10

a/=10

%=

a=a%10

a%=10

2. Increment and decrement operators: 1)++ increment operator 2) -- decrement operator ++: it is used to increment 1 to a variable. a. pre increment( ++a) ex:- printf(“%d”,++a); First the value will be incremented, and then the new value will be printed. 2)post increment(a++) ex:- printf(“%d”,a++);

11

First the value will be printed and then the value will be incremented. 2) - - : It is used to decrement the value by 1. a. pre decrement (--a) b. post decrement (a--) 6. Conditional operators: C language contains conditional operators(? , : ) to return a value from 2 values based on a condition. Syn: condition ? value 1 : value 2. If the condition is true then value1 will be returned, if the condition is false then value2 will be returned Ex:- a=40, b=20 Max=a>b ? a : b; Max=40 a=40, b=20 Min=a
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF