Formatted output and variable types for beginners in C

  • 2020-06-03 07:47:43
  • OfStack


C language is the introduction language of programming, everyone wants to learn this programming language quickly, this article introduced in detail about C language formatting output and variable type related content, share for your reference and study, let's start with a detailed introduction.

Formatted output

In PHP, we usually use echo and var_dump and print_r to output debugging statements. In C language, we usually use printf to output debugging statements. However, it is a little special that you need to specify the output data type:

#include <stdio.h>
int main(){
 int age = 10;
 printf("I am %d years old.\n", age);

 return 0;

If the above code, we need to specify %d to indicate that the output is an integer, we commonly used output types are:

Tables Are
d 以10进制形式输出带符号整数(正数不输出符号)
u 以10进制形式输出无符号整数
o 以8进制形式输出无符号整数(不输出前缀0)
x 以106进制形式输出无符号整数(不输出前缀Ox)
f 以小数形式输出单、双精度实数
c 输出单个字符
s 输出字符串

Variable types

C language is a static language. When defining variables, the type needs to be specified:

include <stdio.h>
int main(int argc, char *argv[])
 int age = 100;
 float num = 2.345f;
 double super_num = 56789.4532;
 char initial = 'A';
 char str[] = "str";
 printf("age is %d.\n", age);
 printf("num is %f.\n", num);
 printf("super num is %f.\n", super_num);
 printf("char is %c.\n", initial);
 printf("str is %s.\n", str);
 return 0;

In addition to the basic types above, you can also define arrays:

#include <stdio.h>
int main(int argc, char *argv[])
 int nums[] = {10, 15, 20, 14, 28};
 char name[] = "Cook";
 char full_name[] = {
  'T', 'i', 'm','C','o','o','k','\0'
 printf("The first num is %d, the 2nd %d.\n",areas[0], areas[1]);
 printf("name=\"%s\" and full_name=\"%s\"\n",name, full_name);
 return 0;

In C, a string is a one-byte array that ends with '\0' :

#include <stdio.h>
int main(int argc, char *argv[])
    int nums[4] = {0};
    char name[4] = {'a'};
    printf("nums: %d %d %d %d\n",nums[0], nums[1],nums[2], nums[3]);
    printf("name each: %c %c %c %c\n",name[0], name[1],name[2], name[3]);
    printf("name: %s\n", name);
    return 0;

Output results:

nums: 0 0 0 0
name each: a 
name: a

As you can see, if an integer array is not assigned, the default value is 0, and the character array is empty. Moreover, the character array can be output directly as a string.

As for Boolean types, in C language, there is no Boolean type in the real sense, but is represented by 1 integer. Table 0 false, 1 denotes true

Data type size

The size of data type is very common in C language, we can use sizeof to detect 1 length, he returns 1 long unsigned int type, so to format the output with %ld:

#include <stdio.h>
int main(){
  printf("The size of short: %ld\n", sizeof(short));
  printf("The size of int: %ld\n", sizeof(int));
  printf("The size of float: %ld\n",sizeof(float));
  printf("The size of double: %ld\n", sizeof(double));
  printf("The size of char: %ld\n", sizeof(char));

The operation results are as follows (64-bit machine) :

The size of short: 2
The size of int: 4
The size of float: 4
The size of double: 8
The size of char: 1


Related articles: