ARRAY in C• Array is a collection or group of similar data type  elements stored in contiguous memory.• The individual dat...
Declaring single dimension Arrays• We can declare an array by specify its data  type, name and the number of elements the ...
Declaring single dimension Array• For example, to declare an integer array which  contains 10 elements we can do as follow...
Accessing Single Dimension Array• We can access array elements via  indexes array_name[index]. Indexes of array  starts fr...
Initializing Arrays• It is like a variable, an array can be initialized.• To initialize an array, you provide initializing...
Multi dimensional Arrays:• Often there is a need to store and manipulate  two dimensional data structure such as matrices ...
Declaring Multi Dimensional Arrayfloat table [50][50];char page[50][50];int num[3][3];      0    1   2    0    1    2
Accessing two dimensional arraysint values[2][2];values[0][0]=1;     values[0][1]=2;values[1][0]=3;     values[1][1]=4;int...
ARRAY AND STRINGS• The gets and puts functions:• The gets and puts functions facilitate the transfer  of strings between t...
Example of using gets and puts#include<stdio.h>void main(){    char inne[80];    gets(line);    puts(line);}The gets and p...
String library functions• strlen()• This function counts a number of characters  present in a string while giving a call t...
String library functions• strcpy()• This function copies the contents of one string  to another.• The base address of sour...
String library functions• strcat()• This function concatenates the source string  at the end of target sting.           ch...
String library functions• strcmp()• This function compares two strings and  returns some integer value.• If both the strin...
Understanding Functions• What is Function?• A function in C language is a block of code that  performs a specific task.• I...
Structure of a Function<return type> FunctionName (Argument1,Argument2, Argument3……){    Statement1;    Statement2;}int su...
Types of functions: A function may belong to any one of the following categories:• Functions with no arguments and no retu...
Advantages of using functions:• It makes possible      top   down     modular  programming.• The length of the source prog...
Function Example#include <stdio.h>void printMessage (){   printf ("Programming is fun.n");}void main (){   printMessage ();}
Function Which Return Values#include <stdio.h>int add (int x,int y){    int result; result=x+y;    return(result);}void ma...
Recursive Function• Recursive function is a function which  contains a call to itself.• Recursive function allows you to d...
Example of recursive function# include<stdio.h>int factorial(int number){     if(number <= 1)            return 1;    retu...
Scope Rule of Functionsvoid main( ){    int i = 20 ;    display ( i ) ;}void display ( int j ){    int k = 35 ;    printf ...
Session 4
Upcoming SlideShare
Loading in …5
×

Session 4

274 views

Published on

C Language Slides

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
274
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Session 4

  1. 1. ARRAY in C• Array is a collection or group of similar data type elements stored in contiguous memory.• The individual data items can be characters, integers, floating points numbers and so on .• Here contiguous memory allocation means array occupies contiguous bytes as needed in the memory.
  2. 2. Declaring single dimension Arrays• We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name.• Here is the syntax: data_type array_name[size];
  3. 3. Declaring single dimension Array• For example, to declare an integer array which contains 10 elements we can do as follows: int values[10];
  4. 4. Accessing Single Dimension Array• We can access array elements via indexes array_name[index]. Indexes of array starts from 0• With each subscript enclosed in square brackets.• For example, values[0]=5; values[1]=10; printf(“%d”,values[1]);
  5. 5. Initializing Arrays• It is like a variable, an array can be initialized.• To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.• Here is an example of initializing an integer array. int list[5] = {2,1,3,7,8};
  6. 6. Multi dimensional Arrays:• Often there is a need to store and manipulate two dimensional data structure such as matrices & tables.• Here the array has two subscripts. One subscript denotes the row & the other the column.• The declaration of two dimension arrays is as follows: data_type array_name[row_size][column_size]; int m[10][20];
  7. 7. Declaring Multi Dimensional Arrayfloat table [50][50];char page[50][50];int num[3][3]; 0 1 2 0 1 2
  8. 8. Accessing two dimensional arraysint values[2][2];values[0][0]=1; values[0][1]=2;values[1][0]=3; values[1][1]=4;int num[2][2]={1,2,3,4};int matrix[3][3]={{11,12,13}, {21,22,23}, {32,31,33}};
  9. 9. ARRAY AND STRINGS• The gets and puts functions:• The gets and puts functions facilitate the transfer of strings between the computer and the standard input/output devices.• Each of these functions accepts a single argument. The argument must be a data item that represents a string(e.g. character array).
  10. 10. Example of using gets and puts#include<stdio.h>void main(){ char inne[80]; gets(line); puts(line);}The gets and puts functions offer simplealternatives to the use of scanf and printf forreading and displaying strings
  11. 11. String library functions• strlen()• This function counts a number of characters present in a string while giving a call to the function. char msg[] = “Lord krishna”; int n; n=strlen(msg); printf(“length of string =%d”, n);
  12. 12. String library functions• strcpy()• This function copies the contents of one string to another.• The base address of source and target strings are supplied to the function. char source[] = “Lord Krishna”; char target[15]; strcpy(target, source); printf(“Source string is : %s”, source); printf(“target string is :%s”,target);
  13. 13. String library functions• strcat()• This function concatenates the source string at the end of target sting. char s[] = “Lord”; char t[] = “Krishna”; strcat(t,s); printf(“source string is %sn”, s); printf(“target stirng is %sn”, t);
  14. 14. String library functions• strcmp()• This function compares two strings and returns some integer value.• If both the strings are equal then it returns 0 otherwise it returns some other integer value. void main() { char s1[]=”Jerry”; char s2[]=”Ferry”; int j= strcmp(s1, s2); printf(“%d”, j);
  15. 15. Understanding Functions• What is Function?• A function in C language is a block of code that performs a specific task.• It is reusable i.e. it can be executed from as many different parts in a C Program as required.• It also optionally returns a value to the calling program
  16. 16. Structure of a Function<return type> FunctionName (Argument1,Argument2, Argument3……){ Statement1; Statement2;}int sum (int x, int y){ int result; result = x + y; return (result);}
  17. 17. Types of functions: A function may belong to any one of the following categories:• Functions with no arguments and no return values.• Functions with arguments and no return values.• Functions with arguments and return values.• Functions that return multiple values.• Functions with no arguments and return values.
  18. 18. Advantages of using functions:• It makes possible top down modular programming.• The length of the source program can be reduced.• It becomes uncomplicated to locate and separate a faulty function for further study.• c programmer can use function written by others, instead of starting over from scratch.
  19. 19. Function Example#include <stdio.h>void printMessage (){ printf ("Programming is fun.n");}void main (){ printMessage ();}
  20. 20. Function Which Return Values#include <stdio.h>int add (int x,int y){ int result; result=x+y; return(result);}void main (){ int total; total=add(10,12);
  21. 21. Recursive Function• Recursive function is a function which contains a call to itself.• Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily.• This is also a well-known computer programming technique: divide and conquer.
  22. 22. Example of recursive function# include<stdio.h>int factorial(int number){ if(number <= 1) return 1; return number * factorial(number - 1);}void main(){ int x = 5; printf("factorial of %d is %d",x,factorial(x));}
  23. 23. Scope Rule of Functionsvoid main( ){ int i = 20 ; display ( i ) ;}void display ( int j ){ int k = 35 ; printf ( "n%d", j ) ; printf ( "n%d", k ) ;}

×