2

My code:

#include <stdio.h>

int main() {
   const int  LENGTH = 10;
   const int  WIDTH = 5;
   const char NEWLINE ='\n';
   int area;

   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);

   return 0;
}

In the above code the output:

value of area: 50

Process returned 0 (0x0)   execution time : 2.909 s
Press any key to continue.

There is a new line inserted, but when I change NEWLINE="\n" despite knowing it is a char type, there is no error prompted by the compiler and no newline printed out. Why???

Also, I modified my code as,

#include <stdio.h>

int main() {
   const int  LENGTH = 10;
   const int  WIDTH = 5;
   const char NEWLINE ='\n';
   const char k="hjk";
   int area;

   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
   printf("%c", k);
   return 0;
}

The output is only the area calculated and the new line but k is not printed out. I also find this very weird! Can you please give suggestions?

Please be kind enough with the suggestions and point out my mistakes because I am a beginner at C.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

4 Answers4

2

The problem is that you are trying to save a string as a char, so you have to change const char k = "hjk" to const char k[]="hjk" and print it using %s instead of %c.

#include<stdio.h>
int main() {
  const int  LENGTH = 10;
  const int  WIDTH = 5;
  const char NEWLINE ='\n';
  const char k[]="hjk";
  int area;

  area = LENGTH * WIDTH;
  printf("value of area : %d", area);
  printf("%c", NEWLINE);
  printf("%s", k);
  return 0;
}

Some clarification: if you save a "string" without specifying that it is an array of characters char[], if you try to print it as a char %c a warning would be generater (warning: incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char [4]') and if you try to print it as a array of characters %s (string) you are going to receive a segmentation fault.

lorenzozane
  • 1,214
  • 1
  • 5
  • 15
  • The code isn't valid C and has no deterministic behavior. Some weird compiler spat out some weird binary regardless, but what that one will do is anyone's guess. [What must a C compiler do when it finds an error?](https://software.codidact.com/q/277340) – Lundin Nov 13 '20 at 11:26
  • @Lundin could you explain a little better? it is not clear to me what you are referring to specifically (or if I wanted to correct my answer would be even better) – lorenzozane Nov 13 '20 at 11:36
  • I posted an answer now. https://stackoverflow.com/a/64820204/584518 – Lundin Nov 13 '20 at 11:37
2

when I change NEWLINE="\n" despite knowing it is a char type, there is no error prompted by the compiler

const char NEWLINE = "\n"; is invalid C. The reason why it is invalid is explained in detail here: "Pointer from integer/integer from pointer without a cast" issues

The compiler is not required to produce an "error", but it is required to produce some sort of diagnostic message. See What must a C compiler do when it finds an error?

Why your compiler decided to spew out a binary regardless of getting fed invalid C is anyone's guess. You have to ask the people who made the compiler. In case of gcc, you won't find an answer, because this is completely undocumented behavior.

And therefore, any output you get from such a "non C" program is also completely non-deterministic, unless a compiler documented the behavior among non-standard compiler extensions. gcc did not.

Similarly, const char k="hjk"; is also invalid C.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

k seems an array of char. Try to use:

const char k[] = "something";
printf("%s", k);
pradeexsu
  • 1,029
  • 1
  • 10
  • 27
Tiago Silva
  • 223
  • 2
  • 6
-1

The statement const char k="hjk"; is not valid C code. Apparently, your compiler accepts it and assigns the memory address where the literal string "hjk" begins to k. Both a memory address and a char are implemented as integer numbers, so the memory address is interpreted as the numeric code for a character.

Because k is now, most likely, an unprintable character, printf("%c", k); will print nothing.

Exactly the same happens when you do const char NEWLINE ='\n';.

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • There is a conversion from pointer to char, not from integer. – Gerhardh Nov 13 '20 at 10:50
  • Yes, but it doesn't make sense to convert a pointer to a char. The reason a pointer is converted to a char is that a pointer is, in essence, an integer number and it makes sense to convert an integer to a char, because each char is represented by an integer number in memory. To make this clear, I mentioned 'integer' in my answer. – www.admiraalit.nl Nov 13 '20 at 10:54
  • No, the statement `const char k="hjk";` results in a compiler diagnostic for writing invalid C code. – Lundin Nov 13 '20 at 11:27
  • @Lundin, thank you, I have edited my answer and I have given your answer +1. – www.admiraalit.nl Nov 13 '20 at 11:42