Newline Constants in C

Viewed 508

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.

4 Answers

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.

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.

k seems an array of char. Try to use:

const char k[] = "something";
printf("%s", k);

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';.

Related