// C program to demonstrate snprintf()
#include <stdio.h>
int main()
{
char buffer[50];
char* s = "geeksforgeeks";
// Counting the character and storing
// in buffer using snprintf
int j = snprintf(buffer, 14, "%s\n", s);
// Print the string stored in buffer and
// character count
printf("string:\n%s\ncharacter count = %d\n", buffer, j);
return 0;
}
Output is:
string
geeksforgeeks
Character count = 14
I was told by a friend that a C-program requires a setup() and a loop() before it can run as a Sketch in arduino. I added those to the program and moved things around, added the Serial.begin & connected it to a working board & ran it :
#include <stdio.h>
char buffer[50];
char* s = "geeksforgeeks";
void setup() {
Serial.begin(115200);
}
int main()
{
// Counting the character and storing
// in buffer using snprintf
int j = snprintf(buffer, 14, "%s\n", s);
// Print the string stored in buffer and
// character count
printf("string:\n%s\ncharacter count = %d\n", buffer, j);
return 0;
}
void loop() {
// put your main code here, to run repeatedly:
}
It compiled and uploaded successfully, but I got no Serial monitor response. Any ideas why?