码迷,mamicode.com
首页 > 其他好文 > 详细

Learning c section 1

时间:2015-03-29 13:37:39      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

#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

Learning c section 1

标签:

原文地址:http://www.cnblogs.com/huaxiaoyao/p/4375555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!