how can we check the integer length?

Recommended Answers

All 12 Replies

If you mean the number of bytes of an int, then the sizeof operator is what you want.

int size_of_int = sizeof(int);

If you mean the number of bytes of an int, then the sizeof operator is what you want.

int size_of_int = sizeof(int);

i mean is to check how many digit you can enter...
i tried yours method by using while loop to check...

printf("Which Years Are You Join?(2-digits) ");
		scanf("%d", &Student[i].regNo.year);
		integer_len[0] = sizeof(Student[i].regNo.year);
		while(integer_len[0] >= 2)
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Which Years Are You Join?(2-digits) ");
			scanf("%d", &Student[i].regNo.year);
			integer_len[0] = sizeof(Student[i].regNo.year);
		}

but it keep looping me the error message whatever i typed in...

Do you mean how large of a value? If you do then check the limits.h library

Do you mean how large of a value? If you do then check the limits.h library

hmm.. nope ?
im trying to check how many digit of user can be enter..
like example of my code above..
is only allow 2-digits.. if you enter 123 you'll get into the error message(while loop)
else if you enter 12 or 1 you will not be inside the loop..

The reason your while loop keeps going, is because 'sizeof(int)' generates a constant value. An int does not expand when you increase its value, so no matter if you

Since you just want to check wether the year is two digits, you can use:

while (Student[i].regNo.year < 100)

This will keep the loop going until a two- or one-digit value has been entered (to allow -01, -02 etc.)

Your example above may make perfect sense to you but I'm having difficulties understanding what it means.

Sorry, here's the full modified source code:

printf("Which Years Are You Join?(2-digits) ");
		scanf("%d", &Student[i].regNo.year);

		while(Student[i].regNo.year < 100)
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Which Years Are You Join?(2-digits) ");
			scanf("%d", &Student[i].regNo.year);
		}

Since the user is allowed to enter any 1- or 2-digit value, the user allowed to enter any value between 0 and 99 (including those values). 100 won't be allowed.

EDIT: I forgot to mention that the value that is enter might be negative (if the 'year' variable is signed). To prevent a negative value we can use one additional check: Student.regNo.year > 0 in the while loop.

The reason your while loop keeps going, is because 'sizeof(int)' generates a constant value. An int does not expand when you increase its value, so no matter if you

Since you just want to check wether the year is two digits, you can use:

while (Student[i].regNo.year < 100)

This will keep the loop going until a two- or one-digit value has been entered (to allow -01, -02 etc.)

ya..
i know about this method..
but is that have other than this method to do ?
like having command to use.. =D
if i use something never learn in class.. bonus marks.. :D

Why don't you scan them in as characters e.g.

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

int main()
{
	char ch[3];
	unsigned long myint = 0;
	ch[2] = '\0';

	fscanf(stdin, "%c%c", &ch[0],&ch[1]);

	myint = strtol(ch, NULL, 10);

	fprintf(stdout, "ans->%lu\n", myint);
	return 0;
}

Note this is something you 'never learned' so please don't past it off as your own...Bonus marks should be earned.

commented: That input is really lame. Why use the bloated fscanf instead of the simple fgetc? -3

Why don't you scan them in as characters e.g.

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

int main()
{
	char ch[3];
	unsigned long myint = 0;
	ch[2] = '\0';

	fscanf(stdin, "%c%c", &ch[0],&ch[1]);

	myint = strtol(ch, NULL, 10);

	fprintf(stdout, "ans->%lu\n", myint);
	return 0;
}

Note this is something you 'never learned' so please don't past it off as your own...Bonus marks should be earned.

hmm..
dude..
so far i only learn text file only...
so as i know that fscanf is only use on text file mode. :x
somemore what is stdin and also stdout. ?

stdout and stdin are part of the standard streams.

stdout and stdin are part of the standard streams.

oh well.. i dont know how to use that command yet.. :-O
is quite new for me.. =x

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.