LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2005, 10:56 AM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
what is the difference between ch and 'ch' - C programming


I had posted a question just few hours back and thought that isntead of asking this in the same thread, let's start a new thread. I hope this is fine with mods.

ANyway I just wanted to ask that what is the difference between

char ch=3;
and
char ch='3';
and
chat ch="3";

when I uses printf("%d",ch);
than first one print 3 where as second one print 51 (ascii code for 3)

Thanks
 
Old 09-30-2005, 11:00 AM   #2
-X-
Member
 
Registered: Oct 2003
Location: Tx,USA
Distribution: Slackware, Red Hat, CentOS
Posts: 495

Rep: Reputation: 30
Start here. http://cprogramming.com/
 
Old 09-30-2005, 11:08 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Re: what is the difference between ch and 'ch' - C programming

Quote:
Originally posted by indian

when I uses printf("%d",ch);
than first one print 3 where as second one print 51 (ascii code for 3)
It appears you've already answered your question yourself. Unless the real question is something else? If you changed your %d to %c your first one would print a garbage ASCII character, and your 2nd would print 3. You can assign chars a direct ASCII value, which can be useful at times. If you changed it to ch = 51; and printed out ch as a char, you'd get 3 printed out.

Your third example above should give a compiler error. You can't assign a string to a single char like that.
 
Old 09-30-2005, 11:32 AM   #4
indian
Member
 
Registered: Aug 2004
Posts: 137

Original Poster
Rep: Reputation: 15
Thanks...

@deuissum - nice sites u have.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
difference between... GEJOE DANIEL General 3 11-21-2005 05:49 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
what is the difference? mjm70 Mandriva 1 06-14-2005 11:04 AM
whats difference between OT and GTK programming cranium2004 Programming 1 03-04-2005 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration