c语言常用输入输出函数

概述

C语言中常用的输入输出函数包括printf、scanf、getchar、putchar、gets、puts、fopen、fclose、fgetc、fputc、fgets、fputs等。

1. printf函数:用于向屏幕输出格式化的数据。

#include <stdio.h>
int main() 
{
   int num = 10;
   printf("The number is %d\n", num);
   return 0;
}

2. scanf函数:用于从键盘读取输入并赋值给变量。

#include <stdio.h>
int main() 
{
   int num;
   printf("Enter a number: ");
   scanf("%d", &num);
   printf("You entered: %d\n", num);
   return 0;
}

3. getchar函数:用于从键盘读取单个字符。

#include <stdio.h>
int main() 
{
   char ch;
   printf("Enter a character: ");
   ch = getchar();
   printf("You entered: %c\n", ch);
   return 0;
}

4. putchar函数:用于向屏幕输出单个字符。

#include <stdio.h>
int main() 
{
   char ch = 'A';
   putchar(ch);
   return 0;
}

5. gets函数:用于从键盘读取字符串。

#include <stdio.h>
int main() 
{
   char str[100];
   printf("Enter a string: ");
   gets(str);
   printf("You entered: %s\n", str);
   return 0;
}

6. puts函数:用于将字符串输出到屏幕。

#include <stdio.h>
int main() 
{
   char str[] = "Hello, World!";
   puts(str);
   return 0;
}

7. fopen函数:用于打开文件。

#include <stdio.h>
int main() 
{
   FILE *file = fopen("example.txt", "r");
   if (file == NULL) 
   {
      printf("File not found\n");
      return 1;
   }
   // 文件操作
   fclose(file);
   return 0;
}

8. fclose函数:用于关闭文件。

#include <stdio.h>
int main() 
{
   FILE *file = fopen("example.txt", "r");
   if (file == NULL) 
   {
      printf("File not found\n");
      return 1;
   }
   // 文件操作
   fclose(file);
   return 0;
}

9. fgetc函数:用于从文件读取单个字符。

#include <stdio.h>
int main() 
{
   FILE *file = fopen("example.txt", "r");
   if (file == NULL) 
   {
      printf("File not found\n");
      return 1;
   }
   char ch = fgetc(file);
   printf("Character read from file: %c\n", ch);
   fclose(file);
   return 0;
}

10. fputc函数:用于将单个字符写入文件。

#include <stdio.h>
int main() 
{
   FILE *file = fopen("example.txt", "w");
   if (file == NULL) 
   {
      printf("File not found\n");
      return 1;
   }
   char ch = 'A';
   fputc(ch, file);
   fclose(file);
   return 0;
}

11. fgets函数:用于从文件读取一行字符串。

#include <stdio.h>
int main() 
{
   FILE *file = fopen("example.txt", "r");
   if (file == NULL) 
   {
      printf("File not found\n");
      return 1;
   }
   char str[100];
   fgets(str, 100, file);
   printf("String read from file: %s\n", str);
   fclose(file);
   return 0;
}

12. fputs函数:用于将字符串写入文件。

#include <stdio.h>
int main() 
{
   FILE *file = fopen("example.txt", "w");
   if (file == NULL) 
   {
      printf("File not found\n");
      return 1;
   }
   char str[] = "Hello, World!";
   fputs(str, file);
   fclose(file);
   return 0;
}

这些是C语言中常用的输入输出函数和相应的示例代码。请注意,部分函数在C11标准中被认为是不安全的,并且在某些编译器中可能会引起警告或错误。如果遇到问题,请查阅相关文档或使用更安全的函数替代。

  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值