return0在c语言中的作用,[求助]return0的作用

返回值的作用

main 函数的返回值用于说明程序的退出状态。如果返回 0,则代表程序正常退出,否则代表程序异常退出。下面我们在 winxp 环境下做一个小实验。首先编译下面的程序:

int main( void )

{

return 0;

}

然后打开附件里的“命令提示符”,在命令行里运行刚才编译好的可执行文件,然后输入“echo %ERRORLEVEL%”,回车,就可以看到程序的返回值为 0 。假设刚才编译好的文件是 a.exe ,如果输入“a && dir”,则会列出当前目录下的文件夹和文件。但是如果改成“return -1”,或者别的非 0 值,重新编译后输入“a && dir”,则 dir 不会执行。因为 && 的含义是:如果 && 前面的程序正常退出,则继续执行 && 后面的程序,否则不执行。也就是说,利用程序的返回值,我们可以控制要不要执行下一个程序。这就是 int main 的好处。如果你有兴趣,也可以把 main 函数的返回值类型改成非 int 类型(如 float),重新编译后执行“a && dir”,看看会出现什么情况,想想为什么会出现那样的情况。顺便提一下,如果输入 a || dir 的话,则表示如果 a 异常退出,则执行 dir 。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C 语言示例程序,可以将 BMP 文件添加水印: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma pack(push, 1) typedef struct { unsigned short int bfType; unsigned int bfSize; unsigned short int bfReserved1; unsigned short int bfReserved2; unsigned int bfOffBits; } BITMAPFILEHEADER; typedef struct { unsigned int biSize; int biWidth; int biHeight; unsigned short int biPlanes; unsigned short int biBitCount; unsigned int biCompression; unsigned int biSizeImage; int biXPelsPerMeter; int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant; } BITMAPINFOHEADER; #pragma pack(pop) int main(int argc, char *argv[]) { if (argc < 4) { printf("Usage: %s input.bmp output.bmp watermark\n", argv[0]); return 1; } char *input_file = argv[1]; char *output_file = argv[2]; char *watermark = argv[3]; FILE *input = fopen(input_file, "rb"); if (input == NULL) { printf("Failed to open input file: %s\n", input_file); return 1; } BITMAPFILEHEADER file_header; BITMAPINFOHEADER info_header; fread(&file_header, sizeof(BITMAPFILEHEADER), 1, input); fread(&info_header, sizeof(BITMAPINFOHEADER), 1, input); if (file_header.bfType != 0x4D42 || info_header.biBitCount != 24) { printf("Invalid BMP file format\n"); fclose(input); return 1; } int width = info_header.biWidth; int height = info_header.biHeight; int padding = (4 - ((width * 3) % 4)) % 4; char *image_data = (char *) malloc(sizeof(char) * width * height * 3); fread(image_data, sizeof(char), width * height * 3, input); fclose(input); int watermark_length = strlen(watermark); int i, j, k; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { int index = j * width + i; int r = image_data[index * 3 + 2]; int g = image_data[index * 3 + 1]; int b = image_data[index * 3]; if (i < watermark_length) { char c = watermark[i]; int value = (int) c; for (k = 0; k < 8; k++) { int bit = (value >> (7 - k)) & 1; if (bit == 1) { if (j < height - 10) { image_data[index * 3 + 2] = 255; image_data[index * 3 + 1] = 0; image_data[index * 3] = 0; } } index += width; } } if (j == height - 1 && i == width - 1) { printf("Finished adding watermark\n"); } } } FILE *output = fopen(output_file, "wb"); if (output == NULL) { printf("Failed to create output file: %s\n", output_file); free(image_data); return 1; } fwrite(&file_header, sizeof(BITMAPFILEHEADER), 1, output); fwrite(&info_header, sizeof(BITMAPINFOHEADER), 1, output); fwrite(image_data, sizeof(char), width * height * 3, output); fclose(output); free(image_data); printf("Successfully saved watermarked image to %s\n", output_file); return 0; } ``` 这个程序需要三个参数:输入 BMP 文件名、输出 BMP 文件名和水印字符串。例如,如果要将名为 "input.bmp" 的 BMP 文件添加水印 "Hello, world!" 并保存为 "output.bmp" 文件,则可以执行以下命令: ``` ./watermark input.bmp output.bmp "Hello, world!" ``` 请注意,此程序仅适用于 24 位色深的 BMP 文件。如果 BMP 文件的色深不是 24 位,则需要相应地修改程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值