David House
David House

Reputation: 143

printing integers as characters, decimals, floats

Let me start by saying that I am brand new to programming. I am a math major and I don't know very much about the computer programming world. That said, my assignment wants me to enter an integer and print it out as the corresponding ASCII character, decimal, float. I am ok going from a character to the corresponding integer, but the reverse has me puzzled. Please help!

 #include <stdio.h>

    int main (void)
    {
        char E = 'E';
        char e = 'e'; 
        char D = 'D';
        char d = 'd';
        int m;

    printf("\nPlease enter an integer : ");
    scanf("%d", &m);

    printf("\nThe number as a character is : %c", E, e, D, d);

    return 0;
}  // main

This is what I have so far, but I know it's wrong.

Upvotes: 2

Views: 197

Answers (2)

Wolf
Wolf

Reputation: 121

I am not exactly sure what you want to achieve.

printf will treat a parameter as a type you want using % format specifier.

So if you have entered some value which is interpreted as signed decimal integer you can print it treating as a different type with the printf function.

If you want your m variable being printed as character do:

printf("The number as a character is %c", m);

If you want to display it as a float, use %f.

Here is some reference: http://www.cplusplus.com/reference/cstdio/printf/

Upvotes: 2

Work of Artiz
Work of Artiz

Reputation: 1090

You're probably looking at the printf formats

#include <stdio.h>

int main (void)
{
    int m;

    printf("Please enter an integer : ");
    scanf("%d", &m);

    printf("The number as a character is : '%c' %02X %o %d %f", m, m, m, m, (float) m);

    return 0;
}

Upvotes: 1

Related Questions