Printing a smiling face in C

Status
Not open for further replies.

Yamaraj

The Lord of Death
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.

Code:
#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".
 

Yamaraj

The Lord of Death
shantanu_webmaster said:
//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;
}
 
OP
M

mehulved

18 Till I Die............
Yamaraj said:
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.

Code:
#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

The Lord of Death
tech_your_future said:
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.

mediator said:
^This code prints an isosceles triangle at -90 degrees in the FC5's and FreeBSD terminal! :D
Read answer #1 above.
 
OP
M

mehulved

18 Till I Die............
Yamaraj said:
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

Yamaraj said:
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.

Yamaraj said:
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

Technomancer
yamaraj said:
Here's how you can print ASCII 1 smiling face on the screen, and it has nothing to do with libraries and operating systems.
yamaraj said:
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

The Lord of Death
tech_your_future said:
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.

mediator said:
: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.
 
Last edited:

mediator

Technomancer
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

The Lord of Death
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.
 

shantanu

Technomancer
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

The Lord of Death
shantanu_webmaster said:
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 - *www.thinkdigit.com/forum/showpost.php?p=468308&postcount=2
 

shantanu

Technomancer
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. :)
 
OP
M

mehulved

18 Till I Die............
Yeah it was my mistake, I had added that comment in my program. That's another thing I learnt.
 

Pathik

Google Bot
@yamaraj can u suggest a good book which extensively covers c++ .. From the basics to the advanced stuff??
 

Yamaraj

The Lord of Death
pathiks said:
@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.

shantanu_webmaster said:
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

Console Junkie
@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
 

praka123

left this forum longback
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.
[SIZE=-1]www.c-for-dummies.com/[/SIZE]
 
Status
Not open for further replies.
Top Bottom