Home > Article > Backend Development > c language print diamond
c language prints diamond
C language is a process-oriented computer programming language. When we first learn, we They all run C programs through the command line. Let’s take a look at how to write a C program to output a diamond shape on the command line.
Recommended course: C language tutorial
The source code is:
#include<stdio.h> void main() { int n = 6; int i, a, b; //前4行.上半部分 for(i = 1; i <= n; i++)//控制行数 { for( a = n - 1; a >= i; a--)//打印空格 { printf(" "); } for( b = 1;b <= 2*i-1; b++)//打印* { printf("*"); } printf("\n"); } //后3行,下半部分 for( i = n-1;i >= 1;i--) { for( a = i;a <= n-1; a++) { printf(" "); } for( b = 1;b <= 2*i-1; b++) { printf("*"); } printf("\n"); } }
What is worth noting in this program
This program The source code mainly uses loop nesting of for statements. The general form of a for statement is:
for(循环变量赋初值;循环条件;循环变量增值) {语句}
When the first or second condition is just a semicolon, it means: This condition is always true and always established!
c language format
Program files include: source program file (suffix .c), target file (suffix .obj), executable file (suffix .exe), when the suffix If the name is wrong, the program cannot be executed. Usually the code we write is the source program file, so when saving it, use .c. The program will automatically generate the target file .obj, and then the execution file .exe. Then execute it to get the result.
The above is the detailed content of c language print diamond. For more information, please follow other related articles on the PHP Chinese website!