I want to create a program that will print the value of the three parameter by using define in c
#include <stdio.h>
#define Print(num) printf("%d",n##num)
int main()
{
int i;
int n1=1, n2=2, n3=3;
for(i=1;i<=3;i++)Print(i);
}
the problem than n##num equal to ni and to n1,n2,n3.
is there a way to get the values of i to set in num instead of "i"?
The preprocessor runs before the code is even compiled. You can't do loops in the preprocessor like that.
What you really want is an array:
#include <stdio.h>
int main()
{
int i;
int n[] = { 1, 2, 3 };
for(i=0;i<3;i++) printf("%d\n", n[i]);
}
Related
Why this code isn't work? I know there are other ways to do this but it must be this way. How can I fix this problem?
#include <stdio.h>
#include <stdlib.h>
int total(int arr[3], int size_of_array){
int i=0;
int total=0;
for(i=0;i<size_of_array;i++){
total+=arr[i];
}
return total;
}
int main(){
int array2[3]= {0,1,8};
int total(array2, 3);
}
int main(){
int array2[3]= {0,1,8};
int theAnswer; // Make an integer for the result.
theAnswer = total(array2, 3); // Call the function, and store the result.
printf("The answer is %d\n", theAnswer); // Show the result to the user
return 0; // main has to return 0 to indicate "success"
}
This is what I've done so far but the result is always 1 no matter if the array has odd or even numbers inside. What am I doing wroing?
#include <stdio.h>
#include <stdbool.h>
int main(){
int vector[10]={2,4,6,8,10,12,14,16,18,20};
bool function(int i){
for(i=0,i<10,i++){
if(vector[i]%2==0){
return true;
}
return false;
}
}
}
}
If to follow the C Standard then you may not define a function inside another function.
Nevertheless the main problem with your function (apart from syntax errors as for example the invalid for statement) is using the return statement for the first element of the array independently on whether it is odd or even.
The program can look the following way
#include <stdio.h>
#include <stdbool.h>
bool is_even( const int a[], size_t n )
{
size_t i = 0;
while ( i < n && a[i] % 2 == 0 ) i++;
return i == n;
}
#define N 10
int main( void )
{
int vector[N] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
printf( "All elements are even: %d\n", is_even( vector, N ) );
}
Take into account that according to the C Standard function main without parameters shall be declared like
int main( void )
For loop should use ; between entries, not ,. You are never looping.
for(i=0;i<10;i++)
...
Honestly, I would expect your compiler to throw at least a warning on this statement as it is missing elements of a basic for loop.
Next, you need to move your
return false;
outside the for loop or it will never iterate past the first entry.
Last, defining function within main is a not standard in C. Make it it's own function and things will be happier.
Because you are returning it on a final iteration. You should return false when you have checked the whole array, not in middle.
Also, your function was never get called and so you can update it using C semantic.
#include <stdio.h>
#include <stdbool.h>
bool check_even(int vector[], int size){
int i;
for(int i=0; i<size; i++){
if(vector[i] % 2==0) {
return true;
}
// return false; shift this to out of for block
}
return false;
}
int main(){
int vector[10]={2,4,6,8,10,12,14,16,18,20};
bool is_even = check_even(vector, 10); // here function will get called
return 0 // always return with main
}
When I execute the following code on Linux, the output is 32. Why is that so?
#include <stdio.h>
#define m 10+2
int main() {
int i;
i = m * m;
printf("%d", i);
return 0;
}
Macro expansion doesn't heed the surrounding syntax, so i=m*m gets expanded into i=10+2*10+2, rather than i=(10+2)*(10+2). This why one should always parenthesize macro definitions and arguments:
#define m (10+2)
i=m*m is actually calculating as
10+2*10+2
which is
10+20+2 = 32
#include<stdio.h>
#define m 10+2
int main()
{
int i;
i=(m)*(m); \\this will provide you with output as (10+2)*(10+2)
printf("%d",i);
return 0;
}
im a 1st grader when it comes to c and need help with storing 5 random values in an array and outputting them. Heres where am at.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct score_card {int A_ones; int B_twos; int C_threes; int D_fours; int E_fives; int F_sixes; int G_chance;};
int dice_rolls[5];
int randomize(void);
int value;
int main(void) {
struct score_card test;
randomize;
int i;
for(i = 0; i <= 4; i++){
printf("%d\n", dice_rolls[i]);
}
printf("\n");
return 0;
}
int randomize(void){
int i;
srand(time(0));
for(i = 0; i <= 4; i++){
value = rand() % 6 + 1;
dice_rolls[i] = value;
}
}
The output is :
6294304
6294308
6294312
6294316
6294320
the goal was to use modular division to get values from 1 -->6 and store them in the dicerolls array.
I see two immediate problems.
First. you're not terminating your random numbers with a newline. That's why they're all strung together in a big sequence. Change your output line to:
printf("%d\n", &dice_rolls[i]);
Secondly, you're not actually calling randomize. The correct way to call it is with:
randomize();
The statement randomize; is simply an expression giving you the address of the function. It's as useless in this case as the expression 42; which also does nothing. However it's valid C so the compiler doesn't necessarily complain.
I have a problem with this simple program because it doesnt give me the true outcome. I just want to sum two arguments in the first function and then use the outcome in the second one. It will be nice to have overall outcome in the main function. Also I would like to ask the same question with arrays.
#include <stdio.h>
#include <stdlib.h>
int sum()
{
int a=2;
int b=3;
int s=a+b;
printf("sum=%d\n",s);
return s;
}
int sum2(int s)
{
int c=5;
int d=c+s;
}
int main(int s,int d)
{
sum();
printf("sum=%d\n",s);
printf("sum2=%d\n",d);
getchar();
return 0;
}
There are many problems with this code:
int main(int s, int d) won't do what you think. Command-line arguments to your program come in string format. So you would need to use int main(int argc, char *argv[]).
The variables s and d in main() are completely independent to the variables in sum() and sum2(). So changing their values in those functions will not affect the original variables.
You're not even calling the second function!
You can do things like this:
int sum(int a, int b)
{
return a+b;
}
int sum2(int c)
{
return c+5;
}
int main(void)
{
int x = 2;
int y = 3;
int z = sum(x,y);
int w = sum2(z);
printf("z = %d\n", z);
printf("w = %d\n", w);
}
First of all, s is a local variable inside sum( ). Hence it cannot be available outside the function.
int sum() {
// ..
int s = a+b; // local variable, hence local scope
// ..
}
Also, secondly, int main(int s,int d) won't work since in command line arguments come in String format. So can't use a int there
I won't tell you the answer (lol but others have) but I'll give you these clues to figuring it out.
Ask your self which functions are returning data and which ones aren't.
Clue: the function needs a return to return some data.
Then ask yourself which functions' returns are actually being used.
Clue: to collect the data returned from a function you need to assign the result to a variable like so
int i;
i = somefunct();
You can't access the value of variable 's' outside the function sum() since it is out of scope. You'll have to return the value to your main() function. Also your main function parameters are incorrect. You need something more like this:
#include <stdio.h>
#include <stdlib.h>
int sum(int a, int b)
{
int s=a+b;
printf("sum=%d\n",s);
return s;
}
int sum2(int c, int sum)
{
return c+sum;
}
int main(int argc, char *argv[])
{
int val1 = sum(2, 3);
printf("sum=%d\n",val1);
int val2 = sum2(5, val1);
printf("sum2=%d\n", val2);
getchar();
return 0;
}