1

I'm following an introductory tutorial to C programming. It told me to write a simple program that converts degree Celsius to Fahrenheit.

I wrote the code as it was shown in the video but it only prints the first line and then gets stuck.

I don't understand what's wrong with my program:

#include <stdio.h>

//program to convert Celsius to Fahrenheit

int main()
{
    int c;
    int f;

    printf("Enter the temperature in Celsius:");
    scanf("%d\n", &c);
    f=9*c/5 + 32;
    printf("The temperature in Fahrenheit is: %d\n",f);
    return 0;
}

I have recently started using Ubuntu and am using Code Block for building the program with gcc as the compiler.

Please help Thank You;

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 3
    Uhh ....`scanf` waits for you to input a number.... And remove the `\n` from the format string in the `scanf`. – Spikatrix Mar 21 '15 at 12:16

2 Answers2

2

The problem lies in this line of code:

scanf("%d\n", &c);

The escape sequence '\n' does not behave like you think it does in this context: it's not telling scanf() to expect an input x number in the form x\n where '\n' is a linefeed but is actually interpreted as pattern that must be matched exactly since scanf() doesn't expand escape sequences.

From the glibc manual:

Other characters in the template string that are not part of conversion specifications must match characters in the input stream exactly; if this is not the case, a matching failure occurs.

So, if you enter 10\n as input (where \n are actual characters and not a linefeed), your program works.

Since this is obviously not the behaviour that you were looking for, you can solve the problem by removing \n from the template string that you're using to call scanf().

In any case, scanf() by default ignores whitespace (such as '\n') unless you're using %c or %[ as conversion specifiers, so there's no need to try to handle it.

On a side note, there's a mistake in this line

f=9*c/5 + 32;

the correct conversion formula is

f=(9/5) * c + 32;

While doing computer arithmetic the order of operations affects the final result. (Anyway, in this case, it's better to use floats to limit precision loss)

Mariano Macchi
  • 242
  • 1
  • 7
  • `\n` in the format string does not match the exact character `\n` (linefeed). It matches any sequence of whitespace characters. – interjay Mar 21 '15 at 15:47
  • thanks for the answer. solves my problem and also for pointing out the mistake in the arithmetic equation. Else it would have taken me another day to get hat one corrected. Appreciate a lot. Thanks – Subhasis Bose Mar 21 '15 at 22:50
  • "In any case, scanf() by default ignores whitespace (such as '\n')" No. All format specifiers except `%c` and `%[` skip whitspace characters. And `f=(9/5) * c + 32;` is the same as `f=c + 32;`. – Spikatrix Mar 22 '15 at 03:07
  • 1
    Ahem 9/5 returns 1, not 0. ;) – Emil Laine Mar 22 '15 at 09:52
  • Haha you're right, I was thinking (5/9) for some reason. – Mariano Macchi Mar 22 '15 at 09:59
0

In your following program...

#include <stdio.h>

//program to convert Celsius to Fahrenheit


int main()
{
    int c;
    int f;

    printf("Enter the temperature in Celsius:");
    scanf("%d\n", &c);

This line waits for user input on console

    f=9*c/5 + 32;
    printf("The temperature in Fahrenheit is: %d\n",f);
    return 0;
}

When you run the program, you are suppose to enter integer when program is waiting. Once you enter the value, program will continue, do the conversion and show you output.