21xrx.com
2024-04-20 12:07:36 Saturday
登录
文章检索 我的文章 写文章
C 语言的 Hello world 程序
2021-07-06 20:30:28 深夜i     --     --
C e l l o o r l d

如何用C语言编写一个hello world程序? 要学习编程语言,您必须开始用它编写程序,这可能是您的第一个 C 程序。 我们先来看看程序。
 

#include <stdio.h>


int main()
{
  printf("Hello world\n");
  return 0;
}

库函数 printf 用于在屏幕上显示文本,'\n' 将光标置于下一行的开头,“stdio.h”头文件包含该函数的声明。

 

该程序的目的是熟悉 C 编程语言的语法。 在其中,我们打印了一组特定的单词。 要打印您想要的任何内容,请参阅 C 程序以打印字符串。

输出:Hello world

 

C hello world 使用字符变量

#include <stdio.h>


int main()
{
  char a = 'H', b = 'e', c = 'l', d = 'o';
  char e = 'w', f = 'r', g = 'd';

  printf("%c%c%c%c%c %c%c%c%c%c", a, b, c, c, d, e, d, f, c, g);

  return 0;
}

 

我们使用了七个字符的变量,'%c' 用于显示一个字符变量。 请参阅下面的其他有效方法。

我们可以将“hello world”存储在字符串(字符数组)中。
 

#include <stdio.h>


int main()
{
  char s1[] = "HELLO WORLD";
  char s2[] = {'H','e','l','l','o',' ','w','o','r','l','d','\0'};

  printf("%s %s", s1, s2);

  return 0;
}

输出:HELLO WORLD Hello world

如果您不了解该程序,请不要担心,因为您可能还不熟悉这些字符串。

C Hello world 多次

使用循环我们可以多次显示它。
 

#include <stdio.h>


int main()
{
  int c, n;

  puts("How many times?");
  scanf("%d", &n);

  for (c = 1; c <= n; c++)
    puts("Hello world!");

  return 0;
}

C语言中的Hello world无限期

#include <stdio.h>


int main()
{
  while (1)  // This is always true, so the loop executes forever
    puts("Hello World");

  return 0;
}

要终止程序,请按 (Ctrl + C)。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复