I'm trying to do a simple exercise with C! Help! needed!

so i was doing this exercise wher i need to get the birth year of a person, the atual year and then calculate the age and print something if the age is greater or equal to 18.
however, the part where i put the birth year and the actual year doesn’t actually print the numbers i write.
I’m new to this so i would like some help :sweat_smile:

#include <stdio.h>

int main() {
    int n, a;
    char str[25];
    int idade = a - n;
/* idade (eng) = age(pt) */ 
    printf("write your birth year, actual year and name:\n");
    scanf("%s, %d, %d", &str, &n, &a);
    printf("%s, \n %d, \n %d, \n", str, n, a);
    /* Btw this is what appears: " Playground file0.c: In function 'main':
Playground file0.c:10:13: warning: format '%s' expects argument of type 'char ', but argument 2 has type 'char (*)[25]' [-Wformat=]
   10 |     scanf("%s, %d, %d", &str, &n, &a);
      |            ~^           ~~~~
      |             |           |
      |             char *      char (*)[25]
Playground file0.c:7:19: warning: 'a' is used uninitialized in this function [-Wuninitialized]
    7 |     int idade = a - n;
      |                 ~~^~~
 Playground file0.c:7:19: warning: 'n' is used uninitialized in this function [-Wuninitialized]  "

    printf("Since you were born in %d and we are in %d, your age is: %d \n", n, a, idade);
    if (idade >= 18){
        printf("%s, a sua entrada foi permitida.", str);

/* your entry was permited/accepted (eng) = a sua entrada foi permitida(pt) */
    return 0;

So the error messages mean exactly what they say. The trick is understanding what they say.

You are computing idade before getting the values of a and n (it is good practice to use actual words as variable names instead of obscure letters). This means that your subtraction is based upon the junk values that happen to be in your computer memory, not the values from the user.

For the string issue, a variable holding a charcter array is a pointer, no need to use the reference operator &.

1 Like

In addition to @JeremyLT’s excellent answer, you’re requesting input in the order birth year, actual year, name but reading it in the order name, birth year, actual year. This is guaranteed to cause problems for your users.



Thank you for your explanation! Actually i learnt pointers yesterday but totally forgot to apply them now. The a and n are like that because they’re short for a - atual/actual and n- nascimento/birth, but i’ll just keep the word instead of only the letter.

1 Like

oh i didn’t think well on that. Thank you! I’ll be careful with that

The idea is that you’ll remember how the code works better when you go back and look at actual words instead of just single letters.

1 Like

So i’ve changed it and now there are no warnings. However, it doesnt print the correct birth year nor the actual year and the subtraction doesnt work…

#include <stdio.h>

int main() {
    int nascimento; 
    int atual;
    int idade;
    char str[25];

    printf("write your name, birth year and actual year:\n");
    scanf("%s*, %d, %d", str, &nascimento, &atual);
    printf("%s, \n %d, \n %d, \n", str, nascimento, atual);
    idade = atual-nascimento;
    printf("Your age is: %d \n", idade);
    if (idade >= 18){
        printf("%s, a sua entrada foi permitida.", str);

    return 0;

Why the *?

What is the output you are seeing?

so i write the name, the birth year and the actual year and it appears like this :

write your name, birth year and actual year:
Your age is: 32764 
Bruna, a sua entrada foi permitida.

the years are all wrong and it doesn’t do the subtraction.
(and i took out the * just now)

I suspect that there is something screwy with the formatting in the scanf string.


s, String of characters, Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.

If you adjust for this, then I can read the numbers as integers.

thank you for the effort but i still don’t get what i should change :sweat_smile:

It’s hard for me to guess without seeing your latest code. Can you please share your latest version?

Specifically the commas. Those shouldn’t be there.


1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.