Beginner question, can somebody help me solve it?

0 Wakak Laaa · January 20, 2016
I would like to create a program that display the name you have inserted and show the difference of the 2 numbers you have inserted. Here's the coding I have done, but I cant figure it out why can't it work.  Anybody can help me?

#include <stdio.h>
#include <stdlib.h>

int main()
    char name;

    printf("Please enter your name\n");
    scanf("%s", &name);

    int a,b,sum;
    printf("Hello %s, please insert 2 numbers:\n", &name);
    sum = a + ~b + 1;
    printf("Difference of two integers: %d",sum);

    return 0;

Btw, this is my first time in the forum and beginner to C programming. Sorry for being noob :D.

Post a Reply


Oldest  Newest  Rating
0 asad bilal · January 24, 2016
int main()
{ //Start main func

char name[50];

printf("Please enter your name\n");
scanf("%s", name);

int a,b,sum;
printf("Hello %s, please insert 2 numbers: \n",name);
printf(" Enter value of A & B respectively \n");
scanf(" %d %d",&a,&b);

sum = a - b ;
printf("Difference of two integers: %d",sum);

return 0;
} //end main function
0 Gustavo Zapata · January 24, 2016
char name <-- This is why. C doesnt have string type variables. The only variable type used to manage text is char (short for character), however it may only hold one character. For example, you can make:

name = 'a'
name = 'z'
name = '-'

If you want a string, you can use an array of char:

char name[n];

n<--any whole number you want.

char name[20];
char name[250];
char name[54];

Using that, you may scan a whole word and put it inside "name". If you want to scan multiple words, you would need to use other function or some tricks with scanf. I strongly suggest you learn to work with arrays and strings first.
0 Ruben L · January 27, 2016
Well, for starters, you used <&> symbol instead of <%> in the scanf function as a format specificator. Secondly the format specificator you used was float <%f> even though you declared <a> and <b> as int for which you should use <%d>
Last, why would you go <a+ -b+1> if you only want the difference of those numbers. Where did you come up with that formula ?:)
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator