Jump to content

C language: Error message: Conversion may lose significant digits?

- - - - -

  • Please log in to reply
9 replies to this topic

#1
gautham

gautham

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
C Program to accept and display "5" characters using getchar() and putchar() functions:

here is the code:


#include<stdio.h>

void main()

{

char ch[6];


ch[0]=getchar();

ch[1]=getchar();

ch[2]=getchar();

ch[3]=getchar();

ch[4]=getchar();


putchar(ch[0]);

putchar(ch[1]);

putchar(ch[2]);

putchar(ch[3]);

putchar(ch[4]);

}

When i am Compiling this code in "C" language then it is displaying this Error message: "Conversion may lose significant digits", what could be the reason?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What is the exact error message? What line is it on?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
spcm0012

spcm0012

    Newbie

  • Members
  • PipPip
  • 13 posts
What about using fscanf and fprintf instead?
I was able to run your code fine and got the following output:

Quote

[spcm0012@munro ~]$ ./test
abcde
abcde[

What compiler are you using?

Also, char ch[] only needs to be of length 5 since you only have 5 instances of ch being used (0,1,2,3,4), so char ch[5] will do.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think that warning is because getchar function returns an integer, and you're storing them in char array.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
spcm0012

spcm0012

    Newbie

  • Members
  • PipPip
  • 13 posts

Flying Dutchman said:

I think that warning is because getchar function returns an integer, and you're storing them in char array.
Yepp, returns as an unsigned char cast to int. If the op uses scanf or fscanf, he can designate that is reading in a char, and use printf to print out the char, and have no warnings.

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
He said he is required to use getchar and putchar. Try int in your declaration.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#7
gautham

gautham

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Now error message is not coming but why it is not working properly?

---------- Post added at 03:26 PM ---------- Previous post was at 03:23 PM ----------

It is accepting only two characters and displaying back.

#8
spcm0012

spcm0012

    Newbie

  • Members
  • PipPip
  • 13 posts
When inputting the data if you are using spaces between the inputs it will count those as characters. Run it again without puttings spaces between your input characters. If you loop the getchar() functions, you would be able to use the return key to separate the characters you want to input without it having this issue.

#9
gautham

gautham

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts

Quote


thanks mate for your reply but while inputting i am not giving any spaces, if you can compile this code, you will understand.


#10
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Well! You got the wrong type.
getchar
int getchar(void);

If successful, return the object requested from the stream (the current object in the input stream to which the stream points). Char values are returned as unsigned char converted to an int. In your case, you are assigning that returned value to the char type array.

putchar
int putchar(int c);

The putchar() function writes the character c (converted to an ``unsigned char'') to the output stream (stdout).

Change the array type from char to int.

Source
manpagez
I think i'm able to write a code for printing "Hello, World!". Proud of that!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users