This question already has answers here:
What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)
(4 answers)
Closed 6 years ago.
Just started learning c. And I noticed that there are 2 format specifiers for int.
%i
%d
I like to use %i (The i of integer). But I see most people use %d. Is there like a rule or something (like you have rules for variable naming (camelCase or underscores etc.)).
I allready noticed this question but it did not really help.
But this is not my question. My question was which one is more commonly used. And which one should I pick. My question is not wat is the difference.
If you look good at these questions you see that in the other question there is nothing about which one is more commonly used they just say it is interchangeble.
Is it just so that you can choose. Is one of them more common to use or recommanded?
They are the same. Use whichever one you like. One stands for "integer" and the other stands for "decimal".
At a guess, there are two from early, conflicting implementations. And I'm talking like half a century ago.
I use %d and, to be honest, I haven't the faintest idea why.
They mean different things for scanf, though!
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have some old C programs to maintain. For some functions (at least 10) with exactly the same parameters, the programmer utilized a macro to avoid typing the same parameters again and again. Here is the macro definition:
#define FUNC_DECL(foo) int foo(int p1, int p2, ....)
Then, if I want to define function with the same parameters, I need only type:
FUNC_DECL(func1)
Besides avoiding the tedious work of typing same parameters many times, are there any other advantages of this implementation?
And this kind of implementation confuses me a little bit. Are there other disadvantages of it?
Is this kind of implementation a good one?
As I noted in comments to the main question, the advantage of using a macro to declare the functions with the same argument list is that it ensures the definitions do have the same argument list.
The primary disadvantage is that it doesn't look like regular C, so people reading the code have to search more code to work out what it means.
On the whole, I don't like that sort of macro-based scheme, but occasionally there are good enough reasons to use it — this might be a borderline example.
There are at least ten functions with the same parameters. Currently, every function only has 3 parameters.
Oh, only 3 parameters? No excuse for using the macro then — I thought it was 10 parameters. Clarity is more important. I don't think that the code will be clearer using the macro. The chances that you'll need to change 10 functions to use 4 parameters instead of 3 is rather limited — and you'd have to change the code to use the extra parameter anyway. The saving of typing is not relevant; the saving of time spent puzzling over the meaning of the macro is relevant. And the first person who has to puzzle over the code will spend longer doing that than you'd save typing the function declarations out — even if you hunt and peck when typing.
Away with it — off with its head! Expunge the macro. Make your code happy again.
#define is a text processor kind of thing. So, whether you write the full function declaration or use the preprocessor instead, both will do the same thing with similar execution times. Using #define makes a program readable/short and doesn't affect end result at all but more number of #define means more compilation time and nothing else. But generally, programs are used more than they are compiled. So, the usage of #define doesn't hamper your production environment at all.
This question already has answers here:
When should I pass or return a struct by value?
(9 answers)
Closed 7 years ago.
I know the difference between pass by value and pass by reference. I use them and understand how they work in the codes that I've dealt so far. However, I'm looking for a general rule. What is generally the best time to use pointers and what is the best to use actual values? Examples are much appreciated.
As a general rule, pass-by-value for basic types (int, char, etc.), and pass-by-pointer (or better, pass-by-reference) for big data as struct.
Thinking of a struct with 1000 data members, and the cost to copy that gigantic data to a function. It'd be much quicker to pass-by-pointer or pass-by-reference in that case.
This question already has answers here:
Are there any solid large integer implementations in C? [closed]
(7 answers)
Closed 8 years ago.
Recently in programming contest in Here, the problem is pretty straight forward but catch is with worst case scenario which we have to handle data of size 10^10000 .
I tried the program in python which is straight forward as i don't have to specify the datatype(It is taken care by the compiler ) but when i tried with C I couldn't find the correct datatype .
(I tried uintmax_t which didn't work out too).
So how to approach very huge type of data's in C ?
There is no built-in datatype in C that can store that big values. You will either have to write your own implementation or use a library. As this is a competition, though the second is not an option. Every now and then similar problems appear and usually the best approach is to use another language e.g. java(as it is usually available on competitions).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I want to read in a numerical data line from a text file. The line has two parts:
The first part is an index and could be one integer, two integers, ...or no integers at all!
The second part could be one or two numbers of type integer, double, or float.
From the file's header I know what to expect BUT how can I read these lines generally in C without if/casing dozens of cases?? I've thought of unions, X-macros, enums...etc. To add to the problem, I can't point to datatypes in C because I was thinking once I can identify my case I can point to an appropriate datatype. At least I can point to a function.
the goal is to simply load the data into memory. i wanted to use structs but no i think i'm just going to have pointers to each part.
Am I asking too much of C??
You really just have lines containing from 1 to 4 numbers. So:
char line[LINE_MAX]; // populate this somehow
double values[4];
int count = sscanf(line, "%f %f %f %f", &values[0], &values[1], &values[2], &values[3]);
// now count tells you how many values were read, so ignore the others
You might say, "but I wanted integer support too!" Well, you got it. And you got it without any "if" statements. So be happy, and know that a bunch of programming languages (e.g. Javascript, Lua) don't really worry about this distinction either.
i think i'm going to generate functions that all take the same array of pointers.
I'm going to manually write functions that process 0, 1 or 2 indexes but generate the datatypes with macros like
int readingfunc1(aline,arrofp){
return sscanf(aline,"%d XSTRFORMATDATATYPESLIST XSTRFORMATDATATYPESLIST", arrofp[2],arrofp[3])
//arrofp={arrofp[0], NULL, arrofp[2],arrofp[3]}
func0 and func2 as appropriate.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to format a number from 1123456789 to 1,123,456,789 in C?
How can I format a large integral number with commas in C, such that the readability is improved?
222222 should be 222,222 and 44444444 should be 44,444,444.
You do not need to do the formatting yourself; printf in Unix has a ' modifier:
printf("%'d\n", number);
It looks like Visual Studio doesn't support that. This syntax is locale-aware, however.
Use the modulus (%) operation and build your own string.
If you google for "c format thousands separator" then one of the hits is this page http://www.codeguru.com/forum/archive/index.php/t-402370.html
It's C++ though but it should give you an idea of what you can do.