Recall
• Difference between while and do while?
• Difference between == and =
• Difference between break and continue?
• W...
Introduction to C
functions - Arrays – structures - pointers
Week 2- day 2
Functions
Functions
• A function is a group of statements that
together perform a task.
return_type function_name ( parameter list )...
Functions
int equation(int num1, int num2)
{
int result;
result = num1*num1+2*num1*num2+num2*num2;
return result;
}
Return...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter...
#include<stdio.h>
int equation(int a, int b) ;
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);...
#include<stdio.h>
int equation(int a, int b) ;
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);...
Arrays
Arrays
• Is a collection of data of same type with
common name
• Eg: int a[6]
A[5]
A[4]
A[3]
A[2]
A[1]
A[0]
Memory
Each Ce...
Example
Main()
{
int a[10];
For(i=0;i<10;i++)
Scanf(“%d”,&a[i]);
For(i=0;i<10;i++)
printf(“%d”, a[i]);
}`
Strings in C
• There is no string data type in C
• But we can do the same with character
array in C
– Eg: char a[8] =“baab...
main()
{
Char a[7]=“baabtra”; //Correct
Char *p=“baabtra”; //Correct
a=“baabtra”; //Incorrect
Scanf(“%s”,&a); //Correct
Sc...
Structures
• A structure is a collection of variables
under a single name
• These variables can be of different types,
and...
Why structures?
• A structure is a convenient way of
grouping several pieces of related
information together.
• Eg: suppos...
struct student
{
Char gender;
Int age;
}
main()
{
Struct student john,Mary;
John.age=15;
John.gender=“m”;
Mary.age=16;
Mar...
Pointers
• Pointers simply points to locations in
memory
• Each variables will be having a address in
the memory. So point...
Main()
{
int a,*p;
a=15;
P=&a;
Printf(“%d,%d ”,&a,p); // Will print 1001,1001
Printf (“%d, %d”,a,*p); // Will print 15,15
...
Character array as pointer
Character array is a pointer to the first location of
a group of locations
Eg : char a[10];
Her...
Questions?
“A good question deserve a good
grade…”
Self Check !!
Self - Check
#include<stdio.h>
Main()
{
Int a,b,c;
Printf(“Sum = %d”,sum(a,b));
}
Sum(int a,int b)
{
Return a+b;
}
Self - Check
#include<stdio.h>
Int sum(int a,int b)
Main()
{
Int a,b,c;
Printf(“Sum = %d”,sum(a,b));
}
Int sum(int a,int b...
Self- Check
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %...
Self- Check
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %...
Self- Check
• A pointer to a block of memory is
effectively same as an array
A.True
B.False
Self- Check
• A pointer to a block of memory is
effectively same as an array
A.True
B.False
Self- Check
Which of the following are themselves
a collection of different data types?
a) string
b) structures
c) char
d)...
Self- Check
Which of the following are themselves
a collection of different data types?
a) string
b) structures
c) char
d)...
Self- Check
Which of the following cannot
be a structure member?
a) Another structure
b) Function
c) Array
d) None of the ...
Self- Check
Which of the following cannot
be a structure member?
a) Another structure
b) Function
c) Array
d) None of the ...
Self- Check
#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%ct%c", p[0], s[1]);
}
a) Run time err...
Self- Check
#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%ct%c", p[0], s[1]);
}
a) Run time err...
Self- Check
#include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {10...
Self- Check
#include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {10...
Self- Check
#include <stdio.h>
void foo( int[] );
int main()
{
int ary[4] = {1, 2, 3, 4};
foo(ary);
printf("%d ", ary[0]);...
Self- Check
#include <stdio.h>
void foo( int[] );
int main()
{
int ary[4] = {1, 2, 3, 4};
foo(ary);
printf("%d ", ary[0]);...
End of Day 2
Upcoming SlideShare
Loading in...5
×

Introduction to c part 2

306

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
306
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to c part 2

  1. 1. Recall • Difference between while and do while? • Difference between == and = • Difference between break and continue? • What is the difference between i++ and ++i • What if i didn’t give break in switch? • What is the difference between logical operators and relational operators
  2. 2. Introduction to C functions - Arrays – structures - pointers Week 2- day 2
  3. 3. Functions
  4. 4. Functions • A function is a group of statements that together perform a task. return_type function_name ( parameter list ) { body of the function } Eg: main() { Printf(“hello world”);
  5. 5. Functions int equation(int num1, int num2) { int result; result = num1*num1+2*num1*num2+num2*num2; return result; } Return Type Function name Arguments
  6. 6. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; Printf(“%d”,c); }
  7. 7. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } //Function body / function Definition
  8. 8. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Should I use the same variable names? Are they same of what I used in main() ?
  9. 9. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Absolutely not !!! Variable a in equation() is different from varaible a in main(). There is no connection between those two
  10. 10. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } What is this return???
  11. 11. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Return will simply send value out of function to whomsoever it was called
  12. 12. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Can i write the whole function body after main function??
  13. 13. #include<stdio.h> int equation(int a, int b) ; Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Yes you can. But only additional thing you need to do is
  14. 14. #include<stdio.h> int equation(int a, int b) ; Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } You must declare it at the beginning
  15. 15. Arrays
  16. 16. Arrays • Is a collection of data of same type with common name • Eg: int a[6] A[5] A[4] A[3] A[2] A[1] A[0] Memory Each Cell will be size of int
  17. 17. Example Main() { int a[10]; For(i=0;i<10;i++) Scanf(“%d”,&a[i]); For(i=0;i<10;i++) printf(“%d”, a[i]); }`
  18. 18. Strings in C • There is no string data type in C • But we can do the same with character array in C – Eg: char a[8] =“baabtra”//enables us to store string with 6 characters /0 A R T B A A B A[7] A[6] a[5] a[4] a[3] a[2] a[1] a[0]
  19. 19. main() { Char a[7]=“baabtra”; //Correct Char *p=“baabtra”; //Correct a=“baabtra”; //Incorrect Scanf(“%s”,&a); //Correct Scanf(“%s”, a); //Correct;But why?? will discuss soon Printf(“%s”,a); }
  20. 20. Structures • A structure is a collection of variables under a single name • These variables can be of different types, and each has a name which is used to select it from the structure. struct struct_name { //Variable declarations; }
  21. 21. Why structures? • A structure is a convenient way of grouping several pieces of related information together. • Eg: suppose there are several variables called name,age,gender related to a student . So we can bring all of them under one name student using structures • Eg: struct student { Char name[25]; Int age;
  22. 22. struct student { Char gender; Int age; } main() { Struct student john,Mary; John.age=15; John.gender=“m”; Mary.age=16; Mary.gender=“f”; } age gender Student
  23. 23. Pointers • Pointers simply points to locations in memory • Each variables will be having a address in the memory. So pointer is just another variable which simply stores the address of it Main() { int a,b, *p,*q; 1002 1001 25 99 1004 1003 1002 1001 q p b a
  24. 24. Main() { int a,*p; a=15; P=&a; Printf(“%d,%d ”,&a,p); // Will print 1001,1001 Printf (“%d, %d”,a,*p); // Will print 15,15 } 1001 15 1002 1001 p a
  25. 25. Character array as pointer Character array is a pointer to the first location of a group of locations Eg : char a[10]; Here a stores memory address of a[0] main() { char a[10]="baabtra"; printf("%d %d",&a[0],a); // will print the same address A R T B A A B A[6] a[5] a[4] a[3] a[2] a[1] a[0] 1006 1005 1004 1003 1002 1001 1000
  26. 26. Questions? “A good question deserve a good grade…”
  27. 27. Self Check !!
  28. 28. Self - Check #include<stdio.h> Main() { Int a,b,c; Printf(“Sum = %d”,sum(a,b)); } Sum(int a,int b) { Return a+b; }
  29. 29. Self - Check #include<stdio.h> Int sum(int a,int b) Main() { Int a,b,c; Printf(“Sum = %d”,sum(a,b)); } Int sum(int a,int b) { Return a+b; }
  30. 30. Self- Check int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0; } A.2, 1, 15 B.1, 2, 5 C.3, 2, 15 D.2, 3,
  31. 31. Self- Check int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0; } A.2, 1, 1 5 B.1, 2, 5 C.3, 2, 1 5 D.2, 3, 2
  32. 32. Self- Check • A pointer to a block of memory is effectively same as an array A.True B.False
  33. 33. Self- Check • A pointer to a block of memory is effectively same as an array A.True B.False
  34. 34. Self- Check Which of the following are themselves a collection of different data types? a) string b) structures c) char d) All of the mentioned
  35. 35. Self- Check Which of the following are themselves a collection of different data types? a) string b) structures c) char d) All of the mentioned
  36. 36. Self- Check Which of the following cannot be a structure member? a) Another structure b) Function c) Array d) None of the mentioned
  37. 37. Self- Check Which of the following cannot be a structure member? a) Another structure b) Function c) Array d) None of the mentioned
  38. 38. Self- Check #include <stdio.h> void main() { char *s= "hello"; char *p = s; printf("%ct%c", p[0], s[1]); } a) Run time err b) h h c) h e d) h l
  39. 39. Self- Check #include <stdio.h> void main() { char *s= "hello"; char *p = s; printf("%ct%c", p[0], s[1]); } a) Run time err b) h h c) h e d) h l
  40. 40. Self- Check #include<stdio.h> struct course { int courseno; char coursename[25]; }; int main() { struct course c[] = { {102, "Java"}, {103, "PHP"}, {104, "DotNet"} }; printf("%d ", c[1].courseno); printf("%sn", (*(c+2)).coursename); return 0; } A.103 DotNet B.102 Java C.103 PHP D.104 DotNet
  41. 41. Self- Check #include<stdio.h> struct course { int courseno; char coursename[25]; }; int main() { struct course c[] = { {102, "Java"}, {103, "PHP"}, {104, "DotNet"} }; printf("%d ", c[1].courseno); printf("%sn", (*(c+2)).coursename); return 0; } A.104 DotNet B.102 Java C.103 PHP D.103 DotNet
  42. 42. Self- Check #include <stdio.h> void foo( int[] ); int main() { int ary[4] = {1, 2, 3, 4}; foo(ary); printf("%d ", ary[0]); } void foo(int p[4]) { int i = 10; p = &i; printf("%d ", p[0]); } a) 10 10 b) Compile time c) 10 1 d) Undefined be
  43. 43. Self- Check #include <stdio.h> void foo( int[] ); int main() { int ary[4] = {1, 2, 3, 4}; foo(ary); printf("%d ", ary[0]); } void foo(int p[4]) { Int i=10; p = &i; printf("%d ", p[0]); } a) 10 10 b) Compile time c) 10 1 d) Undefined be
  44. 44. End of Day 2
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×