473,473 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

char math?

mdh
Can someone enlighten me?

let "s" be a string array and "i, n" be an integers.

Why does the expression:

s[i]= n % 10 + '0' put the "string value" of the modulo in the array.

I thought by placing the 0 in a single quote, you are telling the
compiler to treat 0 as a character.
I fail to see how "n % 10" added to that produces the correct result,
which it does!
Thanks in advance.

Ref to this is page 64 of K&R(2);

Jun 5 '06 #1
2 4954
mdh said:
Why does the expression:

s[i]= n % 10 + '0' put the "string value" of the modulo in the array.


Because of 5.2.1(3) which says, in part, "...the value of each character
after 0 in the above list of decimal digits shall be one greater than the
previous".

(It means '0', by the way.)

So - 0 + '0' is '0', obviously. But the above text means that 1 + '0' is
guaranteed to be '1', 2 + '0' is guaranteed to be '2', etc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 5 '06 #2
mdh

Richard Heathfield wrote:
mdh said:
Because of 5.2.1(3) which says, in part, "...the value of each character
after 0 in the above list of decimal digits shall be one greater than the
previous".

(It means '0', by the way.)

So - 0 + '0' is '0', obviously. But the above text means that 1 + '0' is
guaranteed to be '1', 2 + '0' is guaranteed to be '2', etc.


OK...thanks...
I was trying to make a much bigger deal out of it.

Jun 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Nitin Manoharan | last post by:
Hello, How can I convert an int to a *char (best way please) I know a few round about methods myself :) Thank you
0
by: Will Oram | last post by:
Xcode, OS X's toolkit, compiles this, but chokes if anything (here, a '( ' char) is inputted. Its goal is to take an input string character by character and put it in a linked list. The output...
10
by: Ramprasad A Padmanabhan | last post by:
Hello all, On my linux box ( redhat 7.2 ), I have been using char as a boolean data type. In order to save on the number of bytes as compared to using int or short int. typedef char boolean;...
7
by: Frank | last post by:
Sorry that the question I posted few minutes ago didn't correctly describe the problem. So please ignore it. I repost the question as below: ============================================== I...
34
by: yaniv.dg | last post by:
hi all, i building an equition with float type now,what function should i use in order to identify is someone enter a char value by mistake?
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
15
by: Alok Kumar | last post by:
#include <string.h> void myfn() { char a = 'A'; char b; strcpy(b, &a); } Would I always get 'A' in b and '\0' in b after the strcpy?
12
by: javaBookWorm | last post by:
import java.applet.Applet; import java.awt.*; // Contains all of the classes for creating user interfaces and for painting graphics and images. import java.awt.event.*;//The root event class for...
9
by: cirfu | last post by:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz": cant i write something like: if char in "": ?
2
by: zxry | last post by:
I'm new to C++ and need to write a program that reads and calculates math equations from a char string. My question: is there a way to convert a char math symbol such as '+' into a + the compiler...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.