430 likes | 678 Views
C: Quick-start guide. Information Physics Soon-Hyung Yook. Outlook. Hello.c Basic syntax Variable and simple operations Loops Conditional sentence Array and pointer Function File I/O. Hello.c. The most famous program (in K&R Book) #include < stdio.h > /* header */ main() {
E N D
C: Quick-start guide Information Physics Soon-Hyung Yook
Outlook • Hello.c • Basic syntax • Variable and simple operations • Loops • Conditional sentence • Array and pointer • Function • File I/O
Hello.c • The most famous program (in K&R Book) #include <stdio.h> /* header */ main() { printf(“Hello, World\n”); return (0); } • Save the file: Hello.c • Compile and link (using gcc) > gccHello.c Generate an execution file: a.out • How to run? >a.out
Variable names • There are some restrictions on the names of variables and symbolic constants • Names are made of letters and digits • The first character must be a letter. • Upper case and lower case letters are distinct. • Some keywords such as if, else, int, float, etc. are reserved, and cannot be used as variable names.
Data types and size • char, unsigned char: 1 byte • short: 2 bytes • int, unsigned int: 4bytes • float: 4 bytes • double: 8bytes
Declaration • All variables must be declared before use • A declaration specifies a type, and contain a list of one or more variables of that type. • Examples: int lower, upper, step; char c, line[1000]; int x; float y; double z, width, height;
Operators • Arithmetic operators • +, -, *, /, % • Increment and decrement operators • ++, -- • Relational and logical operators • >, >=, <,<=, ==, != • &&, ||, ! • Bitwise operators • &, |, ^, >>, <<, ~ • Assignment operators: • =, +=, -=, /=, *= • sizeof
Basic syntax • Variables • int, float, double, char • Operations: +,-,*,/, ++, --,+=,/=,*=,-= #include <stdio.h> main() { inta,b; double c,d; char e; a=3; b=a+4; printf(“b=%d\n”,b); printf(“c=“); scanf(“%lf”,&c); d=c*0.8; printf(“d=%lf\n”,d); printf(“enter a character: “); scanf(“%c”,&c); printf(“you entered %c”,e); return (0); } • Save the file: variables.c • Compile and link (using gcc) • gccvariables.c –o variables.out • variables.out
Control Flow • Statements and blocks • statement: An expression such as x=0 is followed by a semicolon • Examples x=0; i++; • Block or compound statement: • Braces { and } are used to group declarations and statements together into a block.
Control Flow: if-else if (expression){ statements 1 … } else { statements 2 } No expression Yes statements 1 statements 2
Control Flow: switch switch(expression){ case const-expr: statements1 case const-expr: statements2 default: statements; }
Control Flow: do-while initialization initialization do{ statements 1 } while(expression); statements 2.. statements 1 Yes expression No statements 2
Control Flow: while initialization initialization while(expression){ statements 1 } statements 2.. No expression Yes statements 1 statements 2
Control Flow: for init for(init; expr1;expr2) { statements 1 } statements 2.. No expr1 Yes statements 1 expr2 statements 2
Basic syntax • #define • Loops • for, do-while, while #include <stdio.h> #define N 5 main() { inti; double c; c=0.5; for(i=0;i<5;i++){ c*=0.5; } printf(“%e\n”,c); c=0.5; i=0; do{ c*=0.5; i++; }while(i<5); printf(“%e\n”,c); c=0.5; i=0; while(i<5){ c*=0.5; i++; } printf(“%e\n”,c); return (0); } • Save the file: loops.c • Compile and link (using gcc) • gccloops.c –o loops.out • loops.out
Functions • functions break large computing tasks into smaller ones • code management • enhance the reusability • enhance the readability • reduce the errors • functions behave exactly the same way with the mathematical functions such as sin(x), etc. • when the function is called it should return a value
Functions • main() is the most important function in C programming • predefined functions: • printf(), scanf(), getchar(), rand(),sin(), cos(), exp(),… • one can define his/her own functions • scope of the variables • global variables • local variables
Functions • declaration type function_name(arg_type1,arg_type2); • definition type function_name(x1,x2) { inta,b; … do something return 1; }
Functions #include <stdio.h> #include <stdlib.h> #include <math.h> double Re_x_times_y(double,double,double,double); //declaration of function double Im_x_times_y(double,double,double,double); int main() { double xr,xi,yr,yi,zr,zi; xr=2.0; xi=4.0; // initialize x ; x=2.0+4.0i yr=3.0; yi=1.0; zr=Re_x_times_y(xr,xi,yr,yi); zi=Im_x_times_y(xr,xi,yr,yi); printf(“z=%lf+%lfi\n”,zr,zi); return 1; } double Re_x_times_y(double xr,doublexi,doubleyr,doubleyi) { double zr; zr=xr*yr-xi*yi; return zr; } double Im_x_times_y(double xr,doublexi,doubleyr,doubleyi) { double zi; zi=xi*yr+xr*yi; return zi; }
Functions • Function #include <stdio.h> #include <math.h> #define N 5 #define PI 3.142592 double Re_complex_sum(double,double); main() { double xim, xre; double yim, yre; double zim, zre; double rx, ry; rx=3.0; ry=4.0 xre=rx*cos(PI/4.0); xim=rx*sin(PI/4.0); yre=ry*cos(PI/6.0); yim=ry*sin(PI/6.0); zre=Re_complex_sum(xre,yre); zim=Im_complex_sum(xim,yim); return (0); } double Re_complex_sum(double x,double y) { double z; z=x+y; return z; } double Im_complex_sum(double x,double y) { double z; z=x+y; return z; } • Save the file: function.c • gccfunction.c –o function.out • function.out
Array • array is a consecutive memory block a: a[3] a[0] a[2] a[1] a[4]
Array • declaration char a[5]; intnum[10]; int b[]={1,2,3,4,5}; // declaration with initialization char name[100]=“Jane”;
Array #include <stdio.h> #include <stdlib.h> #define N 5 // another preprocessor—define a macro for constant expression int main() { int b[]={1,2,3,4,5}; char c[100]=“Name”; inti; for(i=0;i<N;i++){ printf(“b[%d]=%d\n”,i,b[i]); } printf(“c=%s\n”,c); return 1; }
Multidimensional array • int a[5][5]; a[0]: a[0][3] a[0][1] a[0][2] a[0][1] a[0][4] a[1]: a[1][3] a[1][1] a[1][2] a[1][1] a[1][4] a[2]: a[2][3] a[2][1] a[2][2] a[2][1] a[2][4] a[3]: a[3][3] a[3][1] a[3][2] a[3][1] a[3][4] a[4]: a[4][3] a[4][1] a[4][2] a[4][1] a[4][4]
Array #include <stdio.h> #include <stdlib.h> #include <math.h> #define N 2 intmatrix_multiply(double [2][2],double [2][2]); int main() { double a[2][2]={{1,2},{2,3}}; double b[2][2]={{1,1},{2,3}}; matrix_multiply(a,b); return 1; } intmatrix_multiply(double x[2][2],double y[2][2]) { inti,j,k; double z[N][N]={{0.,0.},{0.,0,}} for(i=0;i<N;i++){ for(k=0;k<N;k++){ for(j=0;j<N;j++){ z[i][k]+=x[i][j]*y[j][k]; } } } for(i=0;i<N;i++){ for(j=0;j<N;j++){ printf(“%lf\n”,z[i][j]);} printf(“\n”);} return 1; }
Pointer and Array • In C, there is a strong relationship between pointers and arrays. • Any operation that can be achieved by array subscripting can also be done with pointers. • The pointer version will in general faster, but at least to the uninitiated, somewhat harder to understand. int a[5]; int *pa; pa=&a[0]; pa: pa+1 pa+3 a: a[3] a[0] a[2] a[1] a[4] pa+2 pa+0 pa+4
Basic syntax • Array • Pointer #include <stdio.h> #include <math.h> #define N 5 main() { inti,a[N]; /* array */ double *c; /* pointer */ c=(double *)malloc(sizeof(double)*N); for(i=0;i<N;i++){ a[i]=i+4; *(c+i)=exp((double)i); } for(i=0;i<N;i++) printf(“a[%d]=%d c[%d]=%e\n”,a[i],c[i]); return (0); } • Save the file: array.c • Compile and link (using gcc) • gccarray.c –o array.out • array.out
Pointer and array #include <stdio.h> int swap(int,int); int swap2(int *,int *); main() { inta,b; a=3; b=1; swap(a,b); printf(“a=%d b=%d\n”,a,b); swap2(a,b); printf(“a=%d b=%d\n”,a,b); return (1); } int swap(inta,int b) { inttmp; tmp=a; a=b; b=tmp; return 1; } intswap2(int *a,int *b) { inttmp; tmp=*a; *a=*b; *b=tmp; return 1; }
Basic syntax • if…else, switch…case #include <stdio.h> #include <math.h> #define N 5 main() { inti,a[N]; /* array */ double *c; /* pointer */ c=(double *)malloc(sizeof(double)*N); for(i=0;i<N;i++){ a[i]=i+4; *(c+i)=exp((double)i); if(c>10.0){ printf(“c is larger than 10.0\n”); } else{ printf(“c is less than 10.0\n”); } } for(i=0;i<N;i++) printf(“a[%d]=%d c[%d]=%e\n”,a[i],c[i]); return (0); } • Save the file: ifelse.c • Compile and link (using gcc) • gccifelse.c –o ifelse.out • ifelse.out
Structure • A collection of one or more variables • possibly of different types • which is grouped together under a single name for convenient handling • Primitive version of class in OOP struct person { char name[10]; char address[100]; int age; double income; }; tag (optional)
Structure (usage) #include <stdio.h> int main() { struct { double x; double y; double z; } vec1; // declare vec1 structure vec1.x=22.0; vec1.y=2.0; vec1.z=-15.7; printf(“(x1,y1,z1)=(%lf,%lf,%lf)\n”,vec1.x,vec1.y,vec1.z); return 1; }
Structure (usage) #include <stdio.h> int main() { struct point { double x; double y; double z; }; struct point vec1; // declare vec1 structure vec1.x=22.0; vec1.y=2.0; vec1.z=-15.7; printf(“(x1,y1,z1)=(%lf,%lf,%lf)\n”,vec1.x,vec1.y,vec1.z); return 1; }
Structure (usage) #include <stdio.h> int main() { struct point { double x; double y; double z; }; struct point vec1; // declare vec1 structure vec1.x=22.0; vec1.y=2.0; vec1.z=-15.7; printf(“(x1,y1,z1)=(%lf,%lf,%lf)\n”,vec1.x,vec1.y,vec1.z); return 1; }
Structure (usage) #include <stdio.h> #include <math.h> structpoint { double x; double y; double z; }; double get_distance(structpoint,struct point); int main() { struct point vec1,vec2; // declare vec1 structure double d; vec1.x=22.0; vec1.y=2.0; vec1.z=-15.7; vec2.x=20.0; vec2.y=2.0; vec2.z=-15.7; d=get_distance(vec1,vec2); printf(“d=%lf\n”,d); return 1; } double get_distance(struct point r1,struct point r2) { double d; d=sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y)+(r1.z-r2.z)*(r1.z-r2.z)); return d; }
Structure (usage) #include <stdio.h> #include <math.h> typedefstructpoint { double x; double y; double z; } Point; double get_distance(Point,Point); int main() { Point vec1,vec2; // declare vec1 structure double d; vec1.x=22.0; vec1.y=2.0; vec1.z=-15.7; vec2.x=20.0; vec2.y=2.0; vec2.z=-15.7; d=get_distance(vec1,vec2); printf(“d=%lf\n”,d); return 1; } double get_distance(Point r1,Point r2) { double d; d=sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y)+(r1.z-r2.z)*(r1.z-r2.z)); return d; }
Arrays of Structure #include <stdio.h> #include <math.h> typedefstructpoint { double x; double y; double z; } Point; double get_distance(Point,Point); int main() { Pointvec[2]; // declare vec1 structure double d; vec[0].x=22.0; vec[0].y=2.0; vec[0].z=-15.7; vec[1].x=20.0; vec[1].y=2.0; vec[1].z=-15.7; d=get_distance(vec[0],vec[1]); printf(“d=%lf\n”,d); return 1; } double get_distance(Point r1,Point r2) { double d; d=sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y)+(r1.z-r2.z)*(r1.z-r2.z)); return d; }
Arrays of Structure #include <stdio.h> #include <math.h> typedefstructpoint { double x; double y; double z; } Point; double get_distance(Point,Point); int main() { Pointvec[2]; // declare vec1 structure double d; vec->x=22.0; vec->y=2.0; vec->z=-15.7; (vec+1)->x=20.0; (vec+1)-> y=2.0; (vec+1)->z=-15.7; d=get_distance(vec[0],vec[1]); printf(“d=%lf\n”,d); return 1; } double get_distance(Point r1,Point r2) { double d; d=sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y)+(r1.z-r2.z)*(r1.z-r2.z)); return d; } • ->: member operator
Pointer to Structure #include <stdio.h> #include <math.h> #include <stdlib.h> typedefstructpoint { double x; double y; double z; } Point; double get_distance(Point,Point); int main() { Point *vec; // declare vec1 structure double d; vec=(Point *)malloc(sizeof(Point)*2); vec->x=22.0; vec->y=2.0; vec->z=-15.7; (vec+1)->x=20.0; (vec+1)-> y=2.0; (vec+1)->z=-15.7; d=get_distance(vec[0],vec[1]); printf(“d=%lf\n”,d); free(vec); return 1; } double get_distance(Point r1,Point r2) { double d; d=sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y)+(r1.z-r2.z)*(r1.z-r2.z)); return d; }
Union • A variable that may hold objects of different types and sizes • compiler keep track of size and alignment requirement • union provides a way to manipulate different kinds of data in a single area of storage, without embedding any machine-dependent information in the program. • members of a union are accessed in the same way as the case of structure
Union Example #include <stdio.h> typedef union my_data{ double d; inti; } MyData; double get_distance(Point,Point); int main() { MyData a; a.d=1234.0; printf(“d: %lf i:%d\n”,a.d, a.i); a.i=1234; printf(“d: %lf i:%d\n”,a.d, a.i); return 1; }
Basic File IO • file pointer • open a file • do something • close the file • basic IO functions which would be frequently used in the following class are • fprintf(filepointer,”….”,variables); • fscanf(filepointer,”…”,pointers of variables);
Basic syntax • File I/O #include <stdio.h> #include <math.h> #define N 5 #define PI 3.142592 double Re_complex_sum(double,double); main() { double xim, xre; double yim, yre; double zim, zre; double rx, ry; FILE *fp; rx=3.0; ry=4.0 xre=rx*cos(PI/4.0); xim=rx*sin(PI/4.0); yre=ry*cos(PI/6.0); yim=ry*sin(PI/6.0); zre=Re_complex_sum(xre,yre); zim=Im_complex_sum(xim,yim); fp=fopen(“file_name.res”,”w”); fprintf(fp,”%e+%ei\n”,zre,zim); fclose(fp); return (0); } double Re_complex_sum(double x,double y) { double z; z=x+y; return z; } double Im_complex_sum(double x,double y) { double z; z=x+y; return z; } • Save the file: file_io.c • gccfile_io.c –o file_io.out • file_io.out