473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Difference between %c and %s in scanf()

11
Hello

I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code:

Expand|Select|Wrap|Line Numbers
  1. int loop = 1;
  2. char userIN;
  3. while(loop)
  4. {
  5. printf("\nIf you want to retrieve tuples prompt y/n: \t");
  6. //items_read = scanf("%s",&userIN);
  7. scanf("%s",&userIN);
  8. if(userIN=='n'||userIN=='N')
  9. {
  10. loop = 0;
  11. printf("\n user prompt: %c \n",userIN);
  12. break;
  13. }
  14. else if((userIN=='y')||(userIN=='Y'))
  15. {
  16. loop = 1;
  17. printf("\n user prompt: %c \n",userIN);
  18. }
  19. else
  20. {
  21. printf("Invalid Response, exiting:\t");
  22. printf("\n user prompt: %c \n",userIN);
  23. break;
  24. }

and why does it break in this code(only after the first prompt):
Expand|Select|Wrap|Line Numbers
  1. int loop = 1;
  2. char userIN;
  3. while(loop)
  4. {
  5. printf("\nIf you want to retrieve tuples prompt y/n: \t");
  6. //items_read = scanf("%s",&userIN);
  7. scanf("%c",&userIN);
  8. if(userIN=='n'||userIN=='N')
  9. {
  10. loop = 0;
  11. printf("\n user prompt: %c \n",userIN);
  12. break;
  13. }
  14. else if((userIN=='y')||(userIN=='Y'))
  15. {
  16. loop = 1;
  17. printf("\n user prompt: %c \n",userIN);
  18. }
  19. else
  20. {
  21. printf("Invalid Response, exiting:\t");
  22. printf("\n user prompt: %c \n",userIN);
  23. break;
  24. }
  25.  
The only difference between the two codes is in the scanf().

Thanks!
Mar 2 '08 #1
6 8724
gpraghuram
1,275 Expert 1GB
If you try to read a string with a character then what happens is array overwrite.
Since you are passing the character variables address you wont even get a compiler error for that.

Raghuram
Mar 3 '08 #2
romcab
108 100+
%c - accepts a character
%s - accepts a string

Hope this helps.

Hello

I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code:

Expand|Select|Wrap|Line Numbers
  1. int loop = 1;
  2. char userIN;
  3. while(loop)
  4. {
  5. printf("\nIf you want to retrieve tuples prompt y/n: \t");
  6. //items_read = scanf("%s",&userIN);
  7. scanf("%s",&userIN);
  8. if(userIN=='n'||userIN=='N')
  9. {
  10. loop = 0;
  11. printf("\n user prompt: %c \n",userIN);
  12. break;
  13. }
  14. else if((userIN=='y')||(userIN=='Y'))
  15. {
  16. loop = 1;
  17. printf("\n user prompt: %c \n",userIN);
  18. }
  19. else
  20. {
  21. printf("Invalid Response, exiting:\t");
  22. printf("\n user prompt: %c \n",userIN);
  23. break;
  24. }

and why does it break in this code(only after the first prompt):
Expand|Select|Wrap|Line Numbers
  1. int loop = 1;
  2. char userIN;
  3. while(loop)
  4. {
  5. printf("\nIf you want to retrieve tuples prompt y/n: \t");
  6. //items_read = scanf("%s",&userIN);
  7. scanf("%c",&userIN);
  8. if(userIN=='n'||userIN=='N')
  9. {
  10. loop = 0;
  11. printf("\n user prompt: %c \n",userIN);
  12. break;
  13. }
  14. else if((userIN=='y')||(userIN=='Y'))
  15. {
  16. loop = 1;
  17. printf("\n user prompt: %c \n",userIN);
  18. }
  19. else
  20. {
  21. printf("Invalid Response, exiting:\t");
  22. printf("\n user prompt: %c \n",userIN);
  23. break;
  24. }
  25.  
The only difference between the two codes is in the scanf().

Thanks!
Mar 3 '08 #3
akk003
11
If you try to read a string with a character then what happens is array overwrite.
Since you are passing the character variables address you wont even get a compiler error for that.

Raghuram
Thankyou for your prompt reply. But userIN happens to be a char variable and it isn't an array.
Mar 3 '08 #4
Parul Bagadia
188 100+
Even i had got the same problem ; when i had used do-while loop;
actually what happens is; many times when we say %c; a value is taken from the buffer; any garbagde value which is already stored there.
Where as a new value is accepted in case of %s..
Mar 3 '08 #5
akk003
11
thankyou all for the quick responses !!!
I now understand the problem which Parul just explained.
Mar 3 '08 #6
Hey, you could try using gets() for a string or getch() for a character, and getche() to echo the reply back to the user(so they see what they typed in). Also, for those functions you might need to
Expand|Select|Wrap|Line Numbers
  1.  #include <conio.h> 
i tend to only use scanf() when reading in number data.

goodluck
Mar 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
18
by: SDZ | last post by:
Could somebody explain in simple forms, what is/are the difference(s) between - scanf and sscanf and ssscanf - printf and sprintf - open and fopen Thanks in advance, SDZ
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
3
by: sushant | last post by:
hi all, whats the difference between assignement and initialisation in C. is assignement related with scanf() and initialisation with (int x=10;) sushant
39
by: August Karlstrom | last post by:
Hello all, If c is a char then is there any difference between c = '\0' and c = 0
18
by: Martin Jørgensen | last post by:
Hi, Today I got a really strange problem... I've made myself a data-file and I read in data from that file.... When I read something like this line: 03 04 05, 00 04 01, 05 03 07, 08 03...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
2
by: subramanian100in | last post by:
Consider the following program named as x.c #include <stdlib.h> #include <stdio.h> int main(void) { unsigned int u; char str;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.