您现在的位置是:首页 > 学无止境 > C语言网站首页C语言 如何学习C语言、入门C语言,看这篇就够了

如何学习C语言、入门C语言,看这篇就够了

  • 莫愁
  • C语言
  • 2019-11-10
简介该文章将会把C语言基础语法梳理一遍,主要记录帮助理解相关知识点的代码示例和重要或容易忽略的面试知识,将会陆续发布系类文章.
字数 24775

函数

1.重要的面试题

scanf()和printf()函数的返回值是什么?
答:scanf()函数返回传递给函数的变量个数.例如,

scanf("%d%d",&x,&y);    // 返回2.

printf()函数返回显示为输出的字节数.例如,

printf("Hello"); // 返回5

2.程序 函数的形式(此处仅列举一种)

/* sum函数不需要传值也没有返回值*/
#include <stdio.h>
void sum(){
    int x = 10,y = 25;
    int z = x+y;
    printf("Result = %d.\n",z);
}
int main(){
    sum();
}

输出:

Result = 35.

3.程序 函数的定义

/* 用square()函数计算整数的平方*/
#include <stdio.h>
int square(int num){
    int result;
    result = num*num;
    return result;
}
int main(){
    int x,res;
    printf("Enter an integer: ");
    scanf("%d",&x);
    // 调用函数并传递参数x,同时保存返回值
    res = square(x);
    printf("Square value = %d\n",res);
}

输出:

Enter an integer: 16
Square value = 256

4.程序

/* 两个整数的最大公约数*/
#include <stdio.h>
// 函数原型声明
int gcd(int u,int v);
int main(){
    int x,y;
    printf("Type two integer values: ");
    scanf("%d,%d",&x,&y);
    printf("GCD value = %d\n",gcd(x, y));
}

int gcd(int u,int v){
    int r;
    while (v!=0) {
        r = u%v;
        u = v;
        v = r;
    }
    return u;
}

输出:

Type two integer values: 150,35
GCD value = 5

5.程序

/* 判断是否为d素数*/
#include <stdio.h>
int isprime(int n){
    int i,flage = 1;
    for (i=2; i<n; i++) {
        if (n%i == 0) {
            flage = 0;
            break;
        }
    }
    return flage;
}

int main(){
    int num,result;
    printf("Enter number: ");
    scanf("%d",&num);

    result = isprime(num);
    if (result == 1)
        printf("%d is prime\n",num);
    else printf("%d is not prime\n",num);
}

输出:

Enter number: 11
11 is prime

6.程序 向函数传递数组

#include <stdio.h>
// 输入数组存储到一维数组中
void getarr(int arr[],int n){
    int i;
    printf("\n");
    for (i=0; i<n; i++) {
        printf("Enter element: ");
        scanf("%d",&arr[i]);
    }
}

// 显示数组中的元素
void disparr(int arr[],int n){
    int i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
}

int main(){
    int  x[50];
    int n;
    printf("How many elements? ");
    scanf("%d",&n);
    /*读取数组, 传递h数组名和数据元素个数给函数*/
    getarr(x,n);
    // 显示数组
    disparr(x,n);
}

输出:

How many elements? 5

Enter element: 44
Enter element: 55
Enter element: 66
Enter element: 77
Enter element: 88
44    55    66    77    88

7.程序 冒泡排序算法

/* 冒泡排序*/
#include <stdio.h>
#define MAX 50
void sorting(float arr[],int n){
    float t;
    int i,pass;
    for (pass = 0; pass<=n-2; pass++) {
        for (i = 0; i<=n-2; i++) {
            if (arr[i]>arr[i+1]) {
                //交换元素
                t = arr[i];
                arr[i]>arr[i+1];
                arr[i+1]=t;
            }
        }
    }
}
int main(){
    float x[MAX];
    int i,n;
    printf("How many elements? ");
    scanf("%d",&n);
    // 向数组中存储h数字
    for (i=0; i<n; i++) {
        printf("Enter number: ");
        scanf("%f",&x[i]);
    }
    // 调用排序函数,传递数组参数
    sorting(x, n);
    printf("\nSorted elements:\n");
    for (i=0; i<n; i++) {
        printf("%.2f\t",x[i]);
    }
}

输出:

How many elements? 5
Enter number: 11
Enter number: 23
Enter number: 45
Enter number: 66
Enter number: 37

Sorted elements:
11.00    23.00    45.00    66.00    66.00

8.程序 编写一个函数读取并显示它

#include <stdio.h>
#define ROWS 50
#define COLS 50
void getarr(int arr[ROWS][COLS],int r,int c){
    int i,j;
    for (i=0; i<r; i++) {
        for (j=0; j<c; j++) {
            scanf("%d",&arr[i][j]);
        }
    }
}

void disparr(int arr[ROWS][COLS],int r,int c){
    int i,j;
    for (i=0; i<r; i++) {
        for (j=0; j<c; j++) {
            printf("%d\t",arr[i][j]);
        }
        printf("\n");
    }
}

int main(){
    int x[ROWS][COLS];
    int r,c;
    printf("How many row,cols? ");
    scanf("%d,%d",&r,&c);

    // 使用getarr()函数读取二维数组并制定数组名,列数以及行数
    printf("\nEnter matrix:\n");
    getarr(x, r, c);
    printf("\nYour matrix:\n");
    // 使用disparr()函数显示二维数组传递数组名,行数,列数
    disparr(x, r, c);
}

输出:

How many row,cols? 2, 3

Enter matrix:
1 2 3
4 5 6

Your matrix:
1    2    3    
4    5    6    

9.递归函数

/* 一个计算阶乘的递归函数*/
#include <stdio.h>
long int factorial(int n){
    long int result;
    if (n == 0)
        result = 1;
    else
        result = n*factorial(n-1);
    return result;
}

int main(){
    int j;
    for (j = 0; j<10; j++) {
        printf("Factorial(%d) = %ld\n",j,factorial(j));
    }
}

输出:

Factorial(0) = 1
Factorial(1) = 1
Factorial(2) = 2
Factorial(3) = 6
Factorial(4) = 24
Factorial(5) = 120
Factorial(6) = 720
Factorial(7) = 5040
Factorial(8) = 40320
Factorial(9) = 362880

10.程序 理解函数变量的作用域

/* 演示函数变量的作用域*/
#include <stdio.h>
void funct(){
    /* 块作用域,临时变量*/
    int x = 1;
    /* 块作用域,永久变量*/
    static int y = 1;
    printf("x = %d\ty = %d",x,y);
    x++;
    y++;

}

int main(){
    // 调用四次函数
    funct();
    funct();
    funct();
    funct();
}

输出:

x = 1    y = 1
x = 1    y = 2
x = 1    y = 3
x = 1    y = 4

11.重要的面试题

什么是库;形参和实参有什么区别?
答:对于程序而言,库就是一个语言或者软件对非常有用的内置资源的封装,例如,C标准库包含了许多内置函数.
形参是函数定义中变量,实参表示函数在调用时传递的参数值.

12.重要的面试题

函数声明和函数定义有什么区别?
答:函数的定义指的是函数的具体实现,内存分配,而函数声明或原型表示的是函数的引用,它告诉编辑器函数在程序某处已经被定义,在链接对的时候会被找到,通常,函数声明写在main()函数之前,而函数的定义则在main()之后.

13.重要的面试题

什么是静态函数?
答:静态函数就是其作用域被限定在当前程序文件中的一个函数.静态函数可以被同一个程序文件的代码看见和使用,而不能被文件外的函数使用,静态函数对于解决函数名冲突非常有用.

14.重要的面试题

除.h之外,其他类型文件可以被#include语句包含吗?
答:可以将任意的文件当作头文件来包含.例如,

#include <myfile.txt>

myfile.txt文件可能包含其他头文件或者函数原型.

15.重要的面试题

C里面的函数可以重载吗?
答:编写有相同的名字但是参数列表各不相同的函数叫做函数重载.C语言部分支持函数重载,例如,printf()函数:

printf("%d",x); // 两个参数
printf("%d,%d",x,y); // 三个参数

在C内置函数里面可以这样做函数重载,但是其他地方不支持函数重载,用户自定义函数不可以重载.

 第5页/共11  首页 上一页 3 4 5 下一页 尾页


转载: 感谢您对莫愁个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源莫愁个人博客 https://www.mochoublog.com/study/367.html”。

文章评论

    • 评论
    人参与,条评论

技术在线

服务时间

周一至周日 12:00-22:00

关闭下雪
关闭背景特效