Why does this code with '1234' compile in C++?
It's a multicharacter literal, and has a type of int
.
C++11 §2.13.2 Character literals
A character literal is one or more characters enclosed in single quotes, as in
’x’
, optionally preceded by the letterL
, as inL’x’
. A character literal that does not begin withL
is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has typeint
and implementation-defined value.
Unable to convert character array elements as integer elements in C
atoi uses all the string to do the conversion, not only the first character. Then
&data[0]
isdata
being "1234" =>atoi(&data[0])
returns 1234&data[1]
isdata+1
being "234" =>atoi(&data[1])
returns 234&data[2]
isdata+2
being "34" =>atoi(&data[2])
returns 34&data[3]
isdata+3
being "4" =>atoi(&data[3])
returns 4
you want to do something like data[i] - '0'
where i is 0 .. 3
#include <stdio.h>
int main(void)
{
char data[] = {"1234"}; /* or const char * data = "1234" */
int i;
for (i = 0; i != 4; i += 1)
printf("num%d = %d\n", i+1, data[i] - '0');
return 0;
}
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
num1 = 1
num2 = 2
num3 = 3
num4 = 4
pi@raspberrypi:/tmp $
Out of that I encourage you to stop to use atoi because atoi("aze")
returns 0 without signaling an error. Instead you can use strtol or scanf with the format "%d"
checking scanf returns 1.
Taking 4 digit number in c code
There is another way just enter the entire numbers and they will be separated:
int number, result;
printf("Please, enter four numbers: ");
scanf("%d", &number);
while (number > 0) {
result = number % 10;
number /= 10;
printf("%d\n", result);
}
printf("\n");
The result:
Please, enter four numbers: 1234
4
3
2
1
Is int x = 'fooo' a compiler extension?
"Note that according to the C standard there is no limit on the length of a character constant, but the value of a character constant that contains more than one character is implementation-defined. Recent versions of GCC provide support multi-byte character constants, and instead of an error the warnings multiple-character character constant or warning: character constant too long for its type are generated in this case."
Problems in compiling code due to the modulus operator
You can only use %
with integers, and pow
produces floating-point numbers.
You could write an integer power function, or use a predefined table, but it's simpler to reverse the order of construction and start with the rightmost digit:
int main()
{
int arrx[4]; //stores the individual digits of number as array
int digx = 4; //total number of digits in number
long int dupx = 1234; //number which has to be stored in array
for(int i = 0; i < digx; i++)
{
arrx[digx-i-1] = dupx%10;
dupx = dupx/10;
}
return 0;
}
Related Topics
How to Make My Split Work Only on One Real Line and Be Capable to Skip Quoted Parts of String
How to Get a "Codesigned" Gdb on Osx
When Should I Write the Keyword 'Static' Before a Non-Member Function
Which <Type_Traits> Cannot Be Implemented Without Compiler Hooks
Should I Copy an Std::Function or How to Always Take a Reference to It
Xxxxxx.Exe Is Not a Valid Win32 Application
Friend Class with Limited Access
Conflict Between Dynamic Linking Priority in Osx
What's the Right Way to Use the Rand() Function in C++
How Would You Implement a Basic Event-Loop
Why Does a C/C++ Compiler Need Know the Size of an Array at Compile Time
Unique Hardware Id in MAC Os X
Casting Pointer to Array (Int* to Int[2])