Embedded Basics – The difference of ‘ ‘ and ” “

Embedded software developers commonly interface with low level sensors and have a good understanding of how to work with signed and unsigned types but when it comes to strings and character types, many developers are easily confused.  Take for example using ‘v’ and “v” in a definition or as a function parameter.  Many developers would use these interchangeably but there is a subtle and important difference between them.

charconst

The use of ‘ ‘ defines a character constant of type char.  A character constant can be used to modify a single character in a string or define an ASCII character among other uses.  For example, a developer might define an ASCII character for a comparison as seen below:

if(data == ‘v’)

{

// do something interesting

}

The use of ” ” on the other hand defines a pointer to a string.  The pointer will undoubtedly use an integer worth of memory to store the pointer, which is probably 2 or 4 bytes.  On top of the pointer memory use, the defined string will also take up enough memory to include the character(s) and the NULL (\n) string terminator.  Remember that in C a string is an array of char.

A simple example of where a developer might get confused is when passing a string to a function as parameter or vice versa.  For example, when using printf the following would result in a type error

printf(‘\n’);

printf is expecting a pointer not a constant char.

The real difference between ‘ ‘ and ” ” boils down to the fact that ‘ ‘ defines a character constant while ” ” will define a pointer to a string which is really a pointer to an array.  The difference is subtle but makes it so that the two cannot be used interchangeably.

Share >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.