Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-30-2005, 11:56 AM   #1
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;
char ch='3';
chat ch="3";

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

Old 09-30-2005, 12:00 PM   #2
Registered: Oct 2003
Location: Tx,USA
Distribution: Slackware, Red Hat, CentOS
Posts: 495

Rep: Reputation: 30
Start here.
Old 09-30-2005, 12:08 PM   #3
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

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, 12:32 PM   #4
Registered: Aug 2004
Posts: 137

Original Poster
Rep: Reputation: 15

@deuissum - nice sites u have.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:16 AM
difference between... GEJOE DANIEL General 3 11-21-2005 06:49 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 03:42 AM
what is the difference? mjm70 Mandriva 1 06-14-2005 12:04 PM
whats difference between OT and GTK programming cranium2004 Programming 1 03-04-2005 11:40 PM

All times are GMT -5. The time now is 11:37 PM.

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