Many output methods of Hello World

Keywords: ascii C

I. C language

1. Direct output

  • printf(), puts() output directly
#include<stdio.h>
int main()
{
    printf("Hello World!\n");
    puts("Hello World!");
    return 0;
}
  • Put() string splicing output
#include<stdio.h>
int main()
{
puts("Hello"" ""World""!");
return 0;
}

2. Output single characters one by one

#include<stdio.h>
int main()
{
    printf("%c%c%c%c%c%c%c%c%c%c%c%c",'H','e','l','l','o','W','o','r','l','d','!');
    return 0;
}

3. Output in array form

 #include<stdio.h>
 int main()
 {
    char str[20]="Hello World!";
    printf(str);
    printf("\n");
    return 0;
}

4. The representation of ASCII

  • Decimal system
#include<stdio.h>
 int main()
 {
    putchar(72);//H 
    putchar(101);//e 
    putchar(108);//l 
    putchar(108);//l 
    putchar(111);//o 
    putchar(32);// Blank space 
    putchar(87);//w
    putchar(111);//o 
    putchar(114);//r 
    putchar(108);//l 
    putchar(111);//d 
    putchar(33);//!
  return 0;
 }
  • Hexadecimal
#include<stdio.h>
 int main()
 {
    putchar(0x48);//H 
    putchar(0x65);//e 
    putchar(0x6c);//l 
    putchar(0x6c);//l 
    putchar(0x6f);//o 
    putchar(0x20);// Blank space 
    putchar(0x57);//w
    putchar(0x6f);//o 
    putchar(0x72);//r 
    putchar(0x6c);//l 
    putchar(0x64);//d 
    putchar(0x21);//!
  return 0;
 }
  • Octal number system
#include<stdio.h>
 int main()
 {
    putchar(0110);//H 
    putchar(0145);//e 
    putchar(0154);//l 
    putchar(0154);//l 
    putchar(0157);//o 
    putchar(040);// Blank space 
    putchar(0127);//w
    putchar(0157);//o 
    putchar(0162);//r 
    putchar(0154);//l 
    putchar(0144);//d 
    putchar(041);//!
  return 0;
 }

5. Define macro definition output

#include <stdio.h>
#define Say(sth) puts(#sth)
int main()
{
   Say(Hello World!);
   return 0;
}

[explanation]
#define is a macro definition. When compiling a program, you can directly replace Say(sth) with puts (ා sth); Say(Hello world!) with puts (ා Hello world!).
”#"Is used in macro definition to convert macro parameters to strings. puts("Hello world!) is also equivalent to puts(" Hello world! ").

6. Pointer output

#include <stdio.h>
main()
{
char *str="Hello World!";//*str is a pointer to an address
printf("%s",str);
return 0;

}

7. Function call output

#include <stdio.h>
void Display()
{
printf("Hello World!");

}
int main()
{
    Display();
    return 0;

}

7. strcpy function connection

#include <stdio.h>
#include<string.h>
int main()
{
    char str1[]="Hello ";//Leave a space for the last digit
    char str2[]="World";
    strcat(str1,str2);//Connect the two strings and place the result in str1 (str1=Hello World!)
    printf(str1);
    return 0;
}

Posted by feri_soft on Thu, 02 Jan 2020 19:56:44 -0800