user16111: I first used the Arduino IDE but now I'm using PlatformIO which is awesome. But I have some big trouble with Serial.print() and Serial.println(). To show this error I have the following example code:
#include <avr/io.h>
#include <util/delay.h>
#include "Arduino.h"
#include "main.h"

int main(int argc, char const *argv[]) {
  Serial.begin(9600);

  section5ex34();

  return 0;
}

void section5ex34() {
  // Initialization
  DDRC = 0b00000100; // Port C2 as output and the rest (including Port C3) as input. (Port C2 is used for a led that indicates when en bit is being read)

  uint8_t b = 0b00000000;

  // here the code reads bits from PORTC3
  for (int i = 0; i < 8; i++) {
    _delay_ms(1000);
    PORTC |= _BV(PORTC2); // Make Port C2 high (indicates a bit will be read)

    if ((PINC & (0b1 << 3)) != 0) {
      b |= (1 << i);
    }

    _delay_ms(100); // Wait for the user to prepare the next bit (because this is now done by hand)
    PORTC &= ~_BV(PORTC2); // Then make Port C2 low
  }

  Serial.println(b, HEX);
}

When I have the code like this, it prints the right Hexadecimal value corresponding with the 8 read bits.
When I add Serial.println("test"); after the print, it doesn't print that.
Or when I change Serial.println(b, HEX); to Serial.println(b, BIN); it prints 11.
When I replace those 2 prints with Serial.println("Hi test test."); it prints Hi, so it seems like it can only print 2 characters.
When I add Serial.println("Hello world . hello . hi"); four times after Serial.begin(9600);, it prints Hello world . h and nothing more. So here it seems like it can print more than 2 characters, but not all. If I wait a few seconds it adds ello to this, so it says Hello world . hello.
In the initial code, when I replace the if-statement with
if ((PINC & (0b1 << 3)) != 0) {
  b |= (1 << i);
  Serial.println("1");
} else {
  Serial.println("0");
}

and when the input is connected to 5v (so high) it prints a 1 once and nothing else, not even a hexadecimal value when the reading is finished.
This problem drives me crazy.
I have an Arduino Uno and use a baudrate of 9600 , 8,N,1.
I hope someone can help me out with this.

user23841: Arduino main() is not so simple as you think. If you use standard Arduino loop() and setup() functions it might be working much better.
But if you're not allowed to use Arduino libraries, you can use this:
src/main.cpp
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>

void section5ex34();
int uart_putchar(char c, FILE *stream);
void uart_init (unsigned long int baud);

int main(void) {
  uart_init(9600U);

  while(1) section5ex34();
  return 0;
}

void section5ex34() {
  // Initialization
  DDRC = 0b00000100; // Port C2 as output and the rest (including Port C3) as input. (Port C2 is used for a led that indicates when en bit is being read)
  PORTC = 0b11111011; 

  uint8_t b = 0b00000000;

  // here the code reads bits from PORTC3
  for (int i = 0; i < 8; i++) {
    _delay_ms(100);
    PORTC |= _BV(PORTC2); // Make Port C2 high (indicates a bit will be read)

    if ((PINC & (0b1 << 3)) != 0) {
      b |= (1 << i);
    }

    _delay_ms(100); // Wait for the user to prepare the next bit (because this is now done by hand)
    PORTC &= ~_BV(PORTC2); // Then make Port C2 low
  }
  printf("0x%02X and something loooooooooooong\n",b);
}

int uart_putchar(char c, FILE *stream) {
  if (c == '\n') uart_putchar('\r', stream);
  loop_until_bit_is_set(UCSR0A, UDRE0);
  UDR0 = c;
  return 0;
}

// function to initialize UART
void uart_init (unsigned long int baud) {
    unsigned int baudrate = F_CPU/(baud*16UL)-1;
    UBRR0H = (baudrate>>8);             // shift the register right by 8 bits
    UBRR0L = baudrate;                  // set baud rate
    UCSR0B|= (1<<TXEN0)|(1<<RXEN0);     // enable receiver and transmitter
    UCSR0C|= (1<<UCSZ00)|(1<<UCSZ01);   // 8bit data format

    //// with static FILE variable:
    // static FILE uart0fd;
    // fdev_setup_stream((&uart0fd), uart_putchar, NULL,  _FDEV_SETUP_WRITE);
    // stdout = &uart0fd;

    //// or directly by using dynamically allocated FILE struct:
    stdout = fdevopen(uart_putchar,NULL); // uses malloc/calloc, it might be inconvenient
}


user7508: You have two issues here:
The first is that you are using the Arduino core library and failed to
initialize it. This is done automatically if you rely on the core's
supplied main(), i.e. you stick with the setup()/loop() style of
coding. If you write your own main(), you should start by calling
init() to initialize the core.
Edit: In this particular instance, you could get away by just enabling
interrupts with sei(). But I consider it good practice to initialize the
core if you are ever going to use it.
The second issue is the abrupt termination of main:
Serial.print() and println() do not really print to the serial port.
Instead, they put the characters to be printed in the output buffer, and
let an interrupt take care of actually sending them through the UART.
Your program ends with
return 0;

This will stop it. It will stop the whole program, including
interrupt-driven tasks. So you will see whatever characters did make it
to the UART, and nothing more.
If you instead end the program with and endless loop
for (;;) ;

it should work as expected.

16111: <p>I first used the Arduino IDE but now I'm using PlatformIO which is awesome. But I have some big trouble with <code>Serial.print()</code> and <code>Serial.println()</code>. To show this error I have the following example code:</p>

<pre><code>#include &lt;avr/io.h&gt;
#include &lt;util/delay.h&gt;
#include "Arduino.h"
#include "main.h"

int main(int argc, char const *argv[]) {
  Serial.begin(9600);

  section5ex34();

  return 0;
}

void section5ex34() {
  // Initialization
  DDRC = 0b00000100; // Port C2 as output and the rest (including Port C3) as input. (Port C2 is used for a led that indicates when en bit is being read)

  uint8_t b = 0b00000000;

  // here the code reads bits from PORTC3
  for (int i = 0; i &lt; 8; i++) {
    _delay_ms(1000);
    PORTC |= _BV(PORTC2); // Make Port C2 high (indicates a bit will be read)

    if ((PINC &amp; (0b1 &lt;&lt; 3)) != 0) {
      b |= (1 &lt;&lt; i);
    }

    _delay_ms(100); // Wait for the user to prepare the next bit (because this is now done by hand)
    PORTC &amp;= ~_BV(PORTC2); // Then make Port C2 low
  }

  Serial.println(b, HEX);
}
</code></pre>

<p>When I have the code like this, it prints the right Hexadecimal value corresponding with the 8 read bits.</p>

<p>When I add <code>Serial.println("test");</code> after the print, it doesn't print that.</p>

<p>Or when I change <code>Serial.println(b, HEX);</code> to <code>Serial.println(b, BIN);</code> it prints <code>11</code>.</p>

<p>When I replace those 2 prints with <code>Serial.println("Hi test test.");</code> it prints <code>Hi</code>, so it seems like it can only print 2 characters.</p>

<p>When I add <code>Serial.println("Hello world . hello . hi");</code> <em>four times</em> after <code>Serial.begin(9600);</code>, it prints <code>Hello world . h</code> and nothing more. So here it seems like it can print <em>more</em> than 2 characters, but not all. If I wait a few seconds it adds <code>ello</code> to this, so it says <code>Hello world . hello</code>.</p>

<p>In the initial code, when I replace the <code>if</code>-statement with</p>

<pre><code>if ((PINC &amp; (0b1 &lt;&lt; 3)) != 0) {
  b |= (1 &lt;&lt; i);
  Serial.println("1");
} else {
  Serial.println("0");
}
</code></pre>

<p>and when the input is connected to 5v (so high) it prints a <code>1</code> once and nothing else, not even a hexadecimal value when the reading is finished.</p>

<p>This problem drives me crazy.</p>

<p>I have an Arduino Uno and use a baudrate of 9600 , 8,N,1.</p>

<p>I hope someone can help me out with this.</p>
 23841: <p>Arduino <code>main()</code> is not so simple as you think. If you use standard Arduino <code>loop()</code> and <code>setup()</code> functions it might be working much better.</p>

<p>But if you're not allowed to use Arduino libraries, you can use this:</p>

<p><strong>src/main.cpp</strong></p>

<pre><code>#include &lt;avr/io.h&gt;
#include &lt;util/delay.h&gt;
#include &lt;stdio.h&gt;

void section5ex34();
int uart_putchar(char c, FILE *stream);
void uart_init (unsigned long int baud);

int main(void) {
  uart_init(9600U);

  while(1) section5ex34();
  return 0;
}

void section5ex34() {
  // Initialization
  DDRC = 0b00000100; // Port C2 as output and the rest (including Port C3) as input. (Port C2 is used for a led that indicates when en bit is being read)
  PORTC = 0b11111011; 

  uint8_t b = 0b00000000;

  // here the code reads bits from PORTC3
  for (int i = 0; i &lt; 8; i++) {
    _delay_ms(100);
    PORTC |= _BV(PORTC2); // Make Port C2 high (indicates a bit will be read)

    if ((PINC &amp; (0b1 &lt;&lt; 3)) != 0) {
      b |= (1 &lt;&lt; i);
    }

    _delay_ms(100); // Wait for the user to prepare the next bit (because this is now done by hand)
    PORTC &amp;= ~_BV(PORTC2); // Then make Port C2 low
  }
  printf("0x%02X and something loooooooooooong\n",b);
}

int uart_putchar(char c, FILE *stream) {
  if (c == '\n') uart_putchar('\r', stream);
  loop_until_bit_is_set(UCSR0A, UDRE0);
  UDR0 = c;
  return 0;
}

// function to initialize UART
void uart_init (unsigned long int baud) {
    unsigned int baudrate = F_CPU/(baud*16UL)-1;
    UBRR0H = (baudrate&gt;&gt;8);             // shift the register right by 8 bits
    UBRR0L = baudrate;                  // set baud rate
    UCSR0B|= (1&lt;&lt;TXEN0)|(1&lt;&lt;RXEN0);     // enable receiver and transmitter
    UCSR0C|= (1&lt;&lt;UCSZ00)|(1&lt;&lt;UCSZ01);   // 8bit data format

    //// with static FILE variable:
    // static FILE uart0fd;
    // fdev_setup_stream((&amp;uart0fd), uart_putchar, NULL,  _FDEV_SETUP_WRITE);
    // stdout = &amp;uart0fd;

    //// or directly by using dynamically allocated FILE struct:
    stdout = fdevopen(uart_putchar,NULL); // uses malloc/calloc, it might be inconvenient
}
</code></pre>
 7508: <p>You have two issues here:</p>

<p>The first is that you are using the Arduino core library and failed to
initialize it. This is done automatically if you rely on the core's
supplied <code>main()</code>, i.e. you stick with the <code>setup()</code>/<code>loop()</code> style of
coding. If you write your own <code>main()</code>, you should start by calling
<code>init()</code> to initialize the core.</p>

<p><strong>Edit</strong>: In this particular instance, you could get away by just enabling
interrupts with <code>sei()</code>. But I consider it good practice to initialize the
core if you are ever going to use it.</p>

<p>The second issue is the abrupt termination of main:
<code>Serial.print()</code> and <code>println()</code> do not really print to the serial port.
Instead, they put the characters to be printed in the output buffer, and
let an interrupt take care of actually sending them through the UART.</p>

<p>Your program ends with</p>

<pre><code>return 0;
</code></pre>

<p>This will stop it. It will stop the whole program, including
interrupt-driven tasks. So you will see whatever characters did make it
to the UART, and nothing more.</p>

<p>If you instead end the program with and endless loop</p>

<pre><code>for (;;) ;
</code></pre>

<p>it should work as expected.</p>
