Q
qazmlp
I have written the following code to extract the last 3 digits from a string.
Is there any improvement needed for this code?
-------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
const char* nameWithID = "SOME_NAME_261" ;
char bufToHold3Digits[4] = {0} ;
strncpy( bufToHold3Digits , nameWithID + strlen( nameWithID ) - 3 , 3 ) ;
printf( "%d", atoi(bufToHold3Digits) ) ;
}
-------------------
Is there any improvement needed for this code?
-------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
const char* nameWithID = "SOME_NAME_261" ;
char bufToHold3Digits[4] = {0} ;
strncpy( bufToHold3Digits , nameWithID + strlen( nameWithID ) - 3 , 3 ) ;
printf( "%d", atoi(bufToHold3Digits) ) ;
}
-------------------