includestdio h int num 1 num 1 is

  • Slides: 24
Download presentation

#include<stdio. h> int num 1; // num 1 is global variable void test(void); /*Function

#include<stdio. h> int num 1; // num 1 is global variable void test(void); /*Function Prototype*/ int main() { num 1 = 19; // no num 1 declaration printf ("line 1 (main) : num 1 = %dn", num 1); test(); printf ("line 2 (main) : num 1 = %dn", num 1); return 0; } void test() { num 1 = 26; // no num 1 declaration printf ("line 1 (test) : num 1 = %dn", num 1); } 4

#include<stdio. h> void test(void); /*Function Prototype*/ int main() { int num 1; // local

#include<stdio. h> void test(void); /*Function Prototype*/ int main() { int num 1; // local num 1 in main() num 1 = 19; printf ("line 1 (main) : num 1 = %dn", num 1); test(); printf ("line 2 (main) : num 1 = %dn", num 1); return 0; } void test() { int num 1; // local num 1 in test() num 1 = 26; printf ("line 1 (test) : num 1 = %dn", num 1); } 5

จงแสดง output จากโปรแกรมตอไปน #include<stdio. h> void test(void); /*Function Prototype*/ int num 1; //global num

จงแสดง output จากโปรแกรมตอไปน #include<stdio. h> void test(void); /*Function Prototype*/ int num 1; //global num 1 int main() { int num 1; // local num 1 in main() num 1 = 19; printf ("line 1 (main) : num 1 = %dn", num 1); test(); printf ("line 2 (main) : num 1 = %dn", num 1); return 0; } void test() { num 1 = 26; printf ("line 1 (test) : num 1 = %dn", num 1); } 6

Pass by Value Function 0100 6012 Computer Programming void func(int va) { va =

Pass by Value Function 0100 6012 Computer Programming void func(int va) { va = va+1; printf ("In function la = %dn", va); } 11

Pass by Reference Function 0100 6012 Computer Programming void func 2(int *pa) { *pa

Pass by Reference Function 0100 6012 Computer Programming void func 2(int *pa) { *pa = *pa +1; printf ("In function *pa = %dn", *pa); } 12

Main Function 0100 6012 Computer Programming int main() { int x; x = 10;

Main Function 0100 6012 Computer Programming int main() { int x; x = 10; printf ("Before call function x = %dn", x); func(x); printf ("After call function x = %dn", x); printf ("nn"); x = 10; printf ("Before call function x = %dn", x); func 2(&x); printf ("After call function x = %dn", x); return 0; } 13

Output 0100 6012 Computer Programming 14

Output 0100 6012 Computer Programming 14

ตวอยาง ฟงกชนกบอะเรย #include <stdio. h> int findmax( int * ); int main() { int

ตวอยาง ฟงกชนกบอะเรย #include <stdio. h> int findmax( int * ); int main() { int i, a[10]; printf ("Input 10 number "); for ( i=0; i<10; i++ ) scanf ( "%d", &a[i] ); printf ( “ nmax number = %d “ , findmax( a ) ); return 0; } 0100 6012 Computer Programming int findmax( int *p ) { int i, temp; for (i=0; i<9; i++) { if ( *(p+1) < *p ) { temp = *p; *p = *(p+1) ; *(p+1) = temp; } p++ ; } return *p; } 16

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { x=10;

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { x=10; printf("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } 0100 6012 Computer Programming OUTPUT x = 10 a = 3 b = 5 17

Q&A #include <stdio. h> #include <conio. h> int a; void func(int *x) { *x=10;

Q&A #include <stdio. h> #include <conio. h> int a; void func(int *x) { *x=10; printf("x = %dn", *x); } int main() { int b; a = 3; b = 5; func(&b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } 0100 6012 Computer Programming OUTPUT x = 10 a = 3 b = 10 18

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { a=10;

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { a=10; printf("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } 0100 6012 Computer Programming OUTPUT x = 5 a = 10 b = 5 19

คำถามทายบท (2) #include <stdio. h> #include <string. h> struct product { char name[15]; int

คำถามทายบท (2) #include <stdio. h> #include <string. h> struct product { char name[15]; int number; float price; }; struct product getdata(void) { struct product x; strcpy(x. name, "Chewing gum"); x. number = 100; x. price = 25. 5; return x; } ……… showdata(………………) { …………………. . } void sub 10(struct product &x) { if (x. number > 10) x. number -=10; } (1) 0100 6012 Computer Programming …………add 10(………………. . ) { x. number +=10; } int main(void) { int i; struct product y; y = getdata(); showdata(y); add 10(y); showdata(y); sub 10(y); showdata(y); return 0; } 21 (2)

คำถามทายบท (3) 2. ใหนกศกษาอธบายการทำงานของโปรแกรมน Hint: มทผด 1 จด 0100 6012 Computer Programming พรอมกบชใหเหนวาจดผดของโปรแกรมคอจด #include

คำถามทายบท (3) 2. ใหนกศกษาอธบายการทำงานของโปรแกรมน Hint: มทผด 1 จด 0100 6012 Computer Programming พรอมกบชใหเหนวาจดผดของโปรแกรมคอจด #include <stdio. h> #include <string. h> void callnum(int i, char y) { char data [10][8] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; strcpy(y, data[i]); } int main(void) { char ch[10]; int i; int telno[] = {0, 2, 7, 3, 0, 0, 0}; for (i=0; i<9; i++) { callnum(telno[i], ch); printf("%s ", ch); } return 0; 22 }

คำถามทายบท (4) 0100 6012 Computer Programming ใชเวลาคดไมเกน 10นาท 3. โปรแกรมตอไปนแสดงผลเลขอะไรจอภาพ เมอคดเสรจแลวใหลองพมพ code #include <stdio.

คำถามทายบท (4) 0100 6012 Computer Programming ใชเวลาคดไมเกน 10นาท 3. โปรแกรมตอไปนแสดงผลเลขอะไรจอภาพ เมอคดเสรจแลวใหลองพมพ code #include <stdio. h> fa(&i, j); ดงกลาวลงคอมพวเตอร และตรวจสอบคำตอบ int i; printf("n i=%d, j=%d", i, j); struct data { fb(i, &j); int i; printf("n i=%d, j=%d", i, j); int j; fc(X, &Y, Z); }W; printf("n W. i=%d, W. j=%d", W. i, W. j); printf("n X. i=%d, X. j=%d", X. i, X. j); void fa(int *a, int b); printf("n Y. i=%d, Y. j=%d", Y. i, Y. j); void fb(int c, int *d); printf("n Z. i=%d, Z. j=%d", Z. i, Z. j); void fc(struct data x, struct data *y, return 0; struct data z); } int main(void) { void fa(int *a, int b) { int j=0; i = 4; int i = 15; *a = *a+3; struct data X, Y, Z; b = i+2; X. i = 10; } X. j = 20; (1) 23 (2)

คำถามทายบท (5) 0100 6012 Computer Programming void fb(int c, int *d) { c +=

คำถามทายบท (5) 0100 6012 Computer Programming void fb(int c, int *d) { c += i; *d += i; } void fc(struct data x, struct data *y, struct data z) { y->i = x. j++; y->j = y->i + (++x. i); z. i = x. j; z. j = y->j; W. i = y->i + (++x. j); W. j = y->j + z. i; } (3) 24