Reputation:
in my following program, I get an error that "passing argument 1 of 'putch' makes integer from pointer without a cast" if I use 'putch' to print blank space.
#include<stdio.h>
#include<conio.h>
main()
{
int row,i,spc;
for(row=1;row<=5;++row)
{
for(spc=5;spc>row;--spc)
putch(" ");
for(i=1;i<=row;++i)
printf("%d",i);
puts("\n");
}
}
I wonder what does it mean here by 'int' while ' ' is a character. Isn't it? Or am I missing something?
Upvotes: 0
Views: 409
Reputation: 310990
For starters do not use non-standard C functions or macros like putch
and headers like <conio.h>
.
Instead of the function putch
you can use the standard function putchar
.
Also according to the C standard the function main without parameters shall be declared like
int main( void )
In this call
putch(" ");
the function argument is a string literal that has the type char[2]
and defined like
{ ' ', '\n' }
Used as an argument it is implicitly converted to pointer to its first character.
It is the reason of the error. Instead of passing an argument of the type int like
putch( ' ' );
you are passing an argument of the type char *
due to the implicit conversion of the string literal to pointer.
Also try to declare variables in the scope where they are used. Otherwise it is difficult to read programs with numerous declarations of variables in the beginning of the program because their purposes are unclear.
You could output your pattern using only two loops.
Here is a demonstrative program
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
int n;
if ( scanf( "%d", &n ) != 1 || n <= 0 ) break;
putchar( '\n' );
for ( int i = 0; i < n; i++ )
{
int value = 1;
printf( "%*d", n - i, value );
while ( !( i < value++ ) ) printf( "%d", value );
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
Its output might look like
Enter a non-negative number (0 - exit): 9
1
12
123
1234
12345
123456
1234567
12345678
123456789
Enter a non-negative number (0 - exit): 8
1
12
123
1234
12345
123456
1234567
12345678
Enter a non-negative number (0 - exit): 7
1
12
123
1234
12345
123456
1234567
Enter a non-negative number (0 - exit): 6
1
12
123
1234
12345
123456
Enter a non-negative number (0 - exit): 5
1
12
123
1234
12345
Enter a non-negative number (0 - exit): 4
1
12
123
1234
Enter a non-negative number (0 - exit): 3
1
12
123
Enter a non-negative number (0 - exit): 2
1
12
Enter a non-negative number (0 - exit): 1
1
Enter a non-negative number (0 - exit): 0
Upvotes: 0
Reputation: 222753
I wonder what does it mean here by 'int' while ' ' is a character.
Your source code contains " "
, but your question here contains ' '
. You should be aware of the difference: Computers do not intuit what you mean. If you type quotation marks, that means something different than if you type apostrophes.
No, ' '
is not a character. In the terms of the C standard, it is an integer character constant. Its type is int
.
" "
is a string literal. It specifies a static array of 2 char
that is initialized to contain the space character and the null character. When used as the argument of a function call, the array is automatically converted to a pointer to its first element. Hence putchar(" ")
attempts to pass this pointer to putchar
, but putchar
requires an int
argument.
Upvotes: 1
Reputation: 13984
As AlexP already mentioned in his comment,you need to use a simple quoting like this:
char ch = ' ';
putch(ch)
Upvotes: 0