so i have this C code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
int c;
scanf("%d", &b);
scanf("%d", &a);
c = a + b;
printf(c);
return 0;
}
but, after i insert number for a and b, the program stop working. please help me
C noob here
In your code you have the following line is wrong:
printf(c);
as the printf() syntax would be like what i've written below
printf("%d",c);
so your now code would be:
#include <stdio.h>
int main()
{
int a;
int b;
int c;
scanf("%d", &b);
scanf("%d", &a);
c= a + b;
printf("%d",c); //this is the correct printf() syntax
return 0;
}
printf(c);
should be
printf("%d\n", c); /* `\n` at the end of the string flushes the `stdout` */
because printf expects a const char* as its first argument, not an int.
Related
Ok I have been trying to figure this out for 3 hours and I have search a bunch of stack overflow questions and answers but I have this same error:
/usr/bin/ld: /tmp/ccXbXNRV.o: in function 'main':
main.c:(.text+0x5a): undefined reference to 'plus'
/usr/bin/ld: main.c:(.text+0x67): undefined reference to `minus'
collect2: error: ld returned 1 exit status
And I cant figure this out because my code doesn't seem that he is the problem
main.c:
#include <stdio.h>
#include "funcs.c"
int main()
{
int z = 0;
int wh = 1;
while (wh == 1)
{
printf("What you want?\n1-Plus\n2-Minus\n");
scanf("%d", &z);
if (z == 1)
{
plus();
}
if (z == 2)
{
minus();
}
}
printf("The program ended\n");
return 0;
}
funcs.c
#include <stdio.h>
inline void plus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a + b;
printf("The result is: %d\n", a);
}
inline void minus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a - b;
printf("The result is: %d\n", a);
}
help.h
extern int a;
extern int b;
extern int z;
extern int wh;
inline void minus(void);
inline void plus(void);
I have try to compile it with this command gcc funcs.c main.c
I know that is a simple program but I really want to learn c
If you could help I would be very thankful!
You can fix this by doing three things:
Don't include a .c file. You're already providing it to gcc on the command line.
Include help.h in files where you use those functions.
Not using inline. You can't use inline when the caller and callee are in different translation units.
main.c:
#include <stdio.h>
// #include "funcs.c"
#include "help.h"
int main()
{
int z = 0;
int wh = 1;
while (wh == 1)
{
printf("What you want?\n1-Plus\n2-Minus\n");
scanf("%d", &z);
if (z == 1)
{
plus();
}
if (z == 2)
{
minus();
}
}
printf("The program ended\n");
return 0;
}
funcs.c
#include <stdio.h>
void plus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a + b;
printf("The result is: %d\n", a);
}
void minus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a - b;
printf("The result is: %d\n", a);
}
help.h:
extern int a;
extern int b;
extern int z;
extern int wh;
void minus(void);
void plus(void);
Compile and run like so:
$ gcc -Wall -Werror funcs.c main.c
$ ./a.out
What you want?
1-Plus
2-Minus
^C
Other thoughts:
extern int a;
extern int b;
extern int z;
extern int wh;
You're already declaring these variables locally. This is unneeded. The extern keyword tells the compiler that these variables are defined in another translation unit that it can't see. This isn't true, so you should just remove these.
Say I have the following program:
#include <stdio.h>
typedef unsigned int el_type;
int main(void)
{
el_type n;
scanf("%d", &n);
printf("%d\n", n);
return 0;
}
Is there a conversion character I can use in place of %d in this situation to print n?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i = 4;
double d = 4.0;
char s[] = "HackerRank ";
int a;
double b;
char c[60];
scanf("%d %lf", &a, &b);
scanf("%[^\n]", c);
char r[100];
strcpy(r, s);
strcat(r, c);
a += i;
b += d;
printf("%d\n%.1lf\n%s", a, b, r);
return 0;
}
And the output i get for this code is
16
8.0
HackerRank �j�&�
And the expected output is
16
8.0
HackerRank is the best place to learn and practice coding!
Can someone help me out with the error regarding the strcat part?
I have made a program to print out all ASCII character between two given character but then I write it in functions. The output of two these programs are different. I tried using pointer to pass variables by reference but the output is unlikely to be like the first program. What should I do for making it correctly ?
Here is the first program in C by using linear programming.
#include <stdio.h>>
int main()
{
char a,b,tmp;
int d;
scanf("%c%c",&a,&b);
if(a>b)
{
tmp=a;
a=b;
b=tmp;
}
d = b - a;
for (char c = a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
Here is the other program in functions.
#include <stdio.h>>
void ascii(char a,char b);
int main()
{
char a,b,tmp;
int d;
printf("Enter 2 character => ");
scanf("%c%c",&a,&b);
ascii(&a,&b);
}
void ascii(char a,char b)
{
int d;
if (a>b)
{
char tmp= a;
a=b;
b=tmp;
}
d=b-a;
for (char c=a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
you should not pass the addresses of your variables to your function the correct one is
#include <stdio.h>>
void ascii(char a,char b);
int main()
{
char a,b,tmp;
int d;
printf("Enter 2 character => ");
scanf("%c%c",&a,&b);
ascii(a,b);
}
void ascii(char a,char b)
{
int d;
if (a>b)
{
char tmp= a;
a=b;
b=tmp;
}
d=b-a;
for (char c=a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
It prints the characters in reverse order just fine except when the string is 8 characters long.
Eg -
"what man" gives "am tahw" Why ?
whereas "what many" gives "ynam tahw" just as it should.
#include <stdio.h>
int main(void)
{
char a[100];
char x;
char*i = a;
printf("Enter a message:");
while ((x = getchar()) != '\n')
{
*i = x;
i++;
}
while (i >= &a[0])
{
printf("%c", *i--);
}
printf ("\n");
}
Modification in your code:
Change printf("%c", *i--); to printf("%c", *--i); in while loop.
You can improve the quality of your program as shown below:
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}