1

im a beginner with a not so good teacher and trying to figure things out on my own...

#include<stdio.h>
#include<conio.h>

int n,x,c,a;
main()
{
    scanf("%d",&n);
    do
    {
        scanf("%d",&x);
        a=x;
        c++;
    }
    while(c!=n);
    printf("%d",a);

}

in this code, i'd like to know if it's possible to change the 'a' in (a=x;) to, lets say 'b' then 'c', then 'd'.... i want to store the different values from scanf("%d",&x); in different variables. Example, if i enter the values, 1,2,3,4, i'd like the output to be a=1,b=2,c=3,d=4

thanks for the help

5
  • Look up arrays. Check manual page for scanf for return values. Initialise variables Commented May 10, 2015 at 7:17
  • Be careful when using variables you are not explicitly initializing, for example c. It works now because global variables are zero-initialized, but if you make c a local variable it will not be initialized and c would have an indeterminate value, and using it uninitialized would lead to undefined behavior. And you really should make the variables local, having global variables is considered a big no-no by most. Commented May 10, 2015 at 7:19
  • thanks for the response, although there are some concepts you mentioned that i dont understand (beginner...). zero-initialized,uninitialized,undefined behavior,global variables. would you care to explain please? (it's ok if you dont, google and stuff...) Commented May 10, 2015 at 7:23
  • what he means is that you should assign a value to c before the do/while loop. Right now it works, but you should do it to avoid potential problems with it Commented May 10, 2015 at 7:38
  • oooh.... looking back, this makes sense. well, thanks, i'll keep it in mind. Commented May 10, 2015 at 7:44

1 Answer 1

2

I strongly recommend you to use arrays, if your prof. will be ok with it. And there is a question: do you want to just print it ? or change ..

#include<stdio.h>
int main(){
    int n[4];
    int i;

    for(i=0;i<4;i++){
        printf("Enter 4 numbers");
        scanf("%d",&n[i]);  
    }
    for(i=0;i<4;i++){
        printf("%c = %d\n",(97+i),n[i]);    
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of (97+i) it would be better to write 'a' + i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.