Why Does This Code with '1234' Compile in C++

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 letter L, as in L’x’. A character literal that does not begin with L 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 type int 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] is databeing "1234" => atoi(&data[0])returns 1234
  • &data[1] is data+1being "234" => atoi(&data[1])returns 234
  • &data[2] is data+2being "34" => atoi(&data[2])returns 34
  • &data[3] is data+3being "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



Leave a reply



Submit