View Full Version : Printing a smiling face in C
mehulved
09-04-2007, 02:21 AM
I have been trying to solve this problem in C
"Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value one."
I did misunderstand the program and have made errors, but the program is here http://rafb.net/p/oFCBvw86.html
Now, the main problem is:
I have given
char i;
printf(" %c", i);
Now on compiling and running this program in a terminal, I just get blank spaces no smiling face. I have no clue now, where is it going wrong.
shantanu
09-04-2007, 03:22 AM
yeah it gives smiley now:
try this now:
#include <stdio.h>
int main()
{
int x,y;
char i=1;
for(x=1;x<=800;x++) //since there are 800 pixels on X axis, one char is 1 pixel i assume
{
printf(" %c ", i); //prints a smiley (you forgot to give a space after " %c " )
if(x==800)
printf("\n"); //gives a line break when 800 pixels are reached
y=x;
if(y==800)
break;
}
}
mehulved
09-04-2007, 03:32 AM
Oops, looks like i put it up before updating it, i=1 not 48 in my file right now. OK i will try to add a space after %c and see how it goes.
Can I have a screenshot of what you got? Also, it seems I am missing it by a long way as the chapter covers for, while loops. And it just asks to print one smiling face. With what I am doing, it's missing one or the other.
shantanu
09-04-2007, 03:42 AM
its printing many smiling faces... i think its some looping error..
here is the screen shot : http://img481.imageshack.us/img481/2910/mehulse3.jpg
value of i is varying here... (wait) i am working on it... :p
mehulved
09-04-2007, 03:53 AM
Well so it's seems to be like the smileys won't show on linux. I wonder if it's something windows specific.
shantanu
09-04-2007, 03:56 AM
what value of I you gave... ?? and i dont think the libaries etc. would also be different in windows and linux :p
mehulved
09-04-2007, 04:09 AM
I gave i=1
Later on I was just changing it around to other values cos some friend told me that it might be taking i as int 1. Even before this I made a program to print ASCII values and their equivalent characters, that time too, it just gave me blank output for many of those ASCII characters.
If not the libraries then could it be the compiler? Can someone try compiling this program on DevC++(I guess it has C compiler too) and see if it comes out right?
Garbage
09-04-2007, 07:04 AM
Try this....
Only change the values of x & y in loops according to screen size.
I wrote this program as my college assignment.
It is compiled using Bloodshed DevC++
Correct me if I'm wrong plz.
#include <stdio.h>
#include <conio.h>
int main()
{
int x,y;
int i=1;
for(y=1;y<=20;y++)
{
for (x=1;x<25;x++)
printf(" %c ", i); //prints a smiley
printf("\n"); //goes on next line
}
getch();
}
aditya.shevade
09-04-2007, 10:44 AM
Well so it's seems to be like the smileys won't show on linux. I wonder if it's something windows specific.
I also think that it is OS specific. I have designed a tictactoe program, where I have printed the gameboard using ascii char. But they all get messed up in Linux.
Gotta run now, will post the O/P later.
Aditya
Hi,
You can try this code and see if 1 actually gets printed as a smiley.
main()
{
int i=0;
for(i=0;i<256;i++)
{
printf("%d = %c\t",i,i);
}
getchar();
return 0;
}
As you would have guessed, it will print the number and corresponding ascii character. Then you will be able to see, which number prints the smiley.
I have had the same problem with my program as said above. Instead of printing the lines, the ascii characters are printed as ? and :. Maybe you need the change the default encoding style.
Will try and tell you.
Aditya
mehulved
09-04-2007, 12:05 PM
aditya I have already made that program earlier and it gives me quite a lot of blank spaces. At first I had no clue why it's happening so, but now I think there's something that's either different or it's missing. I will try asking this on irc, maybe we'll come to know what's the reason it's happening.
casanova
09-04-2007, 11:24 PM
#include <stdio.h>
int main()
{
int x;
char i=1;
for(x=1;x<=28*80;x++) //since there are 80 columns and 24-30 rows and increment the counter with no. of chars within the quotes.
printf("%c", i);
}
This program is enough to run it. Sry, I dont run linux and updated the proggy as it was logically incorrect.
When in text mode it is 80 chars in a single row and somewhere around 24-30 columns.
http://img353.imageshack.us/img353/8654/smileypm1.th.jpg (http://img353.imageshack.us/my.php?image=smileypm1.jpg)
mediator
10-04-2007, 12:49 AM
Yes, windows and linux libraries r different. Thats what zeeshan and I discussed in another thread!
Take a look at here => The Ascii Table (http://www.asciitable.com/)!
Now u can clearly observe that 48 is the ascii value of 0. So u cannot print a smiley on 48.
Also u can witness that the characters whose ascii value ranges from 32-126 are readable characters consisting of alphabet, "+" ,commas,numbers(0-9) etc!
The ASCIIs out of this range r the ones which give funny characters!
Try this modified code! And u can create a "for loop" for the those non-readable ASCIIs to see what funny charcters they give!
#include <stdio.h>
int main()
{
int x,y;
int i=28;
for(x=1;x<=800;x++) //AFAIK, pixels on x axis depend on resolution!
{
printf(" %c ", i); //dont know about smiley but 48 is the ascii value of 0!
if(x==800)
printf("\n"); //gives a line break when 800 pixels are reached
y=x;
if(y==800)
break;
}
}
mehulved
10-04-2007, 03:09 AM
Firstly, we are getting it wrong. That's the mistake even I made in the first place. The question clearly says that it's just 1 smiling face not so many.
And yeah in the question they've mentioned that ASCII value of 1 should be used, so the question is faulty by itself. The author at the beginning has intimated that he'd give a note if there were differences so I will try to mail him and see about this. By the way, this is from 'Let Us C' by 'Yashwant Kanetkar'.
mediator
10-04-2007, 09:19 AM
Your question is no longer there in the link. Also, how can there be one smiling face when u have put up a loop of size 800? :confused:
mehulved
10-04-2007, 09:41 AM
Read my first post again. The question is there on the second line.
And I have already said that I didn't read the question properly, I realised it while putting up this post.
But, I doubt if it will work anyways as it seems the ASCII value 1 doesn't print the smiling face on linux at all.
aditya.shevade
10-04-2007, 09:59 AM
^^ Ascii value of 1 prints nothing here. I am using OpenSuSE 10.2. Moreover, I checked all the values..... there is no smiling face anywhere.....
Aditya
mediator
10-04-2007, 10:05 AM
Okies!
"Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value one."
If this is what u were asking then it sounds very illogical to me! On one hand its saying to fill the "entire screen with a smiling face" and then it says it has an ascii value 1 :confused: ?
U cannot AFAIK, print a small funny character on the entire screen!
OR, it cud be that the question hints at how the smiling face looks like with its ascii character?
I don't have much experience with these funny characters. But what I observed is that windows and linux (diff. OSs) may be having different funny characters for non-readable ASCII codes. And if u r practising Yashwant Kanetkar's C, then u shud practise it on "Turbo C 3". I dunno, but he may not be having experience with linux's c programming.
So if u r programming in C on Linux then I suggest u read some UNIX programming book or some book like GCC-The complete reference!
REY619
10-04-2007, 12:13 PM
Sorry for offtopic, but can someone give me the link to download that Turbo C++ IDE Compiler??
I searched but cant find...
Thanx.
mediator
10-04-2007, 02:32 PM
http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=9zB&q=turboc3%2Bdownload&btnG=Search
mehulved
10-04-2007, 03:29 PM
I doubt if yashwant kanetkar doesn't know C on Linux, cos the last 2 chapters are dedicated to C in the book. Maybe it's us who're missing the point here. There is a separate book for answers which can be bought but I am not keen on buying it at this point of time.
I will refer to gcc manual and stuff later on when I come to C programming under Linux.
And I am not interested in adding wine/dosbox so tc is out.
Yamaraj
10-04-2007, 04:24 PM
Here's how you can print ASCII 1 smiling face on the screen, and it has nothing to do with libraries and operating systems. It'll work on any OS, with any compiler and on any system that supports ANSI ASCII codes.
#include <stdio.h>
int main(void)
{
printf("\x001\n");
return 0;
}
NOTE: The ISO C standard doesn't "know" or care about a screen or an A4 paper size, or the size of my shoe. Writing a C program to "fill the entire screen" with a single character involves non-standard C programming techniques, and you're better off throwing that "Let us C" in a wastebin. I recommend C Primer Plus by Stephen Prata (5th ed deals with C99 too) for a proper introduction to the "Standard C".
mediator
10-04-2007, 04:41 PM
^This code prints an isosceles triangle at -90 degrees in the FC5's and FreeBSD terminal! :D
Yamaraj
10-04-2007, 04:45 PM
//since there are 800 pixels on X axis, one char is 1 pixel i assume
There are no pixels in console mode, only characters. Though there is no such standard, most console mode screens are 80x25. I've modified your program to print a LOT of smiling faces based on such a screen.
#include <stdio.h>
#define ROWS 80
#define COLS 25
int main(void)
{
int x = 0;
char ch = 1;
for(x = 0; x < ROWS*COLS; x++)
printf("%c", ch);
printf("\n");
return 0;
}
mehulved
10-04-2007, 04:54 PM
Here's how you can print ASCII 1 smiling face on the screen, and it has nothing to do with libraries and operating systems. It'll work on any OS, with any compiler and on any system that supports ANSI ASCII codes.
#include <stdio.h>
int main(void)
{
printf("\x001\n");
return 0;
}
NOTE: The ISO C standard doesn't "know" or care about a screen or an A4 paper size, or the size of my shoe. Writing a C program to "fill the entire screen" with a single character involves non-standard C programming techniques, and you're better off throwing that "Let us C" in a wastebin. I recommend C Primer Plus by Stephen Prata (5th ed deals with C99 too) for a proper introduction to the "Standard C". Well the code of yours again gives me a blank space, I am using gcc-4.1.1.
Well can you explain what exactly does that \x001 mean in the printf line.
About the book, well what would the C Primer Plus cost? If I can afford it I will buy it a bit later on.
Yamaraj
10-04-2007, 05:10 PM
Well the code of yours again gives me a blank space, I am using gcc-4.1.1.
Well can you explain what exactly does that \x001 mean in the printf line.
About the book, well what would the C Primer Plus cost? If I can afford it I will buy it a bit later on.
1. That's because ASCII code 1 is an unprintable character, and what's displayed in place of it on the console/terminal is dependent on the system. Windows displays a smiling face. Solaris GUI displays a bullet character, but the console displays a blank - same as yours. Mediator's systems displays something else.
2. \x is an escape sequence, which prints the hexadecimal character constants following it. \x001 or \x01 in this case, will print hex 01.
3. It shouldn't cost more than 300-350/-. But if you can find in on P2P networks, you can have it for the cost of bandwidth required to download it. But it's good to have a book in hardcopy rather than in electronic format.
^This code prints an isosceles triangle at -90 degrees in the FC5's and FreeBSD terminal! :D
Read answer #1 above.
mehulved
10-04-2007, 05:18 PM
1. That's because ASCII code 1 is an unprintable character, and what's displayed in place of it on the console/terminal is dependent on the system. Windows displays a smiling face. Solaris GUI displays a bullet character, but the console displays a blank - same as yours. Mediator's systems displays something else.
All right
2. \x is an escape sequence, which prints the hexadecimal character constants following it. \x001 or \x01 in this case, will print hex 01.
Ok. got that now.
3. It shouldn't cost more than 300-350/-. But if you can find in on P2P networks, you can have it for the cost of bandwidth required to download it. But it's good to have a book in hardcopy rather than in electronic format.
300-350 is quite affordable, I have saved some money so should be able to buy it.
I am not interested in using P2P for such things and anyways I don't prefer electronic format at all for such extensive reading and learning.
mediator
10-04-2007, 05:20 PM
Here's how you can print ASCII 1 smiling face on the screen, and it has nothing to do with libraries and operating systems.
1. That's because ASCII code 1 is an unprintable character, and what's displayed in place of it on the console/terminal is dependent on the system. Windows displays a smiling face. Solaris GUI displays a bullet character, but the console displays a blank - same as yours. Mediator's systems displays something else.
:confused: I think it has to do with operating systems then!
Yamaraj
10-04-2007, 05:30 PM
300-350 is quite affordable, I have saved some money so should be able to buy it.
I am not interested in using P2P for such things and anyways I don't prefer electronic format at all for such extensive reading and learning.
In case you can't find it - go for "A Book on C" by Iral Pohl, or "C : How To Program" by Deitel & Deitel. Deitel's book is a little expensive and heavy at more than 1000 pages, but it's a good text.
:confused: I think it has to do with operating systems then!
You're confusing the source code with the result. The code is in standard C and does NOT depend on system, compilers etc. But, since we're trying to print an "unprintable" character, the result can vary *even on the same system*. The outcome is not limited to a smiling face, a triangle, a bullet or a blank. A system-X could actually throw an alien out of the display in trying to print an unprintable. It's called "undefined behaviour". Try using different terminals and consoles in any Linux or UNIX system and see what happens.
mediator
10-04-2007, 05:36 PM
Yea thats what I thought. Thanx for the confirmation brother! Neways, the graphics programs that run in turbo c do not run in gcc. Also getch() doesn't work in gcc and gives error when I include conio.h. Are those in standard C?
Yamaraj
10-04-2007, 05:56 PM
Turbo C must NOT be used for any type and form of C and/or C++ coding. It's an outdated compiler, with non-standard extensions that are not supported by the ANSI/ISO committee or other compilers. There are much better and free compilers available even for Windows.
Any type of a graphics program in C or C++ is non-standard. The languages do not support graphics programming at all. This is the reason why portable libraries exist, and platforms like Java have become popular.
conio.h and getch() are not a part of standard C. These are some of the non-standard extensions supported by some compilers like Turbo C/C++. clrscr() is another example.
mediator
10-04-2007, 06:05 PM
Hmmm....I wondered! Neways thanx for clearing the confusion! :)
shantanu
10-04-2007, 10:19 PM
Originally Posted by shantanu_webmaster
//since there are 800 pixels on X axis, one char is 1 pixel i assume
There are no pixels in console mode, only characters. Though there is no such standard, most console mode screens are 80x25. I've modified your program to print a LOT of smiling faces based on such a screen.
Code:
#include <stdio.h>
#define ROWS 80
#define COLS 25
int main(void)
{
int x = 0;
char ch = 1;
for(x = 0; x < ROWS*COLS; x++)
printf("%c", ch);
printf("\n");
return 0;
}__________________
well Mr. yamaraj
i dint say that... it was a part of post made by T_Y_F :D
Yamaraj
10-04-2007, 10:32 PM
well Mr. yamaraj
i dint say that... it was a part of post made by T_Y_F :D
O'reilly? I quoted this post of yours - http://www.thinkdigit.com/forum/showpost.php?p=468308&postcount=2
shantanu
10-04-2007, 11:12 PM
oh my god... i copied the program of Mr. T_Y_F.. it was pre-written in that... i didn't write it.
(the program was in link given by Tech_your_future. :-)
mehulved
10-04-2007, 11:19 PM
Yeah it was my mistake, I had added that comment in my program. That's another thing I learnt.
Pathik
10-04-2007, 11:43 PM
@yamaraj can u suggest a good book which extensively covers c++ .. From the basics to the advanced stuff??
Yamaraj
11-04-2007, 12:41 AM
@yamaraj can u suggest a good book which extensively covers c++ .. From the basics to the advanced stuff??
Don't expect any one book to elevate you from writing a "Hello, World" to becoming a C++ guru. A recommended path is Beginner>Intermediate, and Intermediate>Advanced.
1. C++ Primer Plus - Stephen Prata
2. C++ Primer, 4th Ed - Stanley Lippman
3. The C++ Programming Language, 3rd Special Edition - Stroustrup
There are others like Accelerated C++, Effective C++, Exceptional C++ etc for more advanced take on the language. Remember, all these books deal with the standard C++ language only. You'll need a book or two on the C++ library, others on Windows/Linux/UNIX programming interfaces, algorithms & data structures et cetera.
The path to becoming a C++ master is long. Take one small step at a time.
oh my god... i copied the program of Mr. T_Y_F.. it was pre-written in that... i didn't write it.
(the program was in link given by Tech_your_future. :-)
Nevermind. His original link was expired by the time I clicked on it.
aditya.shevade
11-04-2007, 07:21 AM
@Yamraj
Can you suggest any book on C/C++ for linux programming interfaces? Or any site which might give the information.
And thanks for the list above, I was looking for that.
Aditya
mehulved
11-04-2007, 12:03 PM
OK I did ask this on irc yesterday night, this is what I got http://pastebin.ca/434096
praka123
11-04-2007, 12:19 PM
I got C for Dummies 2nd ed by Dan gookin(not sure 2nd ed avail in india).Is a nice one for even a non computer guy.
www.c-for-dummies.com/
Yamaraj
11-04-2007, 03:11 PM
OK I did ask this on irc yesterday night, this is what I got http://pastebin.ca/434096
So, they happen to agree with me, hmm?
Just to let you know, C_Dreamer is Richard Heathfield - who authored "C Unleashed" with several others. It's an excellent advanced level book that deals with different problems, how to tackle them in C.
He frequented in the Yahoo Programming:1, and we had some interesting talk over there during 2000-02.
@Yamraj
Can you suggest any book on C/C++ for linux programming interfaces? Or any site which might give the information.
And thanks for the list above, I was looking for that.
Aditya
What kind of Linux programming interfaces are looking for? There are different books for everything - for example, if you're to learn Linux network programming in C you should have UNIX Network Programming. Same for GTK+ GUI development, or learning Vi[M]/Emacs, Tcl/Tk, kernel programming etc.
It is recommended that you learn ANSI/ISO standard C first. When you're confident in your skills, you can always go for specialization later.
aditya.shevade
11-04-2007, 09:51 PM
Actually I am looking for GTK + GUI development.... as far as basic C is concerned, I have been using C under Windows for quite a long time now, but I have just switched to linux. I read about signals and fork, child, etc in 'Let Us C', by Y. Kanitkar.
I wanted to know if there is any book, which explains all this in a bit more detail. (Basic C programming in linux).
Thanks,
Aditya
Yamaraj
11-04-2007, 10:27 PM
Actually I am looking for GTK + GUI development.... as far as basic C is concerned, I have been using C under Windows for quite a long time now, but I have just switched to linux. I read about signals and fork, child, etc in 'Let Us C', by Y. Kanitkar.
I wanted to know if there is any book, which explains all this in a bit more detail. (Basic C programming in linux).
Thanks,
Aditya
Check ACCU book reviews - http://accu.org/index.php/book_reviews/ and pick only "recommened" and "highly recommened" titles from your local book store.
anantkhaitan
08-05-2007, 10:48 PM
ASCIIs from 33-126 are available in Linux other are not shown I don't understand why??
Anyways I want a function similar to getch() i.e. waiting till a key is pressed and returning char/int value of the key. Plz if anyone can help. I m also looking for gotoxy() funtion.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.