Вы находитесь на странице: 1из 16

C Language

What is language? In natural life, a language is a communication medium using which we can communicate with each other... In the same manner while working with computer a language (also known as programming language) is used to developed new software using which user can communicate with the hardware. Generally there are two types of programming languages: 1. Low level language. 2. High level language. Low level language: The languages which can be understood by the computer directly are known as low level language. Eg: binary, assembly (language). High level language: The language in which we write the programme in English language are known as high level language but, these language cannot be understood by the computer directly therefore each high level language consist of its own compiler which is used to compile our program from the source code (program writer in English language) into computer understandable language (machine language e.g.:- COBOL, BASIC, PASCAL, C, C++, JAVA etc.) C language:- C is structured high level programming language developed by Dennis Ritchie in 1971 (in early 1970) at AT & T bell laboratories initially c was developed for the implementation by Unix operating system but now a days it has become all in one language ie. This language can be used to develop any type of software. History of C The milestones in C development UNIX Developed C 1969 DECPDP -7 Assembly Languages. BCPL User Friendly OS Providing powerful development tools developed from BCPL Assembler tedious long & error prone. A new language B a second attempt of C 1970. A Totally new language C a successor of B C 1971. By 1973 Unix OS almost totally written. At & T Bell Labs between 1969 & 1973 according to Ritchie, The most creative period occurred in 1972. It was named because its feature was derived from an earlier language called B which according language to Ken Thompson was a stripped down version of BCPL Programming Language.

The Origin of C is closely tied to the development of the UNIX Operating system , originally implemented on a PDP-7 by Ritchie & Thompson, in cooperating several ideas from colleagues . 1978 Brain Kernighan & Dennis Ritchie published first edition of The C Programming Languages. As from above discussion, it is clear that computer understand the machine language, to reduce the complexity off the programmer the ANSI committee establish a standard code for each character, number and symbols etc.. ANSI C & ISO C
The ANSI version contains many revisions to the syntax and the internal workings of the language, the major ones being improved calling syntax for procedures and standardization of most (but, unfortunately, not quite all!) system libraries.

1983, The American National Standards Institute (ANSI) formed a committee, X3J11 to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3 159- 1989 programming languages C called C89 Or Standard C 1990. The ANSI C standard (with formatting changes) was adopted by International organization for standardization (ISO) as ISO / IEC 9899:1990 which is sometimes called C90. Therefore the C89 and C90 refer to same programming language. For example: Char Basic concept of programming language:
Alphabets- A, B, C, D.X, Y, Z Digits- 0, 1, 2, 3.9 Special Symbol- *,?,!, @, #,$,%,&,

Variables, constant, Keywords

fkjdj
Instruction

Programs

Software

Features
1. C is a compiler based language. 2. C is a case sensitive language. If different also between capital & Upper case. 3. C is a free from language. It ignores while spaces (blank space) New line character. 4. C is a block structured language it may contain one or more block as define below -

i. ii. iii. iv. v. vi.

Comment line. Preprocessor statement. Global dedication. Main body. Local declaration. Executable statement.

5. C is a middle level language. Major parts of C programmers. A C programmer may contain one or more block as below.

1. Comment line /*----------*/. 2. Preprocessor statement [start with #] (A) Linking section. (B) Definition. 3. Global (definition) declaration A) Variable declaration. B) Function declaration. 4. Main Function Section A) Local Declaration. B) Executable part. 5. Sub program section.
Variable- Variable is an entity that can be changed and their name also can be changed. These are the temporary location in the memory.

Rule for making variable names. a) b) c) d) e) A variable names is any combination of 1 To 31, alphabets, digit or under score. Only underscore (_) used to make variable name as a special symbol. No comas or blank are allowed. The variable name starts from any alphabets or underscore, not with digits. Uppercase and Lower Case Letters are distinct.

Constant- A constant is an entity that doesnt change.

Types of c constants C constant can be divided into two major categories: a) Primary Constant b) Secondary Constant These constants are further categorized as below: C Constant

Primary Integer constant Real (float) Constant Character constant

Secondary Array Pointer Structure, Union, & Enum etc.

C Keywords- These are the pre-define keywords that has its own specific work. There are only 32 keywords available in c. Ex- if, else, break, continue, int, float, for, switch, avoid, while, do, default, char, case etc.

Keywords According to ASNI


C89 has 32 keywords (reserved words with special meaning):
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

C Data Type
Derived Built-in Data types Int, Float, Char User- Defined

Basic Data Type


Data Types
Character Integer Floating point Doubleprecision Floating point

Descriptions
Single Character whole number Real Number (precision) 06 places Real number ( precision) 12 places

Keywords
Char int float Double

Memory
1 byte 2 byte 4 byte 8 byte

Conversion
%c %d %f %f

Range ( on 16 bit machine)


-128 to +127 -32767 to +32768 3.4e-38 to 3.4e+c 1.7e-308 to 1.7e+3.8

Operator- Operator is a system defined symbol which works on some values or expression and return another value. Expression- The combination of variables, constant and operator return according to the syntax according to the syntax of the language to called an expression. Arithmetic operators- The arithmetic operators perform arithmetic operators and can be classified into array of binary arithmetic operator. Operator + * / % Meaning Addition Subtraction Multiplication Division Modules

Assignment Operator- it is used assign the value of expression of right hand side a storage space on left hand side. X=x+1 C language also supports short hand assignment operator. Relational operator- Relational operator compares two values of an expression and return true or false depending upon the comparison. Some relational operator <,>, <=, =>, ==, ! = Logical operators- these operators are used to compare the logical relational expressions. The operators are as follows. Operator Meaning && Logical ANI ! Logical Not || Logical OR Increment & Decrement operators- these operators are used to increase on to decrease the value of a variable. These are as follows: Operator Meaning ++ Increases the value by 1 -Decreases the value of 1 Conditional Operator- The operator consists of two symbol, question mark (?) and colon (;). It has three parts, the test expression, and true or false part like.

Test expression- true part: false part it assigns either the true part value a the false part value to a variable depending on the test expression. Eg: i= (j>5) 2:3 If the value of j is greater than 5 than 2 will be assigned to 1 other wise 3. Bitwise operator- This operator on each bit of data. They are used for testing, complementing or shifting of but to right or left.

These are as follows:


Operator Meaning

& Bitwise ! Bit wise or ^ XOR << Bitwise shift left >> right ~ complement Comma Operator- this operator is used to separate set of variables. The (,) symbol is called the comma operator. Important Points: For compilation Alt + F9 For Run Ctrl + F9 To get the latest (output) Alt+ F5

stdio conio Printf Scanf Example :


#include <stdio.h> #Include<conio.h> void main() { Clrsrc(); printf (welcome to c); getch(); } Conversion type: Int/int = int %d Int/float =float %f Float/ int = %f

Standard input /output Console input / output Printf is a pre- defined functions display the output on the screen.
printf( "format", variables );

to take the input from the user.


scanf("format string", variables);

Example : # include <stdio.h> # include <conio.h> Void main () { int p,t; float r,si; clrscr (); printf (enter the value of p,t,r); scanf (%d%d%f,&p,&t,&r); si= (p*t*r)/100; printf (simple interest=%f, si ); getch (); }

Control structure in C:
Introduction- The control structure determines the orders in which the various instructions in a c program me are be executed. In other words, they determine the flow of control in a c program. The different control structures are as follows: 1. Sequential controlled structure. 2. Conditional structure. 3. Unconditional branching statement. 4. Loop structure. Sequential- this type of control structure is are that the instructions are executed in the same ordered in achieve they appears in a c program. Conditional structure- this type of constant structure allows in to take the decision as to which instruction is to be executed. The decision make instruction is as follows: If structure 1. If else structure 2. Nested if structure 3. Switch- case structure If structure- syntax: if (condition) { Statements; } Ex: if (1>5) { Printf (value is greater than 5); }

Here if the given condition is than the statement is given in the brace will be executed. There is no need to write the pair of brace if there is only one statement to be executed.

If else structure. Syntax: if (condition) { statements; } Else { statements ; } Then if statement itself execute single or a glow of statement. When the given condition in true. It does nothing when the condition is false. The purpose of else statement in to execute the set of statement written offer it even when the condition in false, there is no need to write the pair of brake if there is only the statement to be executed. Nested if structure of use write an entire if for or else structure either within the body to and an if statement or the body of a else statement then this is called noted if structure.

Syntax:-

if (condition) { statement; If (condition) statement; }} Eg; if (i>5) If (j>10) { printf (the value of in greater than 5 and); Printf( the value in greater than 10); } Switch-case structure:Syntax:switch (expression) { case value: statement; Break; Case value 2; statement Break: --------------Break; }
It is also knows as multi-way branching statement. It consists of an expression in switch and the constant value in case statement. This structure can have any number of case statement based on the values of the given expression in switch statement matches with any of the constant values in the case statements after the case statement will be executed, but if any of the constant value does not match with the given expression then the statement written in the default section will be executed. A break statement in necessary at the end of each case and also in the default section. In this structure the default section is totally optional.

{ int m1,m2,m3,m4,m5 ,sum; float pr; printf (enter the marks of five subjects); scanf (%d%d%d%d%d, &m1,&m2, &m3, &m4, &m5; sum=m1+m2+m3+m4+m5; pr = sum/5; printf (percentage is %d, pr); getch (); }
Loop control structure - The versatility of a computer lies in ability to perform a set of instruction. Repeatedly this involves some portion of the programme executes either a specified number of times or until a particular condition is satisfied. This type of repetitive in done through loop control structure. While loop structure:Syntax: while (condition) { statement; } increment /decrement. Here all the block statements will be executed as long as the gives condition in true. Once the condition becomes false the control exits from the while structure. Use can also use nested while loop if necessary use of for loop.

Use of If else: Use of for loop: # include <stdio.h> #include <conio.h> Void main() { Int ; Clrscr (); For (i=1,i<=20; i=i+2); Printf (%d/n,); Getch (); } #include<stdio.h> # include <conio.h> Void main () { Int a; Clrscr (); For (a=1980; a2=2010; a=a+1) { if (a%d==0) Printf (%d is leap year ,) else Printf (%d not leap year, ); Getch(); }

Use Of While Loop : #include<stdio.h> # include <conio.h> Void main () { Int a; Clrscr (); a=1980; While (a<=2010) If (a %4==0) Printf (%d is leap year , a); else Printf (%d not leap year, a); } A=a+1 Getch (); }

Use Of Do-While Loop: #include<stdio.h> # include <conio.h> Void main () { Int a; Clrscr (); a=1980; do{ if(a%d==0) Printf (%d is leap year , a) else Printf (%d not leap year, a ); A++; } While (a<=2010); Getch (); }

Functions:
A function is a self contained block of statement that performs specific tasks of some kind. Function break large computing task into smaller ones, C has designed to make function efficient and easy to use. Every C program can be thought at a collection of function as there is no upper limit, but it must have at least one function, i.e. main function. We can call a function any number of times, so the function helps us to avoid rewriting the same code over and over.

Use of functions:
So function in a C program has some properties discussed below.

Every function has a unique name. This name is used to call function from main () function. A function can be called from within another function. A function is independent and it can perform its task without intervention from or interfering with other parts of the program. A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc. A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function. Two types of functions: In-Built Function: C language is collection of various inbuilt functions. If you have written a program in C then it is evident that you have used Cs inbuilt functions. Printf, scanf, clrscr etc. all are Cs inbuilt functions. You cannot imagine a C program without function.

User defined function: A general form of a C function looks like this:


<return type> FunctionName (Argument1, Argument2, Argument3) { Statement1; Statement2; Statement3; } An example of function int sum (int x, int y) { int result; result = x + y; return (result); }

Pointer
Pointer is a powerful loop to efficiently access the data by indicates reference pointer is a special variable which store the address of some other variable. It can hold memory address of a pointer variable also all memory address are unsigned integer. Therefore pointer takes two bytes space is memory. The biggest advantage of pointer is that a function can return more than one value though it uses an alt other different concept ( Is call by address). Consider the declaration, Int i=3; This declaration tells the c compiler to: A. Reserve space in memory to hold the integer value. B. Associate the name I with this memory location. C. Store the value 3 at this location.

I 3 65524

Location name Value at location Location number

We can print this address number through the following program:

Ex: 1 Void main() { Int I =3; Printf(\n Address of I =%u,&i); Printf(\n value of I =%d,i); }

The output of the above program would be: Address of I =65524 Value of I = 3 Ex: 2 Void main () { Int I =3; Printf(\n Address of I =%u&i); Printf(\n value of I =%d,i); Printf(\n value of i=%d,*(&i)); } The output of the above program would be: Address of I =65524 Value of I =3 Value of i =3 Ex: 3 Void main () { Int I =3; Int *j; J=&I; Printf(\n address of I =%u,&i); Printf(\n address of i=%u,j); Printf(\n address of j =%u,&j); Printf(\n value of j =%u,j); Printf(\n value of I =%d,i); Printf(\n value of i=%d,*(&i)); Printf(\n value of I =%d,*j); } The output of the above program would be: Address of I = 65524 Address of I =65524 Address of j =65522 Value of j = 65524 Value of I = 3 Value of I = 3 Value of I = 3 Call by value- whenever called a function passed something to it we have always passed the value of the variable to the called function. Such functions are called call by value. Even if any change done in called function it does not affect the actual argument.

#include <stdio.h> #include<conio.h> Void main () { Int a=10,b=20; Swap(&a,&b); calling by reference Getch(); } Swap (int x, int y); { Int z; *z=*x; *x=*y; *y=*z; Printf(%d%d,*x,*y); }

Call by reference- usually in c programming. We make a call by value. This means in general use
cannot alt the value of always arguments. But if desired it can always be achieved through a call by reference. In this method the address of actual argument of the calling function one which into the format arguments of the called function there we send the address of variable instead of the value of variables if there one any change if effects on the actual argument also, which one within calling function. #include <stdio.h> #include<conio.h> Void swap (int, int); Void main() { Int a, intb; A=50; b=100; Swap (a:b); Printf (a=%d b=%d,a,b); } Void swap (int,int); { int t; t=x; x=y; y=t; printf (x=%d,y=%d,x,y); }

Call by value

Array
Entering data into an array: - Usually we use a for loop to enter data into the elements of an array. Note that before using an array its type size must be mentioned.

Array initialization:- int num [6] = {2,4,12,5,45,6}; Passing array element to a function. Array element can be passed to a function by calling the function by value or by reference. Do while loop Pretested While loop post tested For loop pretested Array is a collection of similar data item continuous memory location. Single dimensional array:Font aliment of array is zero and carry n-1 position when array have single script is called one dimensional array. When array have more than two subscript is called multi-dimensional array (two dimensional). Syntax:Data type variable name [size]; Int a [5]; Memory location of 1D array. Two dimensional array:- Row & column fashion is called two dimensional array or multi-dimensional array. Syntax:Single dimensional #include<stdio.h> #include<conio.h> Void main () { Int a[5],I; For (i=0; i<=4; i++) { Scanf (%d,&a(i)); {for (i=0; i<4;i++) } Printf (%d, a (i)); } Getch (); } Two dimensional Variable name [size][size]; Int b[3][3]; Row column; Void main () { Int a [3][3],I, j; For (i=0;i<=2;i++) ,scanf (%d, &a*i+*j+); } For (i=0; i<=2; i++) For (j=0; j<=2; i++) ,printf (%d, a*i+*j+); } getch (); }

Structure
Array are used to store large set of data manipulate Them but the disadvantage is that all the element stored in an array one to be of the same data type. If we need to use a collection of different data type item it is not possible using an array. When we require using a collection of different data types item it not possible using an array. Structures are used to group together data items of varying types and sizes. When structure variable is defined, storage space allocated to it is sub-divided into different storage location for each data member. Data member could be of varying types and sizes and hence structure can be used to implement the concept of record in c language.

Structure creates memory space of different types and sizes while array creates memory space of same sizes and type. Structures are used to create a template for defining variables at subsequent stage. The keyword Struct is used for the purpose. Structure provides derived data type. To define a structure the statement of following type may be used. Struct<structure_tag> { Type member_name; Type member_name; Type member_name; Type member_name; }; To access individual data member of structure variable following notation is used. Structure name. Member EX:- #include<stdio.h> #include<conio.h> Struct student { Int roll; Char grade; }; Type def struct student stu; Void main () { Clrscr(); Stu s ; Printf(please enter roll no.& grade); Scanf(%d%c,&s.roll, &s.grade); Printf(roll no %d, grade %c, s.roll ,s.grade); }

Unions
Unions like structure contain member whose individual data types may different from one another. How even the member that compose a union all share the some storage area within the computer memory where at each member with in a structure is assigned its own union storage area. Two unions are used to conserve memory. They one useful for application involving multiple members. Where values need not be assigned to all the members at any one time. Like structure union can be declared using keyword union. Union item { Int m; Float p; Char c; Code; } The union contains three members each with a different data type. However we can use only one of them at a time. This because if only one location is allocated for union variables irrespective of size. The compiler allocates a piece of storage that is large enough to access a union member we can use the same syntax that we use a to access structure member, that is Code. M; Code. P;

File handling
File is named collection of data on same secondary storage media c supports a number of function to perform basic file operations. Some imports function is as following. F open () function:- F open () is a file handling function which is used to open a disk file for reading or writing operation. It receives two arguments of the type string. First argument is name of the file which is to be opened and second argument is mode of opening. It returns a pointer to the disk file. The different modes for opening a file are R= open the file for reading only if file does not exist returns a null pointer. W= open the file for writing only (discards its previous contents). If file does not exist creates it. A= open the file for appending data to it. Open the file for writing with its previous content. If file does not exist creates it. The syntax to use f open () is as follows: FILE*fp; Fp=f open (file name, mode);

Вам также может понравиться