-4

Using printf and without using any looping or branching?

qwe2004
  • 21
  • 1

4 Answers4

2

Recursion is your friend:

int print_char_by_char(char * p)
{
  *p && print_char_by_char(p+1);
  printf("%c", *p);
  return 1;
}

int main(void)
{
  print_char_by_char("alk");
  return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
1

Extending the answer of @alk, you can use recursion and pointers to function in order to avoid the use of logical operators:

#include <stdio.h>

void dummy(char *p);
void print(char *p);

void (*fp[])(char *) = {dummy, print};

void dummy(char *p)
{
    (void)p;
}

/**
 *  Read *p as bool (0 or 1) using bitwise operators
 *  Call print() recursively if 1
 *  Call dummy() if 0 and exits from function
 */
void print(char *p)
{
    char i = *p;

    i |= i >> 4;
    i |= i >> 2;
    i |= i >> 1;
    printf("%c", *p);
    fp[i & 1](p + 1);
}

int main(void)
{
    print("Hello world\n");
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • C doesn't have a native type bool that will only be zero or 1. Hence i will be zero or more-than-zero and cause an error when not 1 in the selection of the function. It will work with an array of 255 entries: entry zero has dummy, all others have print_char_by_char. (You can make this array global.) – Paul Ogilvie Mar 03 '15 at 12:09
  • 1
    that means this only works in C99, not in older chainsaw C versions (where the programmer must do everything himself) and in those older versions with macros, the macro will do a (x?1:0) which is a branch and so violates the assignment (and the built-in C99 type bool makes the compiler generate this branch, hence, also violation of the assignment). – Paul Ogilvie Mar 03 '15 at 13:34
  • Or just replace `bool i = *p;` with `int i = !!*p`. – alk Mar 03 '15 at 16:17
  • @PaulOgilvie, I didn’t know that bool was implemented in this way, thanks for pointing that – David Ranieri Mar 03 '15 at 17:04
  • @alk, I think `!` is also a violation of the assignment, it is possible to obtain a boolean `0` or `1` using bitwise operators? – David Ranieri Mar 03 '15 at 17:08
0

Well, your question is

How do you print a string in C character by character?

Using printf and without using any looping or branching?

The simplest answer for that is

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

This is the answer to your question. If this is not what you wanted, then Specify your exact question giving more details.

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • This is not character-by-character. It prints a string in one go. – Paul Ogilvie Mar 04 '15 at 09:08
  • @PaulOgilvie , then what do you mean by "character by character" – Arun A S Mar 04 '15 at 09:11
  • @arun-a-s, this means the pogrammer programs to print one character, then for example increments the string pointer, then programs to print another character, etc. For example using printf("%c",*p); See other examples. – Paul Ogilvie Mar 04 '15 at 09:24
  • @PaulOgilvie , well I understand that, but I believe this answer is more than enough for a question with such less details. If the OP had specified what purpose it is for or gave an example of what should be done, then I would not have posted this. But for the current question, I believe this answer does that ( I mean, doing it like this will have the same effect as the other answers unless there are certain conditions to be met, which clearly have not been specified ). – Arun A S Mar 04 '15 at 10:01
0

@qwe2004, tell your professor that the Stack Overflow community agrees that it can't be done. If he says it can be done, we are all interested in seeing his clever and cunning way of doing it, and learn. (Tell him also we will be critical of hidden branching and logical tests.)

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41