欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-12 15:26:27
...

笨方法学C部分内容

了解下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.
相关标签: c基础 c语言