Dear All,

I am running C programs on DEV-C++. Its 32-bits compiler like MS visual..

Kindly go through the following program.

Program:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <math.h>

main()

{

char ch;

unsigned char u_ch;

int i;

unsigned u_i;

short int s;

unsigned short u_s;

long l;

unsigned long u_l;

float f;

double d;

long double l_d;

 

/*Entering the data*/

printf("1. Enter character = ");

fflush(stdin);

scanf("%c", &ch);

printf("2. Enter unsigned character = ");

fflush(stdin);

scanf("%c", &u_ch);

printf("3. Enter integer = ");

scanf("%d", &i);

printf("4. Enter unsigned integer = ");

scanf("%u", &u_i);

printf("5. Enter short integer = ");

scanf("%d", &s);/*

printf("6. Enter unsigned short integer = ");

scanf("%u", &u_s);

printf("7. Enter long integer = ");

scanf("%ld", &l);

printf("8. Enter unsigned long integer = ");

scanf("%lu", &u_l);

printf("9. Enter float = ");

scanf("%f", &f);

printf("10. Enter double = ");

scanf("%lf", &d);

printf("11. Enter long double = ");

scanf("%Lf", &l_d);

 

/*Printing data*/

printf("1. Character = %c\n\n", ch);

printf("2. Unsigned Character = %c\n\n", u_ch);

printf("3. Integer = %d\n\n", i);

printf("4. Unsigned integer = %u\n\n", u_i);

printf("5. Short = %d\n\n", s);/*

printf("6. Unsigned short = %u\n\n", u_s);

printf("7. Long = %ld\n\n", l);

printf("8. Unsigned long = %lu\n\n", u_l);

printf("9. Float = %f\n\n", f);

printf("10. Double = %lf\n\n", d);

printf("11. Long double = %Lf\n\n\n", l_d);

 

system("pause");

}

Here, I can enter the data of all data type in section “/*Entering the data*/”. I enter 1 (i) for integer, 2 (u_i) for unsigned integer and 3 (s) for short. And others also.

But my compiler prints 0 for unsigned integer u_i.

I.e. Compiler assigns 0 to unsigned integer u_i, even I entered value 2 to it.

When I use same program to TURBO – C, I am getting right output.

Can any body help?

Rpstata

Edited 5 Years Ago by Narue: Added code tags...not that it helped. :P

Dear All,

Kindly go through the following program.

Dear rpstata,

Kindly remove all extraneous blank lines (and comments) and indent your code properly so we can follow it.

Then we can understand the program flow and it will fit on our screen and help mucho bueno.

Code is:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

main()
{
char ch;
unsigned char u_ch;
int i;
unsigned u_i;
short int s;
unsigned short u_s;
long l;
unsigned long u_l;
float f;
double d;
long double l_d;

/*Entering the data*/

printf("1. Enter character = ");
fflush(stdin);
scanf("%c", &ch);
printf("2. Enter unsigned character = ");
fflush(stdin);
scanf("%c", &u_ch);
printf("3. Enter integer = ");
scanf("%d", &i);
printf("4. Enter unsigned integer = ");
scanf("%u", &u_i);
printf("5. Enter short integer = ");
scanf("%d", &s);/*
printf("6. Enter unsigned short integer = ");
scanf("%u", &u_s);
printf("7. Enter long integer = ");
scanf("%ld", &l);
printf("8. Enter unsigned long integer = ");
scanf("%lu", &u_l);
printf("9. Enter float = ");
scanf("%f", &f);
printf("10. Enter double = ");
scanf("%lf", &d);
printf("11. Enter long double = ");
scanf("%Lf", &l_d);

/*Printing data*/

printf("1. Character = %c\n\n", ch);
printf("2. Unsigned Character = %c\n\n", u_ch);
printf("3. Integer = %d\n\n", i);
printf("4. Unsigned integer = %u\n\n", u_i);
printf("5. Short = %d\n\n", s);/*
printf("6. Unsigned short = %u\n\n", u_s);
printf("7. Long = %ld\n\n", l);
printf("8. Unsigned long = %lu\n\n", u_l);
printf("9. Float = %f\n\n", f);
printf("10. Double = %lf\n\n", d);
printf("11. Long double = %Lf\n\n\n", l_d);

system("pause");
}
Comments
Where's the indentation? I even added a link for you.

Have you tried the format string "%lu" for the unsigned integer? If that works, then the compiler is at fault, or their library implementation is (more likely).

Have you tried "%hd" for a short int? scanf() is -very- picky about pointer-types and how to deal with them.

(For people with more time on their hands, start reading up on stdarg and variable argument-list processing, which is why you can pass a format string and then the correct number of additional arguments into scanf/printf.)

This article has been dead for over six months. Start a new discussion instead.