您现在的位置是:首页 > 学无止境 > C语言网站首页C语言 如何学习C语言、入门C语言,看这篇就够了
如何学习C语言、入门C语言,看这篇就够了
- C语言
- 2019-11-10
简介该文章将会把C语言基础语法梳理一遍,主要记录帮助理解相关知识点的代码示例和重要或容易忽略的面试知识,将会陆续发布系类文章.字数
24775
控制语句
1.程序 顺序执行
/* 计算一个三角形的面积,其中三角形三条边一知 */
#include <stdio.h>
#include "math.h"
int main(int argc, char const *argv[])
{
float a,b,c,s,area;
/* 输入三条边的边长*/
printf("\n Enter the three sides: ");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea of triangle: %f\n",area);
}
输出:
Enter the three sides: 10 9.2 3.5
Area of triangle: 16.081221
Program ended with exit code: 0
2.程序 if···else
/* 判断一个数字是正数还是负数 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int num;
printf("\nType an integer number: ");
scanf("%d",&num);
if (num==0) {
printf("\nIt is zero.");
} else if(num>0){
printf("\n%d is positive.\n");
}else{
printf("\n%d is negative.\n");
}
}
输出:
Type an integer number: 5
0 is positive.
3.程序 判断某年是否为闰年.
/* 判断某年是否为闰年.*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
int year;
printf("\nEnter a year: ");
scanf("%d",&year);
if (year % 100 != 0 && year % 4 == 0 || year % 400 ==0) {
printf("\n%d is leap year.\n",year);
} else {
printf("\n%d is not leap year.\n",year);
}
}
输出;
Enter a year: 2012
2012 is leap year.
4.程序 计算二次方程的根
/* 计算二次方程的根 */
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int a,b,c;
float x1,x2,x,d,r,r1;
printf("\nType the values of a,b,c: ");
scanf("%d%d%d",&a,&b,&c);
/* 计算判别式*/
d = b*b-4*a*c;
if (d>0) {
printf("\nRoots are unequal and real:\n");
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("Root1 = %.2f\n",x1);
printf("Root2 = %.2f\n",x2);
} else if(d==0){
printf("\nRoots are equal and real:\n");
x = -b/(2*a);
printf("Root1 = %.2f\n",x);
printf("Root2 = %.2f\n",x);
}else{
printf("Roots are complex and imaginary:\n");
d = -d;
r = -b/(2*a);
r1 = sqrtf(d)/(2*a);
printf("Root1 = %.2f+%.2fi\n",r,r1);
printf("Root2 = %.2f-5.2fi\n",r,r1);
}
}
输出:
Type the values of a,b,c: 1 2 -63
Roots are unequal and real:
Root1 = 7.00
Root2 = -9.00
5.程序 do···while循环
/* 计算一个整数中的各位数字之和 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int num,digit,sum = 0;
printf("Enter a number: ");
scanf("%d",&num);
do {
digit = num % 10;
sum += digit;
num /= 10;
} while (num>0);
printf("The sum of dights= %d.\n",sum);
}
输出:
Enter a number: 1037
The sum of dights= 11.
6.程序1 while循环
/* 计算给定整数的阶乘 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i,n;
long int fact = 1;
printf("Enter a positive integer: ");
scanf("%d",&n);
i = 1;
while(i<=n) {
fact*=i;
i++;
}
printf("Factorial of %d is %ld.\n",n,fact);
}
输出:
Enter a positive integer: 5
Factorial of 5 is 120.
7.程序2 while循环
/* 计算一个数值的乘方 */
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int number,power,counter = 1;
long int result1 = 1;
long int result2 = 1;
printf("Enter number,power: ");
scanf("%d,%d",&number,&power);
while (counter <= power) {
result1 *=number;
++counter;
}
printf("%d rised to the power of %d = %d",number,power,result1);
result2 = pow(number,power);
printf("\n%d rised to the power of %d = %d\n",number,power,result2);
}
输出:
Enter number,power: 5,4
5 rised to the power of 4 = 625
5 rised to the power of 4 = 625
8.程序 for 循环 生成斐波那契序列
* 生成斐波那契序列 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i,n;
long unsigned int f1,f2,f;
printf("How many Fibonaccis you want? ");
scanf("%d",&n);
f1 = 0;
f2 = 1;
if(n==1) printf("%lu",&f1);
else if (n==2){
printf("%lu ",f1);
printf("%lu ",f2);
}else{
printf("%lu ",f1);printf("%lu ",f2);
for (i=3; i<=n; i++) {
f = f1+f2;
printf("%lu ",f);
f1 = f2;
f2 = f;
}
}
}
输出:
How many Fibonaccis you want? 10
0 1 1 2 3 5 8 13 21 34
9.程序 continue语句
/* continue语句 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i;
for (i=10; i>=1; i--) {
printf("\nBefore continue: ");
if (i>5)
continue;
printf("%d",i);
}
}
输出:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue: 5
Before continue: 4
Before continue: 3
Before continue: 2
Before continue: 1
10. break语句和continue语句的区别是什么?
(重要的面试题) break语句径直跳到当前代码快=块的末尾,而continue语句会跳过当前循环的剩余代码,回到循环开始处.
11. 程序1 return语句
/* 调用一个函数,被调用的这个函数计算给定值的平方根并返回该值. */
#include <stdio.h>
#include <math.h>
/* 该函数接收参数y的值,返回他的平方根*/
double squareroot(double y){
double x = sqrt(y); // 将与的平方根保存在变量x中
return x; //返回y的平方根
}
int main(int argc, char const *argv[])
{
double y,result;
printf("Enter a number: ");
scanf("%lf",&y);
/*调用squareroot()函数,计算y的平方根*/
result = squareroot(y);
printf("Sequare root value= %lf.\n",result);
}
输出:
Enter a number: 16.9
Sequare root value= 4.110961.
12. 程序2 return语句
/* 计算正弦级数*/
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int n,i,j = 1;
double x,r,t,sum;
printf("Enter no. of interations: ");
scanf("%d",&n);
printf("Enter the value of angle: ");
scanf("%lf",&x);
/* 将角度转化为弧度*/
r = (x*3.14159)/180;
/*用变量t保存级数第1项*/
t = r;
/*到目前为止所有项之和*/
sum = r;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("Iteration = %d\tSum = %lf",j,sum);
/*第2项的分母*/
i = 2;
/*循环计算第2项至第n项*/
for (j=2; j<=n; j++) {
/*计算下一项*/
t = (-1)*t*r*r/(i*(i+1));
/*将该项累加*/
sum = sum+t;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("\nIteration = %d\tSum = %lf",j,sum);
/* 将变量加2,用于下一项的分母中*/
i = i+2;
}
}
输出:
Enter no. of interations: 10
Enter the value of angle: 60
Iteration = 1 Sum = 1.047197
Iteration = 2 Sum = 0.855800
Iteration = 3 Sum = 0.866295
Iteration = 4 Sum = 0.866021
Iteration = 5 Sum = 0.866025
Iteration = 6 Sum = 0.866025
Iteration = 7 Sum = 0.866025
Iteration = 8 Sum = 0.866025
Iteration = 9 Sum = 0.866025
Iteration = 10 Sum = 0.866025
12. 程序3 return语句
/* 计算指数级数*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
int n,j = 1;
double x,t,sum;
printf("Enter no. of iterations: ");
scanf("%d",&n);
printf("Enter the power value: ");
scanf("%lf",&x);
// 第一项等于1
t = 1;
/*到目前为止所有项之和*/
sum = t;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("\nIteration = %d\tSum = %lf",j,sum);
/*迭代计算第2项至第n项*/
for (j = 1; j < n; j++) {
/*计算下一项*/
t = t * x/j;
/*将该项累加*/
sum = sum+t;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("\nIteration = %d\tSum = %lf",j+1,sum);
}
}
输出:
Enter no. of iterations: 15
Enter the power value: 2.0
Iteration = 1 Sum = 1.000000
Iteration = 2 Sum = 3.000000
Iteration = 3 Sum = 5.000000
Iteration = 4 Sum = 6.333333
Iteration = 5 Sum = 7.000000
Iteration = 6 Sum = 7.266667
Iteration = 7 Sum = 7.355556
Iteration = 8 Sum = 7.380952
Iteration = 9 Sum = 7.387302
Iteration = 10 Sum = 7.388713
Iteration = 11 Sum = 7.388995
Iteration = 12 Sum = 7.389046
Iteration = 13 Sum = 7.389055
Iteration = 14 Sum = 7.389056
Iteration = 15 Sum = 7.389056
转载:
感谢您对莫愁个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源莫愁个人博客 https://www.mochoublog.com/study/367.html”。
- C语言
- 2019-11-10
控制语句
1.程序 顺序执行
/* 计算一个三角形的面积,其中三角形三条边一知 */
#include <stdio.h>
#include "math.h"
int main(int argc, char const *argv[])
{
float a,b,c,s,area;
/* 输入三条边的边长*/
printf("\n Enter the three sides: ");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea of triangle: %f\n",area);
}
输出:
Enter the three sides: 10 9.2 3.5
Area of triangle: 16.081221
Program ended with exit code: 0
2.程序 if···else
/* 判断一个数字是正数还是负数 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int num;
printf("\nType an integer number: ");
scanf("%d",&num);
if (num==0) {
printf("\nIt is zero.");
} else if(num>0){
printf("\n%d is positive.\n");
}else{
printf("\n%d is negative.\n");
}
}
输出:
Type an integer number: 5
0 is positive.
3.程序 判断某年是否为闰年.
/* 判断某年是否为闰年.*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
int year;
printf("\nEnter a year: ");
scanf("%d",&year);
if (year % 100 != 0 && year % 4 == 0 || year % 400 ==0) {
printf("\n%d is leap year.\n",year);
} else {
printf("\n%d is not leap year.\n",year);
}
}
输出;
Enter a year: 2012
2012 is leap year.
4.程序 计算二次方程的根
/* 计算二次方程的根 */
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int a,b,c;
float x1,x2,x,d,r,r1;
printf("\nType the values of a,b,c: ");
scanf("%d%d%d",&a,&b,&c);
/* 计算判别式*/
d = b*b-4*a*c;
if (d>0) {
printf("\nRoots are unequal and real:\n");
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("Root1 = %.2f\n",x1);
printf("Root2 = %.2f\n",x2);
} else if(d==0){
printf("\nRoots are equal and real:\n");
x = -b/(2*a);
printf("Root1 = %.2f\n",x);
printf("Root2 = %.2f\n",x);
}else{
printf("Roots are complex and imaginary:\n");
d = -d;
r = -b/(2*a);
r1 = sqrtf(d)/(2*a);
printf("Root1 = %.2f+%.2fi\n",r,r1);
printf("Root2 = %.2f-5.2fi\n",r,r1);
}
}
输出:
Type the values of a,b,c: 1 2 -63
Roots are unequal and real:
Root1 = 7.00
Root2 = -9.00
5.程序 do···while循环
/* 计算一个整数中的各位数字之和 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int num,digit,sum = 0;
printf("Enter a number: ");
scanf("%d",&num);
do {
digit = num % 10;
sum += digit;
num /= 10;
} while (num>0);
printf("The sum of dights= %d.\n",sum);
}
输出:
Enter a number: 1037
The sum of dights= 11.
6.程序1 while循环
/* 计算给定整数的阶乘 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i,n;
long int fact = 1;
printf("Enter a positive integer: ");
scanf("%d",&n);
i = 1;
while(i<=n) {
fact*=i;
i++;
}
printf("Factorial of %d is %ld.\n",n,fact);
}
输出:
Enter a positive integer: 5
Factorial of 5 is 120.
7.程序2 while循环
/* 计算一个数值的乘方 */
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int number,power,counter = 1;
long int result1 = 1;
long int result2 = 1;
printf("Enter number,power: ");
scanf("%d,%d",&number,&power);
while (counter <= power) {
result1 *=number;
++counter;
}
printf("%d rised to the power of %d = %d",number,power,result1);
result2 = pow(number,power);
printf("\n%d rised to the power of %d = %d\n",number,power,result2);
}
输出:
Enter number,power: 5,4
5 rised to the power of 4 = 625
5 rised to the power of 4 = 625
8.程序 for 循环 生成斐波那契序列
* 生成斐波那契序列 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i,n;
long unsigned int f1,f2,f;
printf("How many Fibonaccis you want? ");
scanf("%d",&n);
f1 = 0;
f2 = 1;
if(n==1) printf("%lu",&f1);
else if (n==2){
printf("%lu ",f1);
printf("%lu ",f2);
}else{
printf("%lu ",f1);printf("%lu ",f2);
for (i=3; i<=n; i++) {
f = f1+f2;
printf("%lu ",f);
f1 = f2;
f2 = f;
}
}
}
输出:
How many Fibonaccis you want? 10
0 1 1 2 3 5 8 13 21 34
9.程序 continue语句
/* continue语句 */
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i;
for (i=10; i>=1; i--) {
printf("\nBefore continue: ");
if (i>5)
continue;
printf("%d",i);
}
}
输出:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue: 5
Before continue: 4
Before continue: 3
Before continue: 2
Before continue: 1
10. break语句和continue语句的区别是什么?
(重要的面试题) break语句径直跳到当前代码快=块的末尾,而continue语句会跳过当前循环的剩余代码,回到循环开始处.
11. 程序1 return语句
/* 调用一个函数,被调用的这个函数计算给定值的平方根并返回该值. */
#include <stdio.h>
#include <math.h>
/* 该函数接收参数y的值,返回他的平方根*/
double squareroot(double y){
double x = sqrt(y); // 将与的平方根保存在变量x中
return x; //返回y的平方根
}
int main(int argc, char const *argv[])
{
double y,result;
printf("Enter a number: ");
scanf("%lf",&y);
/*调用squareroot()函数,计算y的平方根*/
result = squareroot(y);
printf("Sequare root value= %lf.\n",result);
}
输出:
Enter a number: 16.9
Sequare root value= 4.110961.
12. 程序2 return语句
/* 计算正弦级数*/
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int n,i,j = 1;
double x,r,t,sum;
printf("Enter no. of interations: ");
scanf("%d",&n);
printf("Enter the value of angle: ");
scanf("%lf",&x);
/* 将角度转化为弧度*/
r = (x*3.14159)/180;
/*用变量t保存级数第1项*/
t = r;
/*到目前为止所有项之和*/
sum = r;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("Iteration = %d\tSum = %lf",j,sum);
/*第2项的分母*/
i = 2;
/*循环计算第2项至第n项*/
for (j=2; j<=n; j++) {
/*计算下一项*/
t = (-1)*t*r*r/(i*(i+1));
/*将该项累加*/
sum = sum+t;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("\nIteration = %d\tSum = %lf",j,sum);
/* 将变量加2,用于下一项的分母中*/
i = i+2;
}
}
输出:
Enter no. of interations: 10
Enter the value of angle: 60
Iteration = 1 Sum = 1.047197
Iteration = 2 Sum = 0.855800
Iteration = 3 Sum = 0.866295
Iteration = 4 Sum = 0.866021
Iteration = 5 Sum = 0.866025
Iteration = 6 Sum = 0.866025
Iteration = 7 Sum = 0.866025
Iteration = 8 Sum = 0.866025
Iteration = 9 Sum = 0.866025
Iteration = 10 Sum = 0.866025
12. 程序3 return语句
/* 计算指数级数*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
int n,j = 1;
double x,t,sum;
printf("Enter no. of iterations: ");
scanf("%d",&n);
printf("Enter the power value: ");
scanf("%lf",&x);
// 第一项等于1
t = 1;
/*到目前为止所有项之和*/
sum = t;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("\nIteration = %d\tSum = %lf",j,sum);
/*迭代计算第2项至第n项*/
for (j = 1; j < n; j++) {
/*计算下一项*/
t = t * x/j;
/*将该项累加*/
sum = sum+t;
/*显示要迭代的次数以及到当前为止所有项之和*/
printf("\nIteration = %d\tSum = %lf",j+1,sum);
}
}
输出:
Enter no. of iterations: 15
Enter the power value: 2.0
Iteration = 1 Sum = 1.000000
Iteration = 2 Sum = 3.000000
Iteration = 3 Sum = 5.000000
Iteration = 4 Sum = 6.333333
Iteration = 5 Sum = 7.000000
Iteration = 6 Sum = 7.266667
Iteration = 7 Sum = 7.355556
Iteration = 8 Sum = 7.380952
Iteration = 9 Sum = 7.387302
Iteration = 10 Sum = 7.388713
Iteration = 11 Sum = 7.388995
Iteration = 12 Sum = 7.389046
Iteration = 13 Sum = 7.389055
Iteration = 14 Sum = 7.389056
Iteration = 15 Sum = 7.389056
转载: 感谢您对莫愁个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源莫愁个人博客 https://www.mochoublog.com/study/367.html”。
下一篇:C语言如何将正整数分解质因数