Académique Documents
Professionnel Documents
Culture Documents
void main() { int number = 0; int *pointer = NULL; /* A pointer that can point to type int
*/
number = 10; printf("\n number's address: %p", &number); printf("\n number's value: %d\n\n", number); pointer = &number; /* Store the address of number in pointer */ printf("pointer's address: %p", &pointer); /* Output the address */ printf("\npointer's size: %d bytes", sizeof(pointer)); /* Output the size */ printf("\npointer's value: %p", pointer); /* Output the value (an address) */ printf("\nvalue pointed to: %d\n", *pointer); /* Value at the address */
int main() { int integerVariable; int *integerPointer; integerVariable = 2; printf("Thing %d\n", integerVariable); integerPointer = &integerVariable; *integerPointer = 3; printf("Thing %d\n", integerVariable); printf("Thing %d\n", *integerPointer); return (0); } main () { int i; int * ia; i = 10; ia = &i; printf ("The address of i is %8u \n", ia); printf ("its value is %d\n", i); printf ("its value is %d\n", *ia); *ia = 50; printf ("the value of i is %d\n", i); } int main(void) { int target, source; int *m; source = 10; m = &source; target = *m; printf("%d", target); } return 0;
Functions main() { int num[10],i,n,searchno; clrscr(); printf("enter num to be searched"); scanf("%d",&searchno); printf("enter n"); scanf("%d",&n); printf("enter number in list"); printf("the entered numbers are:"); for(i=0;i<n-1;i++) { scanf("%d",&num[i]); } for(i=0;i<n-1;i++) { if(num[i]==searchno) printf("the searchno is %d , loc is %d",num[i],i); else printf("not found"); break; } getch(); } void main(void) { int hour,min,sec,i; clrscr(); printf("ENTER HOURS:"); scanf("%d",&hour); printf("ENTER MINUTES:"); scanf("%d",&min); printf("ENTER SECONDS:"); scanf("%d",&sec); clrscr(); for(i=1;i<=I+1;i++) { sec=sec+1; if(sec==60) { sec=0; min=min+1; } if(min==60) { min=0; hour=hour+1; } if(hour==24) hour=0; clrscr(); gotoxy(10,25); printf("%02d:%02d:%02d",hour,min,sec); sleep(1); } getch(); }
{ int i; printf("\n"); for(i=0;i<30;i++) { printf("-"); } printf("\n"); } void main() { clrscr(); printf("Welcome to function in C"); printline(); printf("Function easy to learn."); printline(); getch(); }
int result; result = x+y; return(result); } void main() { int z; clrscr(); z = add(952,321); printf("Result %d.\n\n",add(30,55)); printf("Result %d.\n\n",z); getch(); } Functions with no arguments but returns value int send() { int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1); } void main() { int z; clrscr(); z = send(); printf("\nYou entered : %d.", z); getch(); }
int a=20, b=11, p,q; clrscr(); calc(a,b,&p,&q); printf("Sum = %d, Sub = %d",p,q); getch(); }
Structures
Structures in C are used to encapsulate, or group together different data into one object. You can define a Structure as shown below: struct object { char id[20]; int xpos; int ypos; };
Structure members can be initialized when you declare a variable of your structure: struct object player1
= {player1, 0, 0}; The above declaration will create a struct object called player1 with an id equal to player1, xpos equal to 0, and ypos equal to 0. To access the members of a structure, you use the . (scope resolution) operator. Shown below is an example of how you can accomplish initialization by assigning values using the scope resolution operator: Sample Code 1. 2. 3. 4. struct object player1; player1.id = player1; player1.xpos = 0; player1.ypos = 0;
9.
For completeness we shall include the full source of the above examples so you may see how it all fits together. object1.c: Sample Code 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. struct object createobj(char id[], int xpos, int ypos); 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. struct object createobj(char id[], int xpos, int ypos) { } printobj(player1); printobj(enemy1); struct object player1 = createobj("player1", 0, 0); struct object enemy1 = createobj("enemy1", 2, 3); void main() { void printobj(struct object obj); struct object { char id[20]; int xpos; int ypos; }; #include <stdio.h> #include <stdlib.h>
24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. } }
struct object newobj; strcpy(newobj.id, id); newobj.xpos = xpos; newobj.ypos = ypos; return newobj;
void printobj(struct object obj) { printf("name: %s, ", obj.id); printf("x position: %d, ", obj.xpos); printf("y position: %d", obj.ypos); printf("n");