sscanf function--powerful C library function

Source: Internet
Author: User

This function is really powerful, and this essay will always update some summaries until most of the applications are covered:

EG1:

A string---"+cbc:0,90,4090" from the cloud in the company project today

I want to get the number 0,90,4090. Of course, we can write a function, judge, and each value, and then mix into the desired data, but this is really too troublesome, so sscanf came.

#include <stdio.h>intMainvoid){    intA; intb; intC; Char*s="+cbc:0,90,4090"; SSCANF (s),"+CBC:%d,%d,%d",&a,&b,&c); printf ("%d%d%d\n", A,b,c); return 0;}

is not very simple to complete the string in the extraction of numbers, and you can format the input.

It should be noted that the corresponding clear, the above string +CBC: (space) So in the sscanf also must correspond, the space others have, you have to have, there is the time of the format, if in the form of character input, can only read a byte (of course, char is a byte of the machine).

#include <stdio.h>intMainvoid){    intA; intb; CharC; Char*s="+cbc:0,90,4090"; SSCANF (s),"+CBC:%d,%d,%c ",&a,&b,&c); printf ("%d%d%c\n", A,b,c); return 0;}

Above the red part, just change to the form%c, the output will only have one byte:

SSCANF far more than this role, more to be continued ...

sscanf function--powerful C library function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.