Weird chars when printing

Posted on 2006-06-06
Last Modified: 2010-04-15
Hi folks,

I got troubles when using printf. I got some weird chars.

I have a struct

struct permission {
      char dl;
      char owner_read;
      char owner_write;
      char owner_exe;
      char grp_read;
      char grp_write;
      char grp_exe;
      char other_read;
      char other_write;
      char other_exe;

   struct permission modes;


   modes.dl=            (S_ISDIR(filebuff.st_mode))      ? 'd' :'-';
   modes.owner_read=      (filebuff.st_mode & S_IRUSR)==S_IRUSR ? 'r':'-';
   modes.owner_write=      (filebuff.st_mode & S_IWUSR)==S_IWUSR ? 'w':'-';
   modes.owner_exe=      (filebuff.st_mode & S_IXUSR)==S_IXUSR ? 'x':'-';
   modes.grp_read=      (filebuff.st_mode & S_IRGRP)==S_IXGRP ? 'r':'-';
   modes.grp_write=      (filebuff.st_mode & S_IWGRP)==S_IXGRP ? 'w':'-';
   modes.grp_exe=      (filebuff.st_mode & S_IXGRP)==S_IXGRP ? 'x':'-';
   modes.other_read=      (filebuff.st_mode & S_IROTH)==S_IROTH ? 'r':'-';
   modes.other_write=      (filebuff.st_mode & S_IWOTH)==S_IWOTH ? 'x':'-';
   modes.other_exe=       (filebuff.st_mode & S_IXOTH)==S_IXOTH ? 'x':'-';


   printf("%5u %10s %d %3u %8ld %s\n",

(ps: struct dirent *entry)

But I got something like:

9830207 -rw-------Š@ 37796   1      100 Makefile

There is a weird char after the (file's) mode



Question by:tiger0516
    LVL 45

    Expert Comment

    Hi tiger0516,

    There's probably some junk in memory right after the string that you formatted.

    Either make it one byte longer and insert a zero into it or change the print format to print exactly 9 characters.

       printf("%5u %10s %d %3u %8ld %9.9s\n",

    Good Luck!
    LVL 84

    Accepted Solution

    There is no guarantee that a struct can be printed with %s
    It looks like you got lucky and your c implementaion happened to allign the characters in a way that worked for the first 10 characters,
    but you still have to limit the print to only 10 characters with
    LVL 45

    Expert Comment

    Hi ozo,

    Seems a little odd the the default behavior is #pragma pack, huh?

    LVL 84

    Expert Comment

    #pragma is implementation defined.
    there is no guarantee that it will work in a differerent implementation
    But since it seems to be packed in this implementation,
    printf("%5u %.10s %d %3u %8ld %s\n"
    should work unportably to deal with the symptom described

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now