473,378 Members | 1,218 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,378 software developers and data experts.

Good morning...



This is prasad. I started learning C just now. Thats why my Questions
may be silly . please don`t mind and help me to learn C in a good way.
i tried the following in Vi editor..

My questions are

main()
{
char a;
printf("enter the char\n");
scanf("%c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2

no doubt for the above
main()
{
char a;
printf("enter the char\n");
scanf(" %c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2
if we give space(s) before %c it is printing as in first case why?
main()
{
char a;
printf("enter the char\n");
scanf("%c ",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2

if we give space(s) after %c it is not printing any thing , to come out
of that program we have to press ctrl+z
why?

main()
{
char a;
printf("enter the char\n");
scanf("% c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is
if we give a space between % and c it is printing as above why?

thanking you in advance

bye....

Jan 22 '07 #1
5 1563
prassu wrote:
>
This is prasad. I started learning C just now. Thats why my Questions
may be silly . please don`t mind and help me to learn C in a good way.
i tried the following in Vi editor..

My questions are

main()
{
char a;
printf("enter the char\n");
scanf("%c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2

no doubt for the above
You may wish to declare a as an int. It will be promoted to int
when it is place on the stack in the call to scanf anyway, and
then again in the call to printf. Furthermore, there is a
"character constant," EOF, that a char cannot represent, that
is often used in character-based I/O routines. Though, to
my knowledge, scanf() doesn't, getchar() does.
>
main()
{
char a;
printf("enter the char\n");
scanf(" %c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2
if we give space(s) before %c it is printing as in first case why?
Check books like "The C Programming Language." Unfortunately, I only
have the first edition of the Kernighhan and Ritchie book available.
In that book, the relevant information is found on page 147.

On page 148, the first edition clearly states that blanks, tabs and
newlines in a control string are ignored. Thus, this is standard
behavior.
main()
{
char a;
printf("enter the char\n");
scanf("%c ",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2

if we give space(s) after %c it is not printing any thing , to come out
of that program we have to press ctrl+z
why?
Did you try typing a space after the 2?
>
main()
{
char a;
printf("enter the char\n");
scanf("% c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is
if we give a space between % and c it is printing as above why?
Because "% " is not a valid conversion specification.
>
thanking you in advance

bye....
You're welcome in advance.

Take care.
Jan 22 '07 #2
In article <e5**************************@ALLTEL.NET>,
Jeff Mullen <41*@nls.netwrote:
....
>when it is place on the stack in the call to scanf anyway, and
You're not allowed to say the word "stack" in this newsgroup.

The regs will be all over you like Cubans on Miami.

Jan 22 '07 #3

"prassu" <ha**************@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
>

This is prasad. I started learning C just now.
Which book(s) are you using? Your code indicates
it/they are teaching a prestandard, obsolete form
of C.

For answers to your questions, see the link cited below.
Thats why my Questions
may be silly . please don`t mind and help me to learn C in a good way.
i tried the following in Vi editor..

My questions are

main()
{
char a;
printf("enter the char\n");
scanf("%c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2

no doubt for the above
main()
{
char a;
printf("enter the char\n");
scanf(" %c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2
if we give space(s) before %c it is printing as in first case why?
main()
{
char a;
printf("enter the char\n");
scanf("%c ",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2

if we give space(s) after %c it is not printing any thing , to come out
of that program we have to press ctrl+z
why?

main()
{
char a;
printf("enter the char\n");
scanf("% c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is
if we give a space between % and c it is printing as above why?
See: http://tinyurl.com/2xt638
(from "Dinkum Compleat Libraries Reference")

Note: Please read and observe Dinkumware's copyright notice at:
http://www.dinkumware.com/manuals/?m...=crit_pjp.html
-Mike
Jan 22 '07 #4
Jeff Mullen said:
prassu wrote:
>>
My questions are

main()
{
char a;
printf("enter the char\n");
scanf("%c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is 2

no doubt for the above

You may wish to declare a as an int.
No need for that - in fact it's probably a bad idea in this case (although,
as you note, it is essential for getchar). But he definitely needs
<stdio.hif he's going to call printf or scanf.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 22 '07 #5
Jeff Mullen <41*@nls.netwrites:
prassu wrote:
This is prasad. I started learning C just now. Thats why my Questions
may be silly . please don`t mind and help me to learn C in a good way.
i tried the following in Vi editor..
My questions are
main()
Should be

int main(void)
{
char a;
printf("enter the char\n");
scanf("%c",&a);
printf("prasad is %c\n",a);
}
o/p : enter the char
2
prasad is 2
no doubt for the above

You may wish to declare a as an int. It will be promoted to int
when it is place on the stack in the call to scanf anyway, and
then again in the call to printf.
scanf's "%c" format expects a pointer to char; you *can't* pass it a
pointer to int.

printf's "%c" expects a value of type int, but a char value (in this
particular context) is promoted to int anyway.
Furthermore, there is a
"character constant," EOF, that a char cannot represent, that
is often used in character-based I/O routines. Though, to
my knowledge, scanf() doesn't, getchar() does.
Right, you need an int to hold the result of getchar(); that doesn't
apply to scanf().

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kirk Larsen | last post by:
Here's what I want: 1. Simplicity. I basically want to be able to post news on the front page, similar to a blog. 2. Good Image Gallery. I would like something that is simple to navigate...
12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
2
by: Sam Martin | last post by:
Morning all, Right, I've read untold articles now, listening to people bitch about there being no Unload method for Assembly. Plenty of people do counter that this is possible by loading the...
6
by: Carlo | last post by:
Good morning someone used the built-in Dotfuscator Community Edition and can give me some suggestions and impressions? Someone knows a good free/opensource obfuscator? TIA Carlo
1
by: BinnuChowdary | last post by:
Very Good news for all freshers and especially those who want to shift to Dotnet Technologies We have established an Training and Development Center Named Z-Axis Technologies in Our City at KPHB,...
7
by: elgiei | last post by:
Good morning at all, i have to implement a server,that every n-seconds (eg. 10sec) sends to other clients,which files and directory has been deleted or modified. i build a n-tree, for each...
9
by: indefatigable | last post by:
Hi, I'm sure this has been done before, but I can't seem to find this one on the net.... I have a datefield but instead of it just displaying: 07/12/18 08:30 I would like to display it...
0
by: handord | last post by:
GOOD MORNING Friends : Today Message : A man can succeed in any endeavour for which he has unlimited enthusiasm. A man who cannot tolerate small ills can never accomplish great things.
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.