微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

scanf输入格式控制字符串

#include <stdio.h>

#define SIZE  20                              // Max characters in a word
void try_input(char *prompt, char *format);   // Input test function

int main(void){
  try_input(Enter as input: -2.35 15 25 ready2go\n,
              %f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s%n );

  try_input(\nEnter the same input again: ,
              %4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n);

  try_input(\nEnter as input: -2.3A 15 25 ready2go\n,
              %4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n);
  return 0;/*from www.j  a v  a2  s . c  om*/
}

void try_input(char* prompt, char *format){
  int value_count = 0;                  // Count of input values read
  float fp1 = 0.0f;                     // Floating-point value read
  int i = 0;                            // First integer read
  int j = 0;                            // Second integer read
  char word1[SIZE] =  ;               // First string read
  char word2[SIZE] =  ;               // Second string read
  int byte_count = 0;                   // Count of input bytes read
  printf(prompt);
  value_count = scanf(format, &fp1, &i , &j,
                    word1, sizeof(word1), word2, sizeof(word2), &byte_count);
  fflush(stdin);                        // Clear the input buffer
  printf(The input format string for scanf() is:\n     \%s\\n, format);
  printf(Count of bytes read = %d\n, byte_count);
  printf(Count of values read = %d\n, value_count);
  printf(fp1 = %f   i = %d   j = %d\n, fp1, i, j);
  printf(word1 = %s   word2 = %s\n, word1, word2);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐