printing int in c by %s in printf

2019-09-22 05:01发布


The compiler doesn't mark it as any error.

  • I know that it's wrong,
  • but I want to know why it's showing the name of the company.

    1. And it doesn't show this message if I type any value less than 4,
    2. it gives different outputs for different such values, try some values like 50, 100,etc.

    Why it's showing such strange outputs.

Here is the program.

void main()

  int a=6;




You are using wrong specifier for int data type. Its undefined behavior. Any thing could happen.

Undefined Behavior:

Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended.


Your program behaviour is undefined since %s is not a valid format specifier for an int.

Exactly why it gives you the values it does is down to conjecture. The compiler also reserves the right to eat your cat. And your dog given that void main() is not compliant with later C standards.

C intentionally does not guard against your doing this as that would compromise the performance of the language. It's difficult (although not impossible as C is statically typed) for a compiler to warn you of this.


It shows you not name of the company. Compiler treats 6 as pointer and prints character data from that address until it finds escape sequence \0 If you compile this code with different compiler, you will most likely get different results, thats why it is called undefined behavior, as it been rightfully pointed in other answers.


First of all you should really start to use the minimum standard. There are a lot of code missing in here and of course you are using an invalid format specifier. You started with an int but you try to print it as a String (%d vs %s) Also your Main Function it's not ok and the return type is missing too. This is what you need:


int main(void){

    int a=6;

    return 0;

标签: c string