c language - bestech solutions
TRANSCRIPT
Programming
History of C
Features of C
Structure of Program
Elements of C Program
1 Introduction Agenda
2
3
4
5
Control Statements
Complex Data Types
6 Input & Output Operations Agenda
7
8
9
10
Functions
Storage Class Specifier
Dynamic Memory Allocation
File Management
11 Pointers Agenda
12
13
14
15
Programming Adages
Post Assessment Test
Programming LanguageIntroduction
History of CIntroduction
C
B
BCPL
ALGOL
Portability Modularity
Flexibility
Extendability
Speed
Introduction Features of C
Documentation sectionLink Section
Definition SectionGlobal Declaration Section
main( ) Function Section {
Declaration partExecution part
}
Subprogram sectionFunction 1Function 2
…Function n
Structure Of Program
• The smallest individual units are known as C Tokens.
• Types
– Keywords
– Identifiers
– Constants
– Strings
– Operators
Elements of C Program C Tokens
• “Keywords” are the words which have special meaning to the C compiler.
• Some of the keywords are:
– auto
– double
– long
– static
– float
– struct
Elements of C Program C Tokens - Keywords
• Identifiers are the names of user-defined variables.
• Rules for identifiers.
• Example of valid identifiers:
Total, a1, SUM1, s_name, _Num
• Example of invalid identifiers:
1SUM,$_name,no., Student name
Elements of C Program C Tokens - Identifiers
• Constants are fixed values and can not be changed at run time.
• Types
– Integer constants
– Floating-point constants
– Character constants
– String constants
Elements of C Program C Tokens - Constants
• String is the sequence of characters.
• Any sequence or set of characters defined within double quotation symbols is a string.
• Examples
– “Good”
– “12345”
– “#412,krishnanagar”
– “$”
Elements of C Program C Tokens - String
• Operators are the symbols that represents specific actions.
• Operands are the variables that can be manipulated.
• Types of operators
– Unary
– Binary
– Ternary
– Special
Elements of C Program C Tokens - Operators
• Data type (or datatype) is a classification identifying one of various types of data.
– int
– long int
– float
– double
– char
– long double
Variable Data
Elements of C Program C Tokens – Data Types
• C standard library is a standardized collection of header files and library routines.
• stdio.h is a header file for standard input-output operations.• The console I/O functions are:
– getchar( )
– putchar( )
– gets( )
– puts( )
– printf( )
– scanf( )
Input & Output Operations
• Branches– if…else – switch
• Loop– for– while – do…while
• Jump– goto– break– continue– exit
Control Statements
Branches• If Statement
– if(expression)
statement;
– if(expression)
statement1;
else
statement2;
• Switch– switch(expression)
{ case 1: statements;
break; case 2: statements;
break; default: statements;}
Control Statements
• The For statement provides a compact way to iterate over a range of values.
• Syntax:
– for(initialization; test; inc/dec)
{
Body of the loop;
}
For LoopControl Statements
• The while statement is used to continually execute a block of statements until a condition remains true.
• Syntax:
– while(condition)
{
Body of the loop;
}
While LoopControl Statements
• The do-while is similar to the while statement but the condition is checked at the end of each iteration.
• Whether a condition is true or false, the loop get executed at least once.
• Syntax:do {
Body of the loop;
}while(condition);
do…while LoopControl Statements
• goto
• break
• continue
• exit
JumpControl Statements
• Complex data types are made up of basic primitive data types.
Complex Data Types
Types
Array String Structure
• An array is a set of values but all are of same basic data type.
• Syntax:
datatype arrayname[width];
• Types
– Single dimensional
– Two dimensional
– Multi dimensional
ArrayComplex Data Types
int array[5][5]; int array[5][5];for (int i=0; i < 5; i++) for(int i=0;i<5;i++)for (int j=0; j < 5; j++) for(int j=0; j<i; j++)array[i][j] = 1; array[i][j]=1;
Array TraversalComplex Data Types
• String is a null-terminated character array.
• Syntax:
char variable_name[size];
• String Manipulation:
– strlen( )
– strcat( )
– strcmp( )
– strcpy( )
– strrev( )
StringComplex Data Types
• Structure is a collection of dissimilar data types.• Syntax:
struct structure_name { data type member1; data type member2; … };
• union • typedef • enum• Nested Structure• Array of Structure
StructureComplex Data Types
• Function is a block of code that performs a specific task.
• Function Arguments.
• Function Prototypes:
– No Arguments and No Return Values.
– Arguments and No Return Values.
– Arguments and Return Values.
– No Arguments and Return Values.
• Recursive functions.
Functions
• Storage Class specifiers specify where the data is stored.
• It defines the default value, scope and the life time of a
variable.
• Four storage class specifiers:
• auto
• static
• register
• extern
• Syntax:
Storage-class-specifier datatype <variable-name>;
Storage Class Specifier
• Pointer is a variable that holds the address of another variable.
• Syntax:data type * <variable-name>;
• Operator:– & ( Address Operator )– * ( Addressed Operator)
• Pointers and Function:– Call by value– Call by reference
• Pointer of Pointer
Pointers
• Dynamic functions used in memory management.
• malloc( ) – ptr=(DataType*)malloc(byte-size);
• calloc( ) – ptr=(DataType*) calloc(n,elem-size);
• free( )– free(ptr);
• realloc( )– ptr=realloc(ptr,newsize);
Dynamic Memory Allocation
• A file is a collection of bytes stored on a secondary storage device.
• File Management functions are as follows:
– fopen() creates a new file and opens an existing file.
– fclose() closes a file which has been opened for use.
– getc() reads a character from a file
– putc() writes a character to a file
– fprintf() writes a set of data values to a file
File Management
THANK YOU !!!