C语言学习1

一、初识C语言

1.1 C语言的起源

  1972年,贝尔实验室的丹尼斯,里奇和肯,汤普逊在开发UNIX操作系统时设计了C语言,然而,C语言不完全是里奇突发奇想出来的,他是在B语言的基础上进行设计的,至于B语言的起源是另外一个故事,C语言设计的初衷是将其作为程序员使用的的一种编程工具,因此,其主要主要目标是成为有用的语言。

1.2 C语言的优缺点

优点:

1.设计特性:C语言程序编写更易懂、更可靠。

2.高效性:C程序更加紧凑,而且运行速度快

3.可移植性:在一种系统C程序稍作修改,或不做修改就可以在其他系统运行。

4.强大的灵活性:许多的编译器和解释器都是用C语言编写的(FORTRAN、Perl、python、BASIAC...)。

5.面向程序员:C语言是为了满足程序员的需求而设计的,程序员可以通过C访问硬  件,操控内存中的位

缺点:C语言使用指针,而涉及指针的编程错误往往难以察觉。

二、C语言概述

2.1 简单的C程序示例

 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     int num;
 6     num = 1;
 7 
 8     printf("I an a simple");
 9     printf(" coputer.\n");
10     printf("My favorite number is %d because it is the first number\n");
11 }

运行结果如下:

:如何进行注释? 

单行注释://

多行注释:/* 注释内容 */

2.2 变量命名

      可以用小写字母,大写字母,数字和下划线(_)来命名,而且,名称的第一个字符必须是字母或者下划线不能是数字

2.3 进一步使用C

 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     int feet, fathoms;
 6 
 7     fathoms = 2;
 8     feet = 6 * fathoms;
 9     printf("There are %d feet in %d fathoms!\n", feet, fathoms);
10     printf("Yes, I said %d feet!\n", feet);
11 }

 运行结果如下:

 三、数据和C

3.1 程序示例

 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     float weight;
 6     float value;
 7 
 8     printf("Are you worth your weight in platinum?\n");
 9     printf("Let's check it out.\n");
10     printf("Please enter your weight in pounds:");
11     scanf("%f",&weight);
12     value = 1700.0 * weight * 14.5833;
13     printf("Your weight in platinum is worth $%.2f.\n",value);
14     printf("You are easily worth that! If platinum prices drop,\n");
15     printf("eat more to maintain your value.\n");
16 }

运行结果:

猜你喜欢

转载自www.cnblogs.com/wangkeqi/p/9261105.html