-1

Task: Write two possibilities printf commands to print the following char array.

char str[] = "Im a sentence";

I found out one:

printf("%s", str) // this prints: Im a sentence

But i need another one, can anyone help me?

Panther
  • 15
  • 1
  • 6

3 Answers3

3

It's a silly question, but they are probably looking for this:

printf(str);       // 1
printf("%s", str); // 2

Note that (1) is not considered good practice, but it is valid and it works.

Paul R
  • 208,748
  • 37
  • 389
  • 560
3

The other one would be passing the string directly to printf:

printf(str);

As long as str is free of format specifiers, this is going to work. However, you should avoid using this in production code, because the compiler would be unable to properly diagnose potential mismatches between parameter types and format specifiers, which could lead to undefined behavior.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1
int x;
for (x=0;x<strlen(str);++x) 
    printf("%c",str[x]);
ForceBru
  • 43,482
  • 10
  • 63
  • 98