zoukankan      html  css  js  c++  java
  • C语言中snprintf sprintf 使用和sizeof和strlen的区别和puts的使用

      char fn[256];
        int rc;
    
        pcm = calloc(1, sizeof(struct pcm));
        if (!pcm || !config)
            return &bad_pcm; /* TODO: could support default config here */
    
        pcm->config = *config;
    
        snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
                 flags & PCM_IN ? 'c' : 'p');
    
        pcm->flags = flags;
        pcm->fd = open(fn, O_RDWR);
        if (pcm->fd < 0) {
            oops(pcm, errno, "cannot open device '%s'", fn);
            return pcm;
        }
    

      

           

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <unistd.h>
    #include <signal.h>
    #include <string.h>
    
    
    // static
    static int seq[] = {1,2,3,4,5,6};
    static const char* string[] = { "a","b","c","d"};
    
    static char* pstring=  "http://www.ABCD.com";
    
    /**
         char fn[256];
         snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
                 flags & PCM_IN ? 'c' : 'p');
    **/
    
    #define  PCM_IN   (0)
    #define  PCM_OUT  (1)
    
    int main()
    {
        int i = 0;
        unsigned int card ;
        unsigned int device;
        unsigned int flags;
    
        char fn[256] = {0};
        sprintf(fn,"/dev/snd/pcmC%uD%u%c",card,device,flags & PCM_IN ? 'c' : 'p');
        //printf("%s
    ",fn);
        puts(fn);
        snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
                 flags & PCM_IN ? 'c' : 'p');
        puts(fn);
        // strlen和sizeof的差距
        printf("%d %d
    ",strlen(fn),sizeof(fn)/sizeof(char));
        while(i++<strlen(fn))
        {
            puts(fn);
        }
    
        while(1);
    
        return 0;
    }
    
    

      

    一勤天下无难事。
  • 相关阅读:
    P1087 FBI树 二叉树
    回顾测试和测试方法
    Postman
    执行用例的思路、批量执行(可选)、定时任务
    unittest平台分析与建表:
    HTMLTestRunner源码
    665. Non-decreasing Array
    661. Image Smoother
    643. Maximum Average Subarray I
    628. Maximum Product of Three Numbers
  • 原文地址:https://www.cnblogs.com/nowroot/p/13659840.html
Copyright © 2011-2022 走看看