quick questions

hello everyone, i've got a quick questions to ask. assuming
a = 1, b = 2, c = 3, d = 4
if d='4' is it true is d='4' and d=4 the same? without ''?
and if !(d = 3) is this false? i know ! means no but not quite sure. thanks
> are d='4' and d=4 the same?

No. 4 is the integer 4, '4' is the code point for the character '4'

( '4' - '0' ) == 4 or ( '0' + 4 ) == '4'
so false?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
   int integer_4 = 4 ;
   int character_literal_4 = '4' ;

   std::cout << "integer_4 == " << integer_4 << "\n\n"
             << "character_literal_4 == " << character_literal_4 << "\n\n"
             << "integer_4 + '0' == " << integer_4 + '0' << "\n\n"
             << "character_literal_4 - '0' == " << character_literal_4 - '0' << "\n\n" ;
}

integer_4 == 4

character_literal_4 == 52

integer_4 + '0' == 52

character_literal_4 - '0' == 4

http://coliru.stacked-crooked.com/a/6f117fe50be74c74
closed account (1CfG1hU5)
 
if !(d=3)


if not d assigned to 3

believe the ! has to be inside the parentheses, not out
compiler error if not.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main()
 {
   int d = 4;

   if (d != 3)
     printf("the value of d is not 3, value of d is %d", d);

return 0;

  }


closed account (1CfG1hU5)
can take some time to look at ascii charts
the char '4' is integer value 52.

added a char equal to '0' and an integer equal to number 4.

outcome was number 4 which is integer/decimal value 52.
tested to see result

probably better to add integers to integers and chars to chars.
if you add 48 + 52, you get 100 decimal. same as char 'd'.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main()
 {
   int a = 48;  // char '0' in decimal form
   int b = 52;  // char '4' in decimal form

   printf("a is 48, b is 52, the addition of both integers is char %c, integer %d\n\n", a+b, a+b);

return 0;

  }
Last edited on
closed account (1CfG1hU5)
char value adding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
 {
   char a = 15; 
   char b = 9;

   printf("char a value is 15, char b value is 9\n\n");
   printf("char a decimal value is %d, char b decimal value is %d\n\n", a, b);
   printf("the addition of both chars is char %c, integer %d\n\n", a+b, a+b);

return 0;

  }



some probems displaying a char with %c.

doing integer to char conversions may be better.

also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char '0' is 48 decimal
char '1' is 49 decimal
char '2' is 50 decimal
char '3' is 51 decimal
char '4' is 52 decimal
char '5' is 53 decimal
char '6' is 54 decimal
char '7' is 55 decimal
char '8' is 56 decimal
char '9' is 57 decimal

NULL char is 0 decimal
Start of heading (SOH) is 1 decimal
Start of text (STX) is 2 decimal
End of text (ETX) is 3 decimal
End of transmissions (EOT) is 4 decimal
Enquiry (ENQ) is 5 decimal
Acknowledge (ACK) is 6 decimal
Bell (BEL) is 7 decimal
Backspace (BS) is 8 decimal
Horizontal tab (TAB) is 9 decimal

0 to 9's to watch out for 

Last edited on
closed account (1CfG1hU5)
https://www.google.com/search?q=ascii+charts&oq=ascii+charts&aqs=chrome..69i57j69i65l3j69i59j5.2192j0j8&sourceid=chrome&es_sm=93&ie=UTF-8
so looking at !(d = 3) this is true?
because since d=4 and (4=3) is false and ! makes it true?
closed account (1CfG1hU5)
the !(d = 3) won't work. if (d != 3) better. d not equal to 3 is true.
Last edited on
Topic archived. No new replies allowed.