C程序
C 程序的输出显示运算符、循环、函数、数组的使用,对字符串、文件、指针执行操作。 下载可执行文件并在不编译源文件的情况下执行它们。 Code::Blocks IDE 用于编写程序; 其中大部分将与 GCC 和 Dev C++ 编译器一起使用。 第一个程序打印“Hello World”。
带输出的 C 编程示例
示例 1 - C hello world 程序/** 我的第一个 C 程序 */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
程序输出:“Hello World”
示例 2 - 使用 scanf 获取用户输入的 C 程序
#include <stdio.h>
int main()
{
int x;
printf("Input an integer\n");
scanf("%d", &x); // %d is used for an integer
printf("The integer is: %d\n", x);
return 0;
}
输出:输入一个整数7897整数为:7897
示例 3 - 使用 if else 控制指令
#include <stdio.h>
int main()
{
int n;
printf("Enter a number\n");
scanf("%d", &n);
if (n > 0)
printf("Greater than zero.\n");
else
printf("Less than or equal to zero.\n");
return 0;
}
输出:输入一个数字 - 45 小于或等于零。
示例 4 - while 循环示例
#include <stdio.h>
int main()
{
int c = 1; // Initializing variable
while (c <= 10) // While loop will execute till the condition is true
{
printf("%d ", c); // Note the space after %d for gap in the numbers we want in output
c++;
}
return 0;
}
输出:1 2 3 4 5 6 7 8 9 10
示例 5 - C 程序检查一个整数是否为素数
#include <stdio.h>
int main()
{
int n, c;
printf("Enter a number\n");
scanf("%d", &n);
if (n == 2)
printf("Prime number.\n");
else
{
for (c = 2; c <= n - 1; c++)
{
if (n % c == 0)
break;
}
if (c != n)
printf("Not prime.\n");
else
printf("Prime number.\n");
}
return 0;
}
示例 6 - 命令行参数
#include <stdio.h>
int main(int argc, char *argv[])
{
int c;
printf("Number of command line arguments passed: %d\n", argc);
for (c = 0; c < argc; c++)
printf("%d argument is %s\n", c + 1, argv[c]);
return 0;
}
该程序打印参数的数量及其内容。
示例 7 - 数组程序
#include <stdio.h>
int main()
{
int array[100], n, c;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("The array elements are:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
示例 8 - 函数程序
#include <stdio.h>
void my_function(); // Declaring a function
int main()
{
printf("Main function.\n");
my_function(); // Calling the function
printf("Back in function main.\n");
return 0;
}
// Defining the function
void my_function()
{
printf("Welcome to my function. Feel at home.\n");
}
示例 9 - 在程序中使用注释
#include <stdio.h>
int main()
{
// Single line comment in a C program
printf("Writing comments is very useful.\n");
/*
* Multi-line comment syntax
* Comments help us to understand a program later easily.
* Will you write comments while writing programs?
*/
printf("Good luck C programmer.\n");
return 0;
}
示例 10 - 在 C 编程中使用结构
#include <stdio.h>
#include <string.h>
struct game
{
char game_name[50];
int number_of_players;
}; // Note the semicolon
int main()
{
struct game g;
strcpy(g.game_name, "Cricket");
g.number_of_players = 11;
printf("Name of game: %s\n", g.game_name);
printf("Number of players: %d\n", g.number_of_players);
return 0;
}
示例 11 - 斐波那契数列的 C 程序
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n", n);
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}
return 0;
}
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,"C:\\TC\\BGI");
outtextxy(10, 20, "Graphics programming is fun!");
circle(200, 200, 50);
setcolor(BLUE);
line(350, 250, 450, 50);
getch();
closegraph( );
return 0;
}
如何使用 GCC 编译器编译 C 程序?
如果您在 Linux 操作系统上使用 GCC,那么您可能需要修改程序。 例如,考虑以下打印前十个自然数的程序。
#include <stdio.h>
#include <conio.h>
int main()
{
int c;
for (c = 1; c <= 10; c++)
printf("%d\n", c);
getch();
return 0;
}
该程序包含一个头文件 <conio.h> 并调用 getch 函数,但该文件是 Borland 特定的,因此它可以在 Turbo C 编译器中运行,但不能在 GCC 中运行。 GCC 的程序必须是这样的:
#include <stdio.h>
int main()
{
int c;
/* for loop */
for (c = 1; c <= 10; c++)
printf("%d\n", c);
return 0;
}
如果您使用 GCC,请将程序保存在一个文件中,例如“numbers.c”以编译程序,打开终端并输入命令“gcc numbers.c”,编译程序并执行它输入命令“。 /a.out" 在执行命令时不要使用引号。 您可以将输出文件名指定为“gcc numbers.c -o numbers.out”,以在终端中运行“./numbers.out”。
C编程教程
程序由包含给予机器执行任务的指令的函数组成。 编写它的过程包括设计算法,绘制流程图,然后编写代码。 写完之后,如果没有产生所需的输出,则需要对其进行测试和调试。
要编写程序,您需要一个文本编辑器(使用您最喜欢的编辑器)和一个编译器。 编译器将源代码转换为机器代码,机器代码只包含 0 和 1,准备在机器上执行。
IDE(集成开发环境)提供文本编辑器、编译器、调试器等用于开发程序和管理项目。 Code::Blocks IDE 提供了理想的开发环境。 它可以导入 Microsoft Visual C++ 项目,可扩展,因为它使用插件、开源和跨平台。
如何编写C程序?
一个程序必须至少有一个 main 函数。 函数由声明和语句组成。 语句是后跟分号的表达式。 例如a + b, printf("C program examples")是表达式,a + b; 和 printf("C 是一种易于学习的计算机编程语言"); 是陈述。
要使用变量,我们必须指明它的类型,无论是整数、浮点数、字符还是其他类型。 C 语言有许多内置的数据类型,我们可以使用结构和联合来创建我们的数据类型。 每种数据类型都有其大小,这可能取决于机器; 例如,一个整数可能是 2 或 4 个字节。 数据以二进制形式存储,即一组位,其中每个位可以是“0”或“1”。
诸如“switch”、“case”、“default”、“register”之类的关键字是具有预定义含义的保留字,不能用作变量或函数的名称。 可以使用 malloc 和 calloc 函数在编译时或运行时分配内存。 C语言具有递归、预处理、条件编译、可移植性、指针、使用外部库的多线程、动态内存分配等诸多特性。 由于这些,它被用于制作便携式软件程序和应用程序。 使用网络 API 的用户可以相互通信和交互并共享文件。
C 标准库包含用于数学运算、字符、输入/输出、文件等的函数。 编写称为编码的程序的过程需要编程语言和逻辑知识才能实现所需的输出。 因此,您应该学习 C 编程基础并开始编写程序。
使用 C 学习数据结构(堆栈、队列、链表、二叉树、图)可以让您在详细研究所有内容时有更深入的理解。 一般的信念是使用高级语言。 但是,在学习 C++ 或 Java 之前先学习 C 是个好主意。 C++是面向对象的,包含了C的所有特性,所以学习C可以帮助你快速学习C++,然后你就可以学习Java了。
C编程PDF
- Dev C++ compiler
- 开发 C++ 编译器
- Essential C
- 基本 C
C编程书籍
- Let Us C By Yashavant Kanetkar
- Yashavant Kanetkar 让我们 C
- PROGRAMMING WITH C By Byron Gottfried, Jitender Chhabra
- 用 C 编程 Byron Gottfried, Jitender Chhabra
- The C Programming By Brian Kernighan and Dennis Ritchie
- 布赖恩·克尼汉 (Brian Kernighan) 和丹尼斯·里奇 (Dennis Ritchie) 的 C 编程
如果您是初学者,请购买前两本书中的任何一本,如果您有编程经验或了解 C 语言基础知识,请购买第三本。
评论区