Skip to main content

1. Input Output Formatting

 

Access Specifiers

  • %d              : signed int
  • %u              : unsigned int
  • %s               : string
  • %c               : char
  • %f               : float
  • %lf              : double
  • %o              : octal
  • %x  or %p : hexadecimal
  • %e or %E  : scientific notation
  • %hd            : short int
  • %ld             : long int

Range of data types

The memory size of the basic data types may change according to 32 or 64-bit operating system.
Let's see the basic data types. Its size is given according to 32-bit architecture.

Data TypesMemory SizeRange
char1 byte−128 to 127
signed char1 byte−128 to 127
unsigned char1 byte0 to 255
short2 byte−32,768 to 32,767
signed short2 byte−32,768 to 32,767
unsigned short2 byte0 to 65,535
int2 byte−32,768 to 32,767
signed int2 byte−32,768 to 32,767
unsigned int2 byte0 to 65,535
short int2 byte−32,768 to 32,767
signed short int2 byte−32,768 to 32,767
unsigned short int2 byte0 to 65,535
long int4 byte-2,147,483,648 to 2,147,483,647
signed long int4 byte-2,147,483,648 to 2,147,483,647
unsigned long int4 byte0 to 4,294,967,295
float4 byte
double8 byte
long double10 byte

The memory size of int for 64-bit architecture is 4 bytes and that of long int is 8 bytes.
  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        double num = 12345.6789;
        printf("%e", num);
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        double num = 0.000123456789;
        printf("%e", num);
    }
  5. What is the output of the following C code for the following inputs
    1. 12 12
    2. 123 123
    3. 1234 1234
    #include <stdio.h>
    int main()
    {
        int a, b;
        scanf("%2d %3d", &a, &b);
        printf("%d %d", a, b);
    }
  6. What is the output of the following C code for the following inputs
    1. 12 12
    2. 123 123
    3. 1234 1234
    #include <stdio.h>
    int main()
    {
        int a, b;
        scanf("%d %d", &a, &b);
        printf("a=%4d b=%3d", a, b);
    }
  7. What is the output of the following C code for the following inputs
    1. 1.2 1.2
    2. 1.23 1.23
    3. 12.34 12.34
    #include <stdio.h>
    int main()
    {
        float a, b;
        scanf("%3f %4f", &a, &b);
        printf("%f %f", a, b);
    }
  8. What is the output of the following C code for the following inputs
    1. 1.2 1.2
    2. 1.23 1.23
    3. 12.34 12.34
    #include <stdio.h>
    int main()
    {
        float a, b;
        scanf("%f %f", &a, &b);
        printf("a=%4.1f b=%7.2f", a, b);
    }
  9. What is the output of the following C code for the following inputs
    1. abc
    2. abcde
    3. abcdefg
    #include <stdio.h>
    int main()
    {
        char str[5];
        scanf("%s", str);
        printf("str=%s", str);
    }
  10. What is the output of the following C code for the following inputs
    1. a
    2. abc
    3. abcd
    #include <stdio.h>
    int main()
    {
        char str[10];
        scanf("%3s", str);
        printf("%s", str);
    }
  11. What is the output of the following C code for the following inputs
    1. a
    2. abc
    3. abcd
    #include <stdio.h>
    int main()
    {
        char str[5];
        scanf("%s", str);
        printf("str=%3s", str);
    }
  12. What is the output of the following C code for the following inputs
    1. a
    2. abc
    3. abcd
    #include <stdio.h>
    int main()
    {
        char str[10];
        scanf("%s", str);
        printf("%.3s", str);
    }
  13. What is the output of the following C code for the following inputs
    1. a
    2. abc
    3. abcd
    #include <stdio.h>
    int main()
    {
        char str[10];
        scanf("%s", str);
        printf("str=%8.3s", str);
    }

Supression Character (%*d)

  • If used with scanf(), then ignores the current value and takes the next value as input.
  • If used with printf(), then prints some whitespaces and gives value of next variable till the last variable.
  1. What is the output of the following C code with input: 20 25 30 35?
  2. #include <stdio.h>
    int main()
    {
        int a, b, c, d;
        scanf("%*d %d %d %d", &a, &b, &c, &d);
        printf("%d %d %d %d", a, b, c, d);
    }
  3. What is the output of the following C code with input: 20 25 30 35?
  4. #include <stdio.h>
    int main()
    {
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        printf("%d %*d %d %d", a, b, c, d);
    }
Prev Next

Comments