Issue
I came across the following statement while I was going through a C program for client/server message handling in Unix.
int main(int argc, char* argv[])
{
(void)sscanf(argv[1],"%d",&port);
}
It would be helpful if you can explain the above statement.
Edit: I don't know why people feel my question is not clear... I just wanted to know that if scanf
takes input from keyboard then the above statement sscanf
also does the same job indirectly. Is that correct?
Solution
sscanf()
reads formatted input from a string.
scanf()
reads formatted input from stdin
.
So in your example sscanf
reads from the first argument of the program and saves it in the variable port
.
As a little example what sscanf
does:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int day;
char str[100];
strcpy(str, "21" );
sscanf(str, "%d", &day);
printf("day: %d", day);
return(0);
}
Output:
day: 21
Answered By - Rizier123