C程序设计 WEEK3 variable、scanf、if-else

今天课上内容主要讲了变量的概念,关于变量需要注意变量从内存上的理解,以及变量的类型。

晚上实践有三大块,一是对不同类型的变量进行熟悉,二是理解输入语句scanf的使用,三是对分支控制if-else的使用

0 复习

0.1 使用for循环,输出 1,2,3,4,5,

0.2 使用for循环,输出1,3,5,7

0.3 使用for循环,输出100,200,300,400


1 变量

  1.1 不同变量的输出

#include <stdio.h>
void main(){
    int a=15;
    float b=138.3576278;
    double c=35648256.3645687;
    char d='p';
    printf("a=%d , %5d , %o , %x\n",a,a,a,a);
    printf("b=%f , %5.4f , %e\n",b,b,b);
    printf("c=%f , %8.4f \n",c,c);
    printf("d=%c , %8c\n",d,d);
}


1.2 不同变量的内存大小及最大值最小值 【可课下做】

#include <stdio.h>      /* for printf definition       */
#include <limits.h>     /* for CHAR_MIN, CHAR_MAX, etc */
#include <float.h>      /* for FLT_DIG, DBL_DIG, etc   */

main () {

  printf ("char           %d bytes %d to %d \n",   sizeof(char          ), CHAR_MIN, CHAR_MAX  );
  printf ("unsigned char  %d bytes %d to %d \n",   sizeof(unsigned char ), 0       , UCHAR_MAX );
  printf ("short          %d bytes %hi to %hi \n", sizeof(short         ), SHRT_MIN, SHRT_MAX  );
  printf ("unsigned short %d bytes %hu to %hu \n", sizeof(unsigned short), 0       , USHRT_MAX );
  printf ("int            %d bytes %i to %i \n",   sizeof(int           ), INT_MIN , INT_MAX   );
  printf ("unsigned int   %d bytes %u to %u \n",   sizeof(unsigned int  ), 0       , UINT_MAX  );
  printf ("long           %d bytes %li to %li \n", sizeof(long          ), LONG_MIN, LONG_MAX  );
  printf ("unsigned long  %d bytes %lu to %lu \n", sizeof(unsigned long ), 0       , ULONG_MAX );
  printf ("float          %d bytes %e to %e \n",   sizeof(float         ), FLT_MIN , FLT_MAX   );
  printf ("double         %d bytes %e to %e \n",   sizeof(double        ), DBL_MIN , DBL_MAX   );
  printf ("precision of float   %d digits\n", FLT_DIG);
  printf ("precision of double %d digits\n",  DBL_DIG);
}            


2 scanf

2.1 整形变量的输入(1)

#include <stdio.h>
 
int mult (int x, int y)
{
  return x * y;
}
 
int main()
{
  int x;
  int y;
 
  printf( "Please input two numbers to be multiplied: " );
  scanf( "%d", &x );
  scanf( "%d", &y );
  printf( "The product of your two numbers is %d\n", mult( x, y ) );
  return 0;
}

2.2 整形变量的输入(2)

#include <stdio.h>
void main(){
	int a,b,c;
	printf("input a,b,c\n");
	scanf("%d%d%d",&a,&b,&c);
	printf("a=%d,b=%d,c=%d",a,b,c);
} 

2.3 字符型变量的输入

#include <stdio.h>
void main(){
	char a,b;
	printf("input character a,b\n");
	scanf("%c%c",&a,&b);
	printf("%c%c\n",a,b);
} 

3 分支结构 if-else

3.1 仅有if

#include <stdio.h>
void main(){
	int a,b,max;
	printf("\n input two numbers: ");
	scanf("%d%d",&a,&b);
	max=a;
	if (max<b) 
		max=b;
	printf("max=%d",max);
}

3.2 if-else都有

#include <stdio.h>
void main(){
	int a, b;
	printf("input two numbers: ");
	scanf("%d%d",&a,&b);
	if(a>b)
		printf("max=%d\n",a);
	else
		printf("max=%d\n",b);
}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值