What exactly does '%Id' mean? (uppercase I, lowercase d) [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm asking about %Id, not %ld.
Can anyone please explain to me what does "I" exactly do:
I For decimal integer conversion (i, d, u) the output uses the
locale's alternative output digits, if any. For
example, since glibc 2.2.3 this will give Arabic-Indic digits in the Persian ("fa_IR") locale.
As an example:
printf("%Id",1);
In other words what is the difference between %d and %Id ?
can anyone please explain it with simple words and simple example stating the difference ?

printf format option I is a glibC extension to select a locale representation for numbers. It is not defined by the C Standard and should not be used in portable code.
If the locale is properly selected and supported by your C library, calling printf("%Id", 1); might produce a string encoding the Unicode code point U+0661 ١ that is the representation of the digit one in arabic.
See http://www.fileformat.info/info/unicode/char/0661/index.htm
Conversely, printf("%d", 1); always prints 1, the western representation of number one.
To make matters even more confusing, 1 is called an arabic numeral, as opposed to roman numeral I... unrelated to the I in %Id.

Related

How to allow user to input both string and int at the same time using C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if someone knows how to allow a user to input both string(char) and integers at the same time where each character of the input is verified. An example of the input would be "05JK1010". I’ve only started studying the C language last week so I only have this figured out
char studentID [20];
printf("Enter your student ID:\n");
scanf("%s", &studentID );
You're close but I think I have a better way of doing this. GNU libc's scanf accepts ranges with its %[ format, like this:
scanf("%19[0-9a-zA-Z]", studentID);
You should put a different size in place of 19 if you resize your array. This is in place to ensure that scanf doesn't overflow the buffer.
Also, you generally shouldn't take the address of one of scanf's parameters if the format specifier for that parameter is %s or %[.
If you require a portable solution, then it's a mouthful:
scanf("%19[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]", studentID);
For more details about scanf, see my post What can I use for input conversion instead of scanf?
C doesn't have a facility to allow integers and chars to be input at the same time.
Like Joshua commented above characters are just integers represented as ASCII symbols.
If you need the numbers entered to be in numerical integer form, you would have to parse them from the string and use various methods to convert them to there numerical value.
C does have a function that will convert ASCII to integer atoi()
http://www.cplusplus.com/reference/cstdlib/atoi/
PLEASE note, atoi() dose not have error checking, this is just to give you ideas.

Converting data to code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In C, I'd like to make a macro called TEST() that takes a valid C arithmetic expression and prints and evaluates it. So as an example, if I were to give it TEST(5*2+3) it would print to console 5*2+3 = 13. Unfortunately, I don't know how to convert an expression to a string nor do I know how to take a given string and evaluate it as code. How would I do this?
You can use the stringification operator to turn the argument into a string, then expand into a printf() call that prints the string and the result. My code assumes that the expression is always an int.
#define TEST(EXP) printf("%s = %d\n", #EXP, (EXP))
For the parsing part of your question:
The Shunting-yard algorithm is what you're after.
It is the most common way (that I know of) to convert a mathematical expression represented by a string of characters into a postfixed version(operators come after the operands instead of between), which is much easier to interpret with code.
Good luck!

Which datatype is used for 10^500 in c language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Problem Statement
Addition is a very basic operation in mathematics. Jimmy was very weak in addition, so his father decided to teach him. Jimmy is given a number and has to perform addition on all the digits of that number till that the large number gets converted into a single digit. Your task is to prepare a program for him so that he can easily find out the final number.
Input Format
First line contains T (1<=T<=100) the number of test cases.
Each test case contains integer N (1<=N<=10^100).
Output Format
For each test case, output the one digit number by repeatedly adding the digits.
Constraints
1<=T<=100
1<=N<=10^500
I'd represent the very large input number as char, and the total of the digits (first pass) will easily fit in an int. You'll need a little more than simple arithmetic, but it shouldn't be difficult (case seems a likely way to manage the job).

Are there ways to specify the conversion character for double and float in C without having to know the number of its decimal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In C I have learned that to specify a conversion character for float or double I will have to use %.xf. Is it possible to not specify and display double or float without having to count up the number of its decimal.
EDIT:Sorry if I am not understanding this because I am just a beginner at programming in general, and C is my first language.
double reallyBigPi = 3.1415914159141591415914159;
printf("Big Pi = %f\n\n", reallyBigPi);
My goal is to print out this input but using the suggested %f I was only able to get Big Pi = 3.141591. So in the end if I want the get that amount I will have to count up the decmial point?
Sadly, neither the C nor the C++ standard libraries expose an interface to Dragon4 (*) (or an improved algorithm) which would correctly determine the number of fractional decimal numbers from a binary floating point representing a decimal number. The libraries are required (by IEEE 754) to correctly format the values, though.
Obviously, determining the number of fractional digits is not equivalent to omitting the precision. Simply omitting the precision behaves the same as specifying the default precision (which, I think, is 6).
You may be able to use Double Conversion as that does expose an interface determining the number of decimals. I found Double Conversion relatively hard to use, though.
(*) Dragon4 is an algorithm described in "How to print floating-point numbers accurately". Note that this link is to an ACM site which asks for payment for the article. I don't have a link to a [legal] free source of the paper.

Plotting with C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a program plot.c to plot a function f(t). I can't use nested For loops unfortunately which make this harder for me.For example a function like : f(t)=t^2-4t+5.
The values of t will be between two values specified as low and high in the program. For each value of t, i want to store an asterisk in the element of a string (i.e. an array of chars) corresponding to the function value f(t), while all leading elements before the asterisk are blank.
This is of course assuming that the f(t) values are rounded to integers.In terms of the array in C, its size could be a variable. For instance:
int m=3*6;
char ex[m];
Here is what the outputs are supposed to look like :
If you have integer values, you could abuse printf format specifications.
If you want to print a * preceeded by n spaces, you could use something like:
printf("%*s\n", n, "*");
Bear in mind tha tif this is for a class assignment, you also need to be able to explain why this works, but the relevant section should be in the man page for printf.

Resources