ok im newbie...

i have a question

if i have

#include <stdio.h>
 
int main() 
{
printf("type something:");
char a;
scanf("%c", &a);
printf("%c\n" ,a );
return 0;
}

i will aways get one character in output.My question is:
-how to get world "hello" as standart output from variable using printf?

You are getting one character as output since you using a container or a variable which can only store a single character (i.e. the char datatype)

Use a character array to take in user input and output it using

printf ("My string is %s " , my_char_array ) ;

Something like:

int main (void)
{
    char my_string [BUFSIZ] = {'\0'} ;
    printf ("Enter string: " ) ;
    fgets (my_string, BUFSIZ, stdin) ;
    printf ("\n%s", my_string ) ;

    return 0 ;
}

Hope it helped, bye.

why that doesnt work?

#include <stdio.h>

int main ()
{
    char lala ;
    printf ("Enter string:");
    scanf("%s", &lala);
    printf ("%s\n", lala );
    return 0;
}

why that doesnt work?


#include <stdio.h>

int main ()
{
char lala ;
printf ("Enter string:");
scanf("%s", &lala);
printf ("%s\n", lala );
return 0;
}

1. please use code tags as shown in ~S.O.S~'s example.

2. you did not declare variable lala correctly -- study ~S.O.S~'s example and pay close attention to how he declared the character string.

3. NEVER usr scanf() -- it is harmful to your program's health because it will let you type in a string that is larger than the buffer you created for it, such as in your example. Use fgets() instead -- again see ~S.O.S~'s example.

Please enclose your code in code tags before posting it, this is the second time i have edited your post to add code tags.

And as far as the code you posted is concerned, it doest work coz the dataype "char" is meant to store only a single character so whatever amount of text you try to put into it will store the first charcter only since it has the capacity of storing only one character.

This is the same as saying you cant store a real number in the integer datatype.

int result = 3 / 2;

Does result when printed out give you 1.5 ?
No because int only capable of handling integers without any fractional component in them.

If you are in need of the kind of functionality you require you can try out one of the many scripting languages out there. Interpreted, hassle free, lot of inbuilt functions, typelesss and ease of coding.

Some of my favourites:
1. Perl
2. Python
3. Lua

Hope it helped, bye.

[EDIT] Mr. Dragon beat me to it :D [/EDIT]

ok one more question

why

[BUFSIZ] = {'\0'}

why that doesnt work?

'cause you still need to reserve space for the string you want to read. Sayin' &lala doesn't change the fact that lala is still a single char. It's also a bad thing because scanf assumes you reserved enough space for a string even though you didn't. A string is an array of char where the last character is '\0'.

#include <stdio.h>

int main ()
{
    char lala[10];
    printf ("Enter string:");
    scanf("%9s", lala);
    printf ("%s\n", lala );
    return 0;
}

Ancient Dragon mentioned this, but scanf is tricky with strings. You have to be careful not to read too much or it'll walk all over memory beyond the end of the array. That's where the 9 in %9s comes from. It tells scanf to read a string, up to 9 characters, and pop a '\0' in after the last character. Nice and safe, but fgets is easier to use. :)

The only problem is that scanf and fgets do different things with strings. scanf defaults to reading a string until it sees whitespace and fgets reads until it sees '\n'. In other words, fgets reads a line and scanf reads a word, by default.

ok one more question

why

[BUFSIZ] = {'\0'}

Initialization along with declaration.

This initializes all the elements of the char array which we are going to use as a C Style string to the null terminator which actually marks the end of C Style string. Safe and simple.

yes that worked great
why

[10]

that makes no sense for me (newbie)

The string can hold 9 characters, but there's also room for a null character at the end. Thusly, the array size is 10.

yes that worked great
why
Code:
[10]

that makes no sense for me (newbie)

Because its the null terminator in the end which differentiates between normal character arrays and C Style Strings. Just remember to think of '\0' whenever you think of strings in C and life would be easier for you:idea:

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.