• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Surprising C errors

Contents

  • Surprising C errors
    • Empty printf
    • Macros
    • Single and Double quotes
    • Pointers
    • Loops
    • Null terminator of Strings
    • Further reading

Those errors might be surprising and a good exercise for C beginners:

Empty printf

#include <stdio.h>

int main()
{
    printf("");
    return 0;
}
error: zero-length gnu_printf format string

Macros

#include <stdio.h>
#define MY_MACRO printf("Hello World\n");

int main()
{
    if (1)
        MY_MACRO;
    else
        printf("Crazy, huh?\n");

    return 0;
}
macro.c: In function &lsquo;main&rsquo;:
macro.c:8: error: &lsquo;else&rsquo; without a previous &lsquo;if&rsquo;

Single and Double quotes

#include <stdio.h>

int main()
{
    printf('hello, world\n');
    return 0;
}
macro.c:5:9: warning: character constant too long for its type
macro.c: In function &lsquo;main&rsquo;:
macro.c:5: warning: passing argument 1 of &lsquo;printf&rsquo; makes pointer from integer without a cast
/usr/include/stdio.h:339: note: expected &lsquo;const char * __restrict__&rsquo; but argument is of type &lsquo;int&rsquo;
macro.c:5: warning: format not a string literal and no format arguments

Thanks to drpaulcarter.com for this example:

int main()
{
    const char * myPointer = 'A';
    (void) myPointer;
    return 0;
}
macro.c: In function &lsquo;main&rsquo;:
macro.c:3: warning: initialization makes pointer from integer without a cast

Pointers

#include <string.h>

int main()
{
    char * myPointer;

    strcpy(myPointer, "Hello World!");
    return 0;
}
macro.c: In function &lsquo;main&rsquo;:
macro.c:7: warning: &lsquo;myPointer&rsquo; is used uninitialized in this function

Loops

int main()
{
    int x = 42;
    while( x > 0 );
        x--;
    return 0;
}

No compiler error, but an infinite loop.

Null terminator of Strings

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char myArray[20];
    printf("Characters: %i\n", strlen(myArray));
    strcpy(myArray, "abc");
    printf("Characters: %i\n", strlen(myArray));
    strcpy(myArray, "Hello World!");
    printf("Characters: %i\n", strlen(myArray));
    printf("String: -%s-\n", myArray);
    printf("Size: %i Byte\n\n", sizeof(myArray));

    char * myString = malloc(strlen(myArray));
    strcpy(myString, myArray);
    printf("String: -%s-\n", myString);
    printf("Size: %i Byte\n", sizeof(myString));
    printf("Characters: %i\n\n", strlen(myString));

    char * breakIt = malloc(strlen(myString));
    strcpy(breakIt, myString);
    printf("String: -%s-\n", breakIt);
    printf("Size: %i Byte\n", sizeof(breakIt));
    printf("Characters: %i\n", strlen(breakIt));

    return 0;
}

Again, you don't get a compiler error, but some strange results:

Characters: 0
Characters: 3
Characters: 12
String: -Hello World!-
Size: 20 Byte

String: -Hello World!-
Size: 4 Byte
Characters: 12

String: -Hello World!-
Size: 4 Byte
Characters: 13

Further reading

  • C Preprocessor and macros
  • Pointer initialisation
  • strcpy, strlen

Published

Dez 28, 2011
by Martin Thoma

Category

Code

Tags

  • C 23
  • learning 15
  • Programming 52

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor