First use in this function [closed] - c

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 3 months ago.
Improve this question
I started learning C recently, and whenever I try to run this it's giving an error of "a" and "b" undeclared as first use in this function, even, though the course I was watching ran the same code with no erros, he just changed the number to 10 and 15 thats all
#define sum (a,b)(printf("%i", a + b));
int main ()
{
the enitre code is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Name "JSDJSDJ"
#define Age 3000
#define sum (a,b) (printf("%i",a + b));
int main ()
{
sum(100,150);
printf ("%i", Age);
return 0;
}
sum (100,150);
}
Not much other than changing the numbers

When you define a macro with parameters, you can't have spaces between the macro name and (. This is an exception to the usual rule that whitespace is insignificant between tokens in C, because the preprocessor needs to be able to distinguish between a function-like macro and a macro that expands into something surrounded by ().
So it should be
#define sum(a,b) (printf("%i", (a) + (b)));
You should also always wrap macro parameters in parentheses in the replacement list, to avoid problems with operator precedence when the argument is an expression.

If you really want a 'function' then the code should look like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Name "JSDJSDJ"
#define Age 3000
void sum (int a, int b){
printf("%i",a + b));
}
int main ()
{
sum(100,150);
printf ("%i", Age);
return 0;
}

Related

usefullness of function name in parenthesis excluding the parameters [duplicate]

This question already has answers here:
Invoke function instead of macro in C
(3 answers)
Why surround the function with parentheses?
(1 answer)
What do the parentheses around a function name mean?
(3 answers)
Closed 4 years ago.
I stumbled over a weird piece of code that I thought would not work the way it is intended, but it did. Example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%d\n", abs(-1)); // output: 1
printf("%d\n", (abs)(-1)); // output: 1
return 0;
}
Apparently putting parenthesis around the function name in a function call has no effect on the program. It just makes it look like a cast.
I am kinda interested if this is defined somehow. But I figure that it just not forbidden and that's why it works.
What I am really curious about is, is there a case where this kind of notation might yield an advantage in any kind? (structure of the code, readability, anything this might be useful for)
Or is it just a "weird" way to write code?
Try
int foo(int a, int b) { return a+b; }
#define foo(a,b) ((a)+(b)+1)
#include <stdio.h>
int main() {
printf("%d %d\n",
foo(3, 5), // Use macro if defined
(foo)(3, 5) // Always call function even if there's a macro
);
return 0;
}
Output:
9 8
See the difference?

strange behavior of #define command [duplicate]

This question already has answers here:
Syntax error while using a #define in initializing an array, and as arguments to a function in C? [closed]
(2 answers)
Closed 5 years ago.
I've tried to figure out this behavior of the define command in C (I'm new with that). I've got this code and I don't know why I see in the output that myAge=15 and not 16 (I know it's 15, but I don't know why). Anybody can help me to find out why does it happen?
this is the code:
#include <stdio.h>
#include <stdlib.h>
#define AGE 15;
int main(void)
{
float myAge = AGE + 1;
printf("Hello!\n");
printf("My name is Raz and I am %d years old!\n", myAge);
system("PAUSE");
return 0;
}
Thanks for helping :)
#define is a textual replacement performed by the preprocessor prior to the compilation step. In this case, you're asking the preprocessor to expand the token AGE to 15;. The semicolon is part of the expansion, so this is the code you would get after the preprocessing step:
float myAge = 15; + 1;
As you can see, it does not expand to what you expect.
You can fix this issue by removing the semicolon from the #define:
#define AGE 15
Better yet, avoid using the preprocessor for simple numerical constants - consider using a const int instead:
const int age = 15;

C Programming Math Functions #include <stdlib.h> abs() [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
I start learing C programming from "Beginning Programming with C For Dummies" by Dan Gookin.
I have a problem with understanding "C Math Functions" - my question is how to use #include <stdlib.h> and abs() function. Only explanation in the book is this:
Here is a simple example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = -42;
int j = abs(i);
printf("i = %d, j = %d\n", i, j);
return 0;
}
LIVE DEMO
Besides the examples given above,functions work just like a mathematical function
in that y = f(x) you put in x and it returns a y(of course some dont return anything and some dont take in anything)
now what you need to do is to catch that value when it returns it by storing it into some memory location which is called a variable that you declare
its also important that you know what the return type is so that you dont truncate or lose some part of the result
for example if the result is a floating point number then you need to return the value into a float/double variable and if it is a integer you can store it in either int or double/float
also if it is a char you would probably want it to be returned to a char variable and so on and so forth
So any function that you write or that you use from somebody elses library/header is going to work like that
It takes in an argument(sometimes it can even take no arguments and you just use it by calling it with parentheses, parentheses must always be typed because otherwise it will look like a variable or something else and convention says so) and it returns some result(if it does return a result) or does something else unrelated to the calling functions variables/values
So hopefully that explains what functions are, and how you can use them
Now all of this described also applies to the functions you posted about, they have a set algorithm that they do which somebody else wrote, and you just give it the argument, and catch the return type and it will do what it says it intended to do i.e abs() gives you the absolute value, pow() returns the square of some base and so on
The line #include <stdlib.h> gives your code access to certain functions and abilities that are within that library. One of which is the abs() function.
The abs() function return the absolute value of an integer, by that we mean it will always convert it to a positive number.
Example:
#include <stdio.h> /* printf */
#include <stdlib.h> /* abs */
int main ()
{
int n,m;
n=abs(23);
m=abs(-11);
printf ("n=%d\n",n);
printf ("m=%d\n",m);
return 0;
}
Edit & Run
Output:
n=23
m=11

#define and then printf not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
For an assignment i must define a variable N as 100, then recall that variable in a printf statement. the code looks like :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define N 100
int main ( )
{
...
printf("Try to guess a number between 1 and N \n\n") ;
...
}
The N is just coming out as N rather than 100.
This is because everything between double quotes are considered as a character array, i.e. a string. So if you want to show N in the string, you shall use it as an "usual" variable:
printf("Try to guess a number between 1 and %d \n\n", N) ;
#define won't expand in literal character string (block of character between "). You should write:
printf("Try to guess a number between 1 and %d \n\n", N)

Can any one explain about X-macros with example code to use them? [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 9 years ago.
Improve this question
I am trying to understand the X-macros topic in detail. But didn't get the full clarity on this. It would be better if any one of the expert will explain this topic with some example "how to use, how to call."
I have found several articles, but didn't get the full clarity on this. In all places, they have used pieces of code where i am lacking in using of those X-macros.
Thanks in advance
Partha
Idea is that you redefine macro X to make data fit your current purpose.
You need at least 2 files. First is a huge table with necessary information, and others where data is used.
table.x:
X("Human", 2, HUMAN)
X("Spider", 8, SPIDER)
module.c:
// ID constants
enum {
#define X(description, legs, id) id,
#include "table.x"
#undef X
COUNT // Last element is total number of elements
};
// Leg array
int NumberOfLegs [] = {
#define X(description, legs, id) legs,
#include "table.x"
#undef X
};
// Description array
const char * Descriptions [] = {
#define X(description, legs, id) description,
#include "table.x"
#undef X
};
Preprocessed output would be:
// ID constants
enum {
HUMAN,
SPIDER,
COUNT // Last element is total number of elements
};
// Leg array
int NumberOfLegs [] = {
2,
8,
};
// Description array
const char * Descriptions [] = {
"Human",
"Spider",
};
In the above example it is easy to add new items to tables. If you managed those lists separately, it would be more easier to make an error.
Edit:
Some clarification on macro usage.
In first line #define X(description, legs, id) legs, we define X macro. Macro must have same number of arguments as our table.x has on each line. For this usage we are only interested on legs parameter. Note that argument names are meaningless, we could as well do #define X(a, b, c) b,.
Second line #include "table.x" includes contents of table.x to module.c. Because macro X has been defined, preprocessor does text replacement to each line with call to X.
Third line #undef X is only for convenience. We remove definition of X so it can be redifined later without compiler throwing warnings.
You basically #define a list of variables as parameters to an place holder macro X:
#define X_LIST_OF_VARS \
X(my_first_var) \
X(another_variable) \
X(and_another_one)
You then use the template:
#define X(var) do something with var ...
X_LIST_OF_VARS
#undefine X
to make code blocks. For example to print all your vars:
#define X(var) printf("%d\n", var);
X_LIST_OF_VARS
#undefine X
will generate:
printf("%d\n", my_first_var);
printf("%d\n", another_variable);
printf("%d\n", and_another_one);

Resources