#include<stdio.h> void main() { puts("hello world"); int x=4; //the %p format will print out the location in hex(base lb) format printf("x lives at %p\n",&x); int * addr_of_x = &x; printf("x lives at %p\n",addr_of_x); printf("the content of addr_of_x is %d\n",*addr_of_x); printf("the size of int is %d\n",sizeof(int)); char quote[]="turtles!"; // this will return 9, which is 8 characters plus the \0 end character printf("the size of int is %d\n",sizeof(quote)); //why pointers have types? /** different data types has different size, when you do pointer arithmetic, the compiler will not know how to increce the value when you not specify the concrete data type of the pointer. **/ int doses[] = {1, 3, 2, 1000}; printf("Issue dose %i\n", 3[doses]); /* char name[40]; printf("Enter your name: "); scanf("%39s",name); printf("your name is :%s\n",name); int age; printf("Enter your age: "); scanf("%i\n", &age); printf("your age is :%i\n",age); //be careful with this , if your input is bigger than your variable to hold it. //it will cause buffer overflows char food[5]; printf("Enter favorite food: "); fgets(food, sizeof(food), stdin); printf("your favorite food is :%s\n",food); */ char *cards="JQK"; //string literals can never be updated //the following code will cause one error, //but it will pass compile //why cause this? the string literals will store one read only memory. //these constants will be used all threads, so can not changed. //cards[1]=‘K‘; puts(cards); printf("address of the cards is :%p\n",cards); printf("address of the string JQK is :%p\n",&"JQK"); char cards2[]="JQK"; puts(cards2); printf("address of the cards is :%p\n",cards2); } refer: head first C
时间: 2024-10-12 14:00:46