小编Bha*_*mar的帖子

程序来查找字符串的长度

如果我输入大于10的字符串,那么为什么不生成编译时错误,因为我声明str了大小为10?例如,我有输入welcome to the world,然后它正在编译和运行没有错误.

#include <stdio.h>
#include <conio.h>

int main() {
    int i = 0, length;
    char str[10];
    printf("enter string: ");
    gets(str);

    while (str[i] !='\0') {
        i = i + 1;
    }
    length = i;
    printf("the length of string is %d", length);
}
Run Code Online (Sandbox Code Playgroud)

c c-strings

0
推荐指数
1
解决办法
50
查看次数

用于查找数组中最大和第二大数字的程序

我已经在很多网站上搜索了这个问题.他们通过一些不同的方法来做到这一点.如果我将数组的第一个元素作为最大值输入,则此代码不提供输出a[0].我认为需要做一些小改动.有人可以告诉我吗?

#include <stdio.h>

int main() {
    int a[10], n;
    int largest1, largest2, i;

    printf("enter number of elements you want in array");
    scanf("%d", &n);
    printf("enter elements");
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    largest1 = a[0];
    for (i = 0; i < n; i++) {
        if (a[i] > largest1) {
            largest1 = a[i];
        }
    }
    largest2 = a[0];
    for (i = 1; i < n; i++) {
        if (a[i] > largest2 && a[i] < …
Run Code Online (Sandbox Code Playgroud)

c arrays data-structures

0
推荐指数
1
解决办法
9万
查看次数

标签 统计

c ×2

arrays ×1

c-strings ×1

data-structures ×1