Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   printing first char (http://www.velocityreviews.com/forums/t962015-printing-first-char.html)

Bill Cunningham 06-22-2013 11:09 AM

printing first char
 
I have tried several things and always get a seg fault. What I want to
do is just print out the first char of an array. Here's my latest attempt.
Maybe someone knows what I'm trying to do.

#include <stdio.h>

int main()
{
char a[] = "+hello world\n";
printf("%s\n", a[0]);
return 0;
}



Lanarcam 06-22-2013 11:20 AM

Re: printing first char
 
Le 22/06/2013 13:09, Bill Cunningham a écrit :
> I have tried several things and always get a seg fault. What I want to
> do is just print out the first char of an array. Here's my latest attempt.
> Maybe someone knows what I'm trying to do.
>
> #include <stdio.h>
>
> int main()
> {
> char a[] = "+hello world\n";
> printf("%s\n", a[0]);
> return 0;
> }
>
>

You should write

printf("%c\n", a[0]);

You want to print a char (%c) not a string (%s).


Bill Cunningham 06-22-2013 11:48 AM

Re: printing first char
 
Lanarcam wrote:

> You should write
>
> printf("%c\n", a[0]);
>
> You want to print a char (%c) not a string (%s).


Oh ok. Sounds simple enough. Thanks



Jorgen Grahn 06-22-2013 12:51 PM

Re: printing first char
 
On Sat, 2013-06-22, Bill Cunningham wrote:
> I have tried several things and always get a seg fault. What I want to
> do is just print out the first char of an array. Here's my latest attempt.
> Maybe someone knows what I'm trying to do.


It's better if you tell us, unless it's a riddle.

> #include <stdio.h>
>
> int main()
> {
> char a[] = "+hello world\n";
> printf("%s\n", a[0]);
> return 0;
> }


You should ask your compiler to report more warnings. Mine tells me:

foo.c:6:5: warning: format '%s' expects argument of type 'char *', but
argument 2 has type 'int'

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

glen herrmannsfeldt 06-22-2013 03:49 PM

Re: printing first char
 
Lanarcam <lanarcam1@yahoo.fr> wrote:

(sniop)

> You should write


> printf("%c\n", a[0]);


> You want to print a char (%c) not a string (%s).


Or if you really want %s, then:

printf("%.1s\n",a);

To print the nth character of a, (where n starts at 0)

printf("%.1s\n",a+n);

-- glen

Malcolm McLean 06-22-2013 09:27 PM

Re: printing first char
 
On Saturday, June 22, 2013 1:51:31 PM UTC+1, Jorgen Grahn wrote:
> On Sat, 2013-06-22, Bill Cunningham wrote:
>
>
> foo.c:6:5: warning: format '%s' expects argument of type 'char *', but
>
> argument 2 has type 'int'
>

What a clever compiler. Mine tells me that printf() is unsafe and tries
to force me to use the wide version, which has no support for floating
point.

Ian Collins 06-22-2013 11:07 PM

Re: printing first char
 
Malcolm McLean wrote:
> On Saturday, June 22, 2013 1:51:31 PM UTC+1, Jorgen Grahn wrote:
>> On Sat, 2013-06-22, Bill Cunningham wrote:
>>
>>
>> foo.c:6:5: warning: format '%s' expects argument of type 'char *', but
>>
>> argument 2 has type 'int'
>>

> What a clever compiler. Mine tells me that printf() is unsafe and tries
> to force me to use the wide version, which has no support for floating
> point.


Always have more than one tool in your box!

One of the good features of code derived from OpenSolaris is it has to
compile cleanly with Sun's compiler+lint and gcc.

--
Ian Collins

Xavier Roche 06-23-2013 08:36 PM

Re: printing first char
 
You might consider minor changes in your code,

Le 23/06/2013 22:23, paskali a écrit :
> char *str = "+string+\n";


const char* str = "+string+\n";
(the string is not writable, so this is safer to tell the compiler)

> int idx = 0;


size_t idx = 0;
(the canonical integer to be used as offset in memory is size_t)

> do {


for(idx = 0 ; str[idx] != '\0' ; idx++) {
(a for-loop is safer is the string is empty, and comparing a character
with the terminating character is better than comparing it with the NULL
pointer)

ie.:
int main(void) {
const char *str = "+string+\n";
size_t idx;

for(idx = 0 ; str[idx] != '\0' ; idx++) {
printf("%c", str[idx]);
}
....
}


Martin Ambuhl 06-23-2013 11:26 PM

Re: printing first char
 
On 6/22/2013 7:09 AM, Bill Cunningham wrote:
> I have tried several things and always get a seg fault. What I want to
> do is just print out the first char of an array. Here's my latest attempt.
> Maybe someone knows what I'm trying to do.


Use the correct printf specifier:
>
> #include <stdio.h>
>
> int main()
> {
> char a[] = "+hello world\n";
> printf("%s\n", a[0]);

^
c
> return 0;
> }
>
>



Barry Schwarz 06-24-2013 01:09 AM

Re: printing first char
 
On Sun, 23 Jun 2013 20:23:55 GMT, "paskali" <2970invalid@webuse.net>
wrote:

>Bill Cunningham <nospam@nspam.invalid> wrote:
>
>> I have tried several things and always get a seg fault. What I want to
>> do is just print out the first char of an array. Here's my latest attempt.
>> Maybe someone knows what I'm trying to do.
>>
>> #include <stdio.h>
>>
>> int main()
>> {
>> char a[] = "+hello world\n";
>> printf("%s\n", a[0]);
>> return 0;
>> }
>>

>
>As well as some one more expert has written you have to replace
>printf("%s\n", a[0]) with printf("%c\n", a[0]).
>
>However, some time i have found more confortable to print the entire
>string as following:


The original post did not ask about printing anything other than a
single character.

>#include <stdio.h>
>
>int main(void) {
>
> char *str = "+string+\n";
> int idx = 0;
>
> do {
> printf("%c", str[idx++]);
> } while(str[idx] != NULL);


This will fail on those systems which define NULL as (void*)0.

Why would you want to call printf multiple times? What is the benefit
since you assume str is a properly terminated string?

>}
>
>May be some one dislike that but for me is good.


--
Remove del for email


All times are GMT. The time now is 10:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.