element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Can't understand how the program is working
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 7 replies
  • Answers 1 answer
  • Subscribers 392 subscribers
  • Views 723 views
  • Users 0 members are here
Related

Can't understand how the program is working

Former Member
Former Member over 9 years ago

How does the Serial monitor display '12594' when we write Serial.print('12');?I have used single quotes intentionally.

[code]

void setup()

{

Serial.begin(9600);

Serial.print('12');

}


void loop()

{

}

[/code]

  • Sign in to reply
  • Cancel

Top Replies

  • gadget.iom
    gadget.iom over 9 years ago in reply to Former Member +1 suggested
    Single quotes are typically used to declare character constants (that means only on character is permitted within the quotes). The compiler should give you warning if you try to include more than one character…
  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago +1 verified
    when you output '12' your outputting 2 characters, a '1' and then a '2', this results in hex 31 or dec 49 being transmitted followed by a 32hex / 50dec, if you concatinate the two into a single integer…
  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago in reply to gadget.iom +1
    according to C99 this is implementation specefic so for the Arduino compiler it apears to be interpreting the character literal as an int which is perfectly valid go figure
  • gadget.iom
    0 gadget.iom over 9 years ago

    Why are you using single quotes?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to gadget.iom

    If we write Serial.print('6'); then its ASCII value get printed.I wanted to see what happens when we write Serial.print('12');.........You may even try Serial.print('10'); or Serial.print('11');

    The values in that case comes out t o be '12592' and '12593' respectively.So I want to know what is actually happening

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to Former Member

    Single quotes are typically used to declare character constants (that means only on character is permitted within the quotes).

    The compiler should give you warning if you try to include more than one character.

     

    sketch_dec17a.ino:4:14: warning: multi-character character constant [-Wmultichar]

     

    In practice, what you are seeing is down to either of the following:

    The value of 'ab' is likely to be either 'a' * 256 + 'b'

    or 'b' * 256 + 'a'. It will vary from one compiler to another.

    In his case (where ASCII 1=49 and ASCII 2=50 you are likely seeing:

     

    (49*256)+50 = 12594

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago

    when you output '12' your outputting 2 characters, a '1' and then a '2', this results in hex 31 or dec 49 being transmitted followed by a 32hex / 50dec, if you concatinate the two into a single integer then you get 12594, sending the 10 works out to be 12592, 11=12593

     

    the print statement is interpreting this as an integer (16bits) instead of two 8 bit characters because in C the character literal is defined as an int and your providing just the right number of 8 bit characters to make an int

     

    if you want to send the string literal "12" then you need the double quotes, this will also terminate the string correctly in the code so the actual memory used in this case would be 3 bytes   '1','2','/0'

     

    hope that helps

     

     

    http://www.asciitable.com/

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to gadget.iom

    according to C99 this is implementation specefic so for the Arduino compiler it apears to be interpreting the character literal as an int which is perfectly valid

     

    go figure

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to gadget.iom

    lol, were on it at the same time

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to Robert Peter Oakes

    Peter Oakes wrote:

     

    lol, were on it at the same time

    Appears so. image

    I was occasionally hitting refresh on the original post, I had a feeling that somebody else was working on it. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube