Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:5cab3f40a061
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Oct 25 11:40:11 2017 +0000 @@ -0,0 +1,46 @@ +/* + Project: StringTests + File: main.cpp + Modified and Ported to mbed/Nucleo by: Dr. C. S. Tritt + Last revised: 10/22/17 (v. 1.0) + + Based on Program 6.2 Lengths of strings - from Beginning C, 5th ed. Ivor + Horton. + +*/ +#include "mbed.h" +#include "string.h" + +int main(void) +{ + // Check for C11 library extensions. It appears that the mbed online + // compiler does not support the library extensions. + #if defined __STDC_LIB_EXT1__ + printf("\nOptional functions are defined.\n\n"); + #else + printf("\nOptional functions are not defined.\n\n"); + #endif + + // Declare and define strings and count variable. + char str1[] = "2B or not 2B"; + char str2[] = ",that is not the \0question."; // Manual null insertion! + unsigned int count; // Used to store the string lengths. + + // Determine "size" three ways. + printf("Size of the string \"%s\" is %d characters.\n", str1, sizeof(str1)); + printf("Length of the string \"%s\" is %d characters.\n", + str1, strlen(str1)); // Danger, not a safe C function. + for (count = 0; count < sizeof(str1); count++) { // Safely count chars. + if (str1[count] == '\0') break; // Stop at null char. + } + printf("Count of the string \"%s\" is %d characters.\n\n", str1, count); + + // Repeat for other string. + printf("Size of the string \"%s\" is %d characters.\n", str2, sizeof(str2)); + printf("Length of the string \"%s\" is %d characters.\n", + str2, strlen(str2)); // Danger, not a safe C function. + for (count = 0; count < sizeof(str2); count++) { // Safely count chars. + if (str2[count] == '\0') break; // Stop at null char. + } + printf("Count of the string \"%s\" is %d characters.\n", str2, count); +} \ No newline at end of file