Help - Search - Members - Calendar
Full Version: Learning C
BleepingComputer.com > Software > Programming
   
xx66stangxx
#include <stdio.h>

int x, y;

int main (void )
{
for (x=0; x<10; x++, printf ("\n") )
for (y=0; y<10; y++)
printf( "%c", 1 );

return 0;
}
Grinler
Helping in chat. The problem is that when he runs the program it shows the console prompt quickly and then exits. This is because its a console program and needs to be run from the console. If you want to see the output when you run from Bloodshed Dev C++ you need to add a pause in there that will keep the program running until you press any key. This can be done via:

CODE
#include <stdio.h>

int x, y;

int main (void )
{
for (x=0; x<10; x++, printf ("\n") )
for (y=0; y<10; y++)
printf( "%c", 1 );
system("PAUSE");
return 0;
}


The system("PAUSE"); will pause the program till you press any key allowing you to see the output from the console program.
xx66stangxx
#include <stdio.h>

int x, y;

int main (void )
{
for (x=0; x<10; x++, printf ("\n") )
for (y=0; y<10; y++)
printf( "%c", );
system ("PAUSE") ;

return 0;
}


and this is what shows up
c:\docume~1\owner\mydocu~1\hello.c: In function `main':
c:\docume~1\owner\mydocu~1\hello.c:9: parse error before `)'
Grinler
Your missing your argument for the printf statement. Make it:

printf("%c",1);

like you had previously.
xx66stangxx
sweet it worked thanks!
Swandog46
There are a number of problems here:

1) Your variables x and y are global since they are declared outside the main() function. This is considered bad form because if you want to use a variable somewhere else called 'x', you will overwrite this one or have unexpected effects. Keep variables local whenever possible (almost always).

2) The program will print out 10 newlines, and then print out 10 instances of an unprintable character whose ASCII representation is 1. This is almost certainly not what you wanted. If you wanted the actual character '1' printed out, you would need to do:

printf("%c", '1');

since '1' is the ASCII representation of the character 1 (it's actually 49 I believe), whereas just a plain 1 will actually be the number 1.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.