C语言学习(基本数据类型、循环语句)

本文深入讲解C语言的数据类型,包括整型、浮点型及字符型,并通过实例演示了for循环、while循环和if语句的使用。通过具体代码展示了变量声明、赋值、打印输出等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

笨方法学C部分内容

了解下C语言中的数据类型

在这里插入图片描述

#include <stdio.h>
int main(int argc, char *argv[]){
    int bugs = 100;
    double bug_rate = 1.2;
    printf("You have %d bugs at the imaginary rate of %f.\n",
    bugs, bug_rate);
    long universe_of_defects = 1L * 1024L * 1024L * 1024L;
    printf("The entire universe has %ld bugs.\n",
    universe_of_defects);
    double expected_bugs = bugs * bug_rate;
    printf("You are expected to have %f bugs.\n",
    expected_bugs);
    double part_of_universe = expected_bugs / universe_of_defects;
    printf("That is only a %e portion of the universe.\n",
    part_of_universe);
    // this makes no sense, just a demo of something weird
    char nul_byte = '\0';
    int care_percentage = bugs * nul_byte;
    printf("Which means you should care %d%%.\n",
    care_percentage);
    return 0;
}
You have 100 bugs at the imaginary rate of 1.200000.
The entire universe has 1073741824 bugs.
You are expected to have 120.000000 bugs.
That is only a 1.117587e-007 portion of the universe.
Which means you should care 0%.

C语言中的循环

for循环

for(INITIALIZER; TEST; INCREMENTER) {
CODE;
}
#include <stdio.h>
int main(int argc,char *argv[]){
	
}
#include <stdio.h>
int main(int argc, char *argv[])
{
    int i = 0;
    // go through each string in argv
    // why am I skipping argv[0]?
    for (i = 1; i < argc; i++)
    {
        printf("arg %d: %s\n", i, argv[i]);
    }
    // let's make our own array of strings 
    char *states[] = {
        "California", "Oregon",
        "Washington", "Texas"};
    int num_states = 4;
    for (i = 0; i < num_states; i++)
    {
        printf("state %d: %s\n", i, states[i]);
    }
    return 0;
}
state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas

while循环

#include <stdio.h>
int main(int argc, char *argv[])
{
    // go through each string in argv
    int i = 0;
    while (i < argc)
    {
        printf("arg %d: %s\n", i, argv[i]);
        i++;
    }
    // let's make our own array of strings 
    char *states[] = {
        "California", "Oregon",
        "Washington", "Texas"};
    int num_states = 4;
    i = 0; // watch for this
    while (i < num_states)
    {
        printf("state %d: %s\n", i, states[i]);
        i++;
    }
    return 0;
}
arg 0: c:\Users\17899\openairinterface5g\hello
state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas

if语句

if(TEST) {
CODE;
} else if(TEST) {
CODE;
} else {
CODE;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
    int i = 0;
    if (argc == 1)
    {
        printf("You only have one argument. You suck.\n");
    }
    else if (argc > 1 && argc < 4)
    {
        printf("Here's your arguments:\n");
        for (i = 0; i < argc; i++)
        {
            printf("%s ", argv[i]);
        }
        printf("\n");
    }
    else
    {
        printf("You have too many arguments. You suck.\n");
    }
    return 0;
}
You only have one argument. You suck.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值