c - Switch: Declared Variable outside switch and use it in it -


the following isn't it's part causing problem:

int s,p;         scanf("%d",s); switch(s) {     case 1:     {     p=10;     break; } case 2:     {     p=15;     break; } } printf("%d",p); 

the problem p prints random , large number, causing it?

so used of advice , know have following code:

int s,p=0;         scanf("%d",&s); switch(s) {     case 1:     {     p=10;     break; } case 2:     {     p=15;     break;  } default: {     printf("number invalid");     return 0; } } printf("%d",p); 

now it's going default though enter 1 or 2

ok worked thank all!

"int p" declaration assigns p arbitrary value: whatever happened in memory. now, if s doesn't equal 1 or 2, value never changes, , that's see. can is

  • add default: clause switch() , assign p meaningful there
  • declare p "int p = 0;"

Comments

Popular posts from this blog

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

apache - Add omitted ? to URLs -

php - How can I stop spam on my custom forum/blog? -