C presentation! BATRA COMPUTER CENTRE

51

Transcript of C presentation! BATRA COMPUTER CENTRE

C is a High level structured oriented programming language.

Used in general purpose programming

C was developed by DENNIS RITCHIE at AT& T Bell Laboratories in USA between 1969-1973.

C is a general-purpose programming language.

C is a programming language which born at “AT & T’s Bell Laboratory” of USA in 1972.

C was written by Dennis Ritchie, that’s why he is also called as father of c programming language.

C language was created for a specific purpose i.e., designing the UNIX operating system (which is currently base of many UNIX based OS From the beginning, C was intended to be useful to allow busy programmers to get things done because C is such a powerful, dominant and supple language

Its use quickly spread beyond Bell Labs in the late 70’s because of its long list of strong features.

Simple Portability Powerful Platform dependent Structure oriented Case sensitive Compiler based Modularity Middle level language Syntax based language Use of Pointers

In C, we have 32 keywords, which have their predefined meaning and cannot be used as a variable name. These words are also known as “reserved words”.

Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.

A data type in a programming language is a set of data with values having predefined characteristics. The language usually specifies the range of values for a given data type, how the values are processed by the computer, and how they are stored.

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program

Characters Allowed : Underscore(_)

Capital Letters ( A – Z )

Small Letters ( a – z )

Digits ( 0 – 9 )

Blanks & Commas are not allowed

No Special Symbols other than underscore(_) are allowed

First Character should be alphabet or Underscore

Variable name Should not be Reserved Word

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify. We have four different storage classes in a C program −

auto

register

static

extern

C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined.

Constants refer to fixed values. They are also called as literals

Constants may be belonging to any of the data type.

Syntax:

const data_type variable_name; (or) const data_type*variable_name;

An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition. There are some different type of operators:

A control statement is a statement that determines whether other statements will be executed.

Conditional statements are used to execute a statement or a group of statement based on certain conditions . Following are the conditional statements:

if

if else

else if

switch

goto

if(condition){Valid C Statements;}

Syntax for if statement in C :

If the condition is true the statements inside the

parenthesis { }, will be executed, else the control will

be transferred to the next statement after if.

if(condition){Valid C Statements;}else{Valid C Statements;}

Syntax for if :

In if else if the condition is true the statements

between if and else is executed. If it is false

the statement after else is executed.

switch(variable){case 1:Valid C Statements;break;--case n:Valid C Statements;break;default:Valid C Statements;break;}

Syntax :

Switch statements can

also be called matching

case statements. If

matches the value in

variable

(switch(variable)) with

any of the case inside,

the statements under

the case that matches

will be executed. If

none of the case is

matched the statement

under default will be

executed.

Basic syntax to use ‘for’ loop is:

for (variable initialization; condition to control loop; iteration of variable) { statement 1; statement 2; .. .. }

It is another loop like ‘for’ loop in C. But do-while loop allows execution of statements inside block of loop for one time for sure even if condition in loop fails.

Basic syntax to use ‘do-while’ loop is:

variable initialization; do { statement 1; statement 2; .. .. iteration of variable; } while (condition to control loop)

It is another loop like ‘do-while’ loop in C. The ‘while’ loop allows execution of statements inside block of loop only if condition in loop succeeds.

Basic syntax to use ‘while’ loop is:

variable initialization; while (condition to control loop) { statement 1; statement 2; .. .. iteration of variable; }

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

The general form of a function definition in C programming language is as follows −

Syntax:

return_type function_name( parameter list ) { body of the function }

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

Syntax:

int max(int num1, int num2);

An array is a collection of data items, all of the same type, accessed using a common name.

A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may.

Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

. Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.

Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets.

Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.

In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allocated only once, at the time the array is declared. The array does NOT change sizes later if the variable used to declare it changes. )

Examples:

int i, j, intArray[ 10 ], number; float floatArray[ 1000 ]; int tableArray[ 3 ][ 5 ];

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −

Syntax:

type *var-name;

Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication.

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.

Syntax:

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization then you can write the above statement as follows −

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.

There are numerous functions defined in "string.h" header file. Few commonly used string handling functions are discussed below:

Function Work of Function

strlen() Calculates the length of string

strcpy() Copies a string to another string

strcat() Concatenates(joins) two strings

strcmp() Compares two string

strlwr() Converts string to lowercase

strupr() Converts string to uppercase

Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds .Structures are used to represent a record

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows −

Syntax:

struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];

You can define pointers to structures in the same way as you define pointer to any other variable −

Syntax:

struct Books *struct_pointer;

Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the '&'; operator before the structure's name as follows −

Syntax:

struct_pointer = &Book1;

A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.

To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows −

Syntax:

union [union tag] { member definition; member definition; ... member definition; } [one or more union variables];

File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our data in Secondary memory (Hard disk). All files related function are available in stdio.h header file.

For achieving file handling in C we need follow following steps:

Naming a file

Opening a file

Reading data from file

Writing data into file

Closing a file

S.No Function Operation

1 fopen() To create a file

2 fclose() To close an existing file

3 getc() Read a character from a file

4 putc() Write a character in file

5 fprintf() To write set of data in file

6 fscanf() To read set of data from file.

5 getw() To read an integer from a file

6 putw() To write an integer in file

Data structure of file is defined as FILE in the standard I/O function. So all files should be declared as type FILE.

Before opening any file we need to specify for which purpose we open file, for example file open for write or read purpose.

Syntax:

FILE *fp; pf=fopen("filename", "mode");

S.No Mode Meaning Purpose

1 r Reading Open the file for reading only.

2 w Writing Open the file for writing only.

3 a Appending Open the file for appending (or adding) data to it.

4 r+ Reading + Writing New data is written at the beginning override existing

data.

5 w+ Writing + Reading Override existing data.

6 a+ Reading + Appending To new data is appended at the end of file.

S.No Function Operation Syntax

1 getc() Read a character from a file

getc( fp)

2 putc() Write a character in file

putc(c, fp)

3 fprintf() To write set of data in file

fprintf(fp, "control string", list)

4 fscanf() To read set of data from file.

fscanf(fp, "control string", list)

5 getw() To read an integer from a file.

getw(fp)

6 putw() To write an integer in file.

putw(integer, fp)

15 SCO , Dayal Bagh , Ambala CanttNear Panchmukhi Hanuman MandirPh: 4000670, 9729666670

E-Mail Id: [email protected] centre.com