330 likes | 498 Views
اشاره گر ها Pointers. char a[10] ; int num[20] ; num a int *b ;. reverse(s) abcde edcba. int x; int *ip , *q; ip = &x ; *ip=0 ; *ip = *ip + 10 ; /* x = 10 */ *ip += 1 ; /* x = 11 */ ++*ip ; /* x = 12 */
E N D
اشاره گر ها Pointers char a[10] ; int num[20] ; num a int *b ;
reverse(s) abcde edcba
int x; int *ip , *q; ip = &x ; *ip=0 ; *ip = *ip + 10 ; /* x = 10 */ *ip += 1 ; /* x = 11 */ ++*ip ; /* x = 12 */ (*ip)++ ; /* x = 13 */ *ip++ ; *(ip++) ; q = ip ; from = line line = from ;
y = y + 5 ; y += 5 ; z *= 20 ; t %= 6 ; t = t % 6 ; += -= *= /= %=
اشاره گر ها Pointers int a[10]; a &a[0] *a a[0] char student_name[40]; student_name &student_name[0]
int a[10] , x = 0; int *p1; a[1] a[0] a[9] a *a a[0] a[0] a a[1] a + 1 *(a + 1) a[1] a[i] a + i a[i] *(a + i) p1 = a; p1= &x;
int strlen(char s[ ]) { int i; i=0; while( s[i] != ‘\0’) ++i; return i ; }
hamid main() { char c ; char name[ ]=“hamid” ; c = *name character string كاراكتريرشته null 0 ‘\0’ ‘A’ “Hello , are you well today” c = *(name + 5) ; name name + 5 name + 1 string constant ثابت رشته اي
#include <stdio.h> main() { char *p; char name[50]; p = “ What is your name?\n” /* printf(“ What is your name?\n”); */ printf(“%s” , p); scanf(“%s” , name); printf(“%s” , name); } 86-3-9 كد 102
تابع scanf() ‘ ‘ و tab و newline را بعنوان جداكننده در نظر مي گيرد. لذا hamid rahimlu را 5 مي شمرد. #include <conio.h> #include <stdio.h> main() { int a, b , c ; clrscr(); scanf("%d%d%d" , &a , &b , &c ); printf("a = %d \t b = %d \t c = %d\n", a , b , c ); } نام برنامهscanf3.c C:\tc_current\Scanf3.exe <inputScanf3.txt از فايل ورودي مقادير a , b , c را مي خواند.
ASCII EBCDIC
Character arrays Enumeration Constants Recursive functions توابع بازگشتي
char name[30] ; char array-name[ size ] ; ثابت رشته اي در آرايه كاراكتري ذخيره مي شود . string constant “Behnam Ahmadi” “#%^jkl123)({} “ “dsgvghsd\\ghh”
int x = 5 ; مقدار اوليه دادن initialization main() int x ; --- --- x = 5 ; --- } scanf( “%d” , &x) ;
char name[] = “Behnam Ahmadi” ; name[0] = ‘B’ name[1] = ‘e’ name[12] = ‘i’ Name[13] = ‘\0’ char name[] = “Behnam123” ; Name[6] = ‘1’ Name[7] = ‘2’ x = ( name[7] – ‘0’ ) * 7
while ( there ‘s another line ) if ( it ‘s longer than the previous longest ) save it ; save it’s length print longest line
It is a book There are 41 students in this class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#include <stdio.h> #define MAXLINE 1000 /* maximum input line length */ int getline(char line[ ], int maxline); void copy(char to[], char from[]); /* print the longest input line */ main() {
/* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; }
/* getline: read a line into s, return length */ int getline(char *s,int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } line
/* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } line longest
x2 x >= 0 f(x) = -x3 x < 0
#include <stdio.h> #include <math.h> double func1(double ) ; main() { double x ; scanf(“%lf” , &x ) ; printf(“f(%lf) = %lf “ , x , func1(x) ); }
double func1( double x ) { if ( x >= 0 ) return pow( x , 2.0 ); else return -pow( x , 3.0 ) ; }
حلقه Do-while : حلقه هاي for و while شرط خاتمه حلقه را ابتدا test مي كنند. لذا اگر شرط غلط باشد دستور يا دستوراتي كه بدنه حلقه را تشكيل مي دهند حتي يكبار هم اجرا نمي شوند. سومين حلقه C به نام do-while بعد از هر بار اجراي دستورات شرط را تست مي كند لذا بدنه حلقه حداقل يكبار اجرا مي شود. do statement ; while ( expression ) ; do { statement1; statementN; } while ( expression ) ; ابتدا statement اجرا مي شود سپس شرط بررسي مي شود. اگر شرط True باشد statement دوباره اجرا مي شود و بهمين ترتيب . اگر شرط غلط باشد از كل ساختار خارج مي شود.
#include <stdio.h> /* strlen: return length of a string s */ تابعstrlen()int mystrlen( char *s) { int i = 0 ; while ( s[i] != ‘\0’ ) ++i ; return i ; } main() { printf( “ %d “ , mystrlen(“hello”) ); }
#include <dos.h> #include <stdio.h> #include <conio.h> int mystrlen( char *s ); main() { char name[40]; clrscr(); scanf("%s" , name); sleep(3); printf( "\n %d " , mystrlen(name) ); sleep(7); /* wait for 7 seconds */ } /* strlen: return length of a string s */ int mystrlen( char *s) { int i = 0 ; while ( *( s + i ) != '\0' ) ++i ; return i ; }
#include <dos.h> sleep( int sec) /* wait for specified seconds in sec */
#include <stdio.h> #include <conio.h> #include <math.h> double polart(double t , double z ); double polarr(double , double ); main() { double x , y ; clrscr(); printf("x ? \n"); scanf("%lf" , &x); printf("y ? \n"); scanf("%lf" , &y); printf("r = %6.2f\n", polarr(x , y ) ) ; printf("tetha = %6.2f\n", polart(x , y ) ) ; }
double polart(double x , double y ) { return atan2( y , x ); } double polarr(double x , double y) { return sqrt( pow(x , 2.0) + pow(y , 2.0) ) ; }
#include <stdio.h> #include <conio.h> #include <math.h> void polar(double x , double y , double *pr, double *pt ); main() { double x , y , r , t ; clrscr(); printf("x ? \n"); scanf("%lf" , &x); printf("y ? \n"); scanf("%lf" , &y); polar( x , y , &r , &t ); printf("r = %6.2f\n", r ) ; printf("tetha = %6.2f\n", t ) ; }
void polar(double x , double y , double *pr, double *pt ) { *pt = atan2( y , x ); *pr = sqrt( pow(x , 2.0) + pow(y , 2.0) ) ; return ; }
pt pr x y x y polar main