//字符比较
#include "stdafx.h"
#define size 256
int main(int argc, char* argv[])
{
char str1[size] = "";
char str2[size] = "";
char * p1 = str1;
char * p2 = str2;
printf("please input the first string:");
gets(p1);
printf("please input the second string:");
gets(p2);
while(*p1 ==*p2 && '' ==*p1){
++p1;
++p2;
}
if(*p1 ==*p2)
printf("They are same.n");
else if (*p1 >*p2)
printf("The first one is larger than the second one.n");
else printf("The first one is smaller than the second one.n");
return 0;
}
#include "stdafx.h"
int main(int argc, char* argv[])
{
int height = 5;
int width = 3;
int girth;
int area;
girth = (height + width)*2;
area = height * width;
printf("长方形的周长是:%dn",girth);
printf("长方形的面积是:%dn",area);
return 0;
}
*/
//练习 2 编写程序,故意犯下的错误: 打印使用 %d 转换一个浮点数。打印使用 %f 转换的整数。打
//印一个字符使用 %d 转换
/*
#include "stdafx.h"
int main(int argc, char* argv[])
{
float a = 4.3;
int b = 3;
char c = 'a';
printf("%dn",a);
printf("%fn",b);
printf("%dn",c);
return 0;
}
*/
//Exercise 3
//使用一个枚举类型,表达:北京、上海、广州三个城市,然后输出他们的值。
/*
#include "stdafx.h"
#include<stdio.h>
enum city{beijing,shanghai,guangzhou};
int main(int argc, char* argv[])
{
enum city a,b,c;
a=beijing;
b = shanghai;
c = guangzhou;
printf("%d,%d,%dn",a,b,c);
return 0;
}
*/
/*
a,b 是变量……指针p和q保存的是a和b的地址 函数的参数传递其实就2种传递
值传递和引用传递 这个是值传递。值传递和引用传递针对的是变量本身,
而不是通过变量的值找到内存地址做什么改动。所以说你的函数
void swap(int *x, int *y)
指针 p,q代入后会生成2份拷贝 x,y
x保存的值是p的值,也就是a的地址,
y保存的值是q的值,也就是b的地址。
所以接下来针对x和y进行的值交换操作,
其结果只是为了让x保存了b的地址,y保存了a的地址,
x和y在函数结束调用后,生命周期结束。所以不会对指针p和q有任何的影响。
*/
#include "stdafx.h"
#include<stdio.h>
int main(int argc, char* argv[])
{
void swap(int *x,int*y);
int a = 3;
int b = 2;
int *p = &a;//3
int *q = &b;//2
swap(p,q);
printf("%dn",*p);//2
printf("%dn",*q);//3
return 0;
}
void swap(int *x,int*y){
int *temp = 0;
temp = x;
x = y;
y = temp;
}
最后
以上就是腼腆毛豆最近收集整理的关于c基础练习的全部内容,更多相关c基础练习内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复