I know that C uses pass-by-value and we can emulate pass-by-reference with the help of pointers. But, for example, in order to calculate a simple mathematical expression, how do I implement pass-by-name (which is kind of lazy evaluation but not exactly) in C?
C is only pass-by-value. You can't pass by reference or name. With the pre-processor you can do various hacks but not in the C language.
Sometimes, people call passing a pointer "pass-by-reference" but this is not the case. The pointer is passed by value like anything else. C++ is a different story but you asked about C.
You might also be interested in this article discussion this at length
The parameter substitution used by function-like preprocessor macros is sometimes described as being "pass by name".
Related
Is it allowed to use varargs in an autosar C code? If not, why?
I'm not familiar with autosar. I found this document for c++14, which says:
Rule A8-4-1 (required, implementation, automated)
Functions shall not be defined using the ellipsis notation.
The reasoning is, that the ellipsis notation bypasses the type check. It is recommended to use variadic templates, function overloading or function call chain.
I haven't found any rule regarding varargs for autosar c. Is there any rule against varargs in c code? Is there any reason to avoid it? Is there any way to avoid it (I need to implement a logging function with string formatting)?
It is in Misra as well. Misra C is to C as AutoSAR C++ is to C++. It improves code quality, safety, and security. Lots of stdlib things in C is unsafe. But especially strings are a lot harder without things like variable arguments.
What I do (also for logging) is to create multiple functions that is appropriate to logging in specific situations. Some thing like log(text, int, int) and log(text, binary data block, size) etc. as required. Inside these functions there is calls to single variable argument function (usually snprintf) that prints everything to the log. You are not fully compliant but you are close and the use of variable arguments is contained to a specific area of code. If you need to be fully compliant the code is decoupled and easier to change.
I want to follow up on this question: What is the difference between a definition and a declaration?, there is answer about definition and a declaration, but nothing about implementation.
I want also know what is the difference between implementation and definition.
implementation: if you have some pseudo-code or something like an UML-Diagram and write your code on that basis it's an implementation
declaration: a declaration is already in your code where you say the compiler/interpreter: "hey look there is this variable that I want to use but I dont want to give it any value yet"
and finally definition: definition is when you finally assign a value to your variable like x = 4. Like your defining x to be 4 (in your code)
Hope this is helpful to you
Function definition etc is a formal term, but "implementation" is a fuzzy informal term. In plain English, it could refer to your application's implementation of something, like for example a function. The implementation phase of a project is typically the phase where you write all the code. And so on - it depends on context.
In formal/technical C programming terms, implementation means the implementation of the C language. That is: formally implementation means the C compiler and standard library. And the formal term implementation-defined behavior means compiler-specific behavior.
I'm looking for the simpliest way, how to determine return type, arguments and function name from c header file written under C99.
it's my school project, which have to be written in Perl without any libs. So i got a few options, i can use the regular expression, but it's not applicable to the hardest function like folowing:
int * (* func(int * arg[]))();
the return type should be "int * (* )()" and argument is "int * []".
Second way is to use grammar and parse it, but i think, that this is not the right way.
My buddy told me about an existing algorithm which can do it. But he doesn't remember name, or where he saw him. The algorithm was quite simple. Something like: Find first end parenthesis, everything between this end parenthesis and the first-match previous start parenthesis is arguments...
Does anyone have some idea what am I looking for?
Look at the magic decoder ring for C declarations
If you can obtain The C Programming Language by Kernighan and Ritchie. Not only is it the C bible, but in chapter 5 they present code to parse C declarations. You can look there to see how they do it and quite possibly adapt their approach (chapter 5, section 12).
You simply have to build a parser for that kind of problem. Usually the top-down approach (e.g. a recursive descent) would do it for this kind of job. Fortunately top-down parsers are more or less straight forward to implement.
The only hard bit in C like languages is, that these languages are usually at least LL1 (1 token look ahead) or even worse LL2 or more. So sometimes you have to peek a few tokens in advance to find out whether it's a function declaration or a function call for example.
I always asked my self this question, Why printf() in C was designed to accept any number of parameters, isn't that Overloading? if yes how does a pure structured language contains an Object oriented language concept like Method overloading?
isn't that Overloading?
No, there is no overloading in C. It is called a "variadic function".
And no, despite its appearance in C++ and absence from C, method overloading is not an object-oriented concept. It is featured prominently in rather old programming languages, such as Prolog, that are not object-oriented.
It isn't overloading.. any method that takes a variable number of argument is called a variadic function.
Variadic methods on wiki
This is called varargs (variadic number of arguments) and existed since the early days of C.
This has no relation with overloading.
In a sense, it is just 'an open prototype', expressing the fact that prototypes weren't always as strictly used as nowadays. The flexibility of C in this department stems from the way in which parameters are passed: the cleanup of parameters is the responsibility of the caller, which can know how much room they occupied at the required times.
Similar techniques would not have been possible (easy) with competing calling conventions (e.g. Pascal calling convention)
I came across this code in Stephen G Kochan's book, Programming in c. Is this possible?
float absolute_value(x)
float x;
{
-----
-----
}
So, as you can see, the argument x is declared after it's used it the method arguments. This throws an obvious compilation error in G++.
So, which C compiler supports this?
That's the old style K&R format. It's not actually declaring the argument x, rather defining its type. By default, things were int unless otherwise specified.
Back when C was a much simpler language, not that far removed from my beloved BCPL, this was how you gave function arguments their types. None of these prototype stuff that you young whippersnappers take for granted.
Oh yeah, and get off my lawn :-)
This is the original way of declaring the types of function parameters in C. Any properly functioning C compiler is required to accept it. It is not allowed in C++, however, so every properly functioning C++ compiler must reject it (though in both cases, note that some specific combination of compiler flags might be required to achieve proper function). Once upon a time, C compilers only accepted this style, and would reject code like: float absolute_value(float x) {}. This was added (along with function prototypes) while C was being standardized.