Strange character added when reading to buffer with length of 12

Hi all,
I got trouble with reading and writing ( to standard input/output, file, socket whatever...).
I will briefly describe it by giving this example. I want to read a long string from keyboard but i don't know how long it is b4. So i need to know the number of character i will read first. Then i initialize my buffer to fit my desirable string and read this string from standard input.
However, everything is ok until i try to read 12 characters from keyboard. A weird character is automatically added : "�". And when i check the string length, it is 15. I don't know why because with other cases, there isn't some things like this happened.
Just a sample code:

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
main(){
int fd;
char str;
int a;
char enter[1];
scanf("%d",&a);
str = malloc(a
sizeof(char));
read(0,str,a);
printf("%s ----- %d",str,strlen(str));
}

I have searched but still found nothing related to this.

Thanks

First off check how big an int can be on your system...doubt if it can hold a 12 digit number unless it's a long long type. Even if that's alright you are most likely hitting the data segment limit for malloc'ing such a huge chunk of memory. Check its return value before moving onto the next step.

long long a;
char *str;
a = 123456789012; / a is a 12 digit number */
if ((str = malloc(a * sizeof(char))) == NULL) { /* here you are trying to allocate a huge chunk of memory */
    perror("malloc error");
    exit(errno);
}

Hi,
My example is:

int a = 12;
char str;
str = malloc(2
sizeof(char));
read(0,str,a);
printf("%s %d",str,strlen(str));

if you run the program and type: "0123456789012" means 12 characters or whatever. What you see in the screen after printf function is: 0123456789012� 15
it does not happen with other value of a but only 12.

To allocate memory for a string of 12 chars you have to allocate memory for 12 chars + 1 for the '\0' char to terminate the string.

Regards

You are either allocating too much or too little. When you need memory for "123456789012" you need to malloc memory for 13 (12 char + 1 NULL) characters otherwise you see junk concatenated in the output. Here you know there will be 12 characters so why not do...

a = 12;
str = malloc(a*sizeof(char)+1);

Thanks for all,
What i need is "why".

[U16511@maildev c_cpp]$ ./a.out
0123456789015
0012345678901� 15[U16511@maildev c_cpp]$ 5

It means nothing related to a string ending 12.

It is a memory managment issue.

Your question is "why'. I will try to answer.

In the input string there are 13 characters, but str is alloted for 12 characters. So where will keep the last ASCII character reading into ? Where will keep the charcter for string end '\0'.

So if you alllocate a location for 12, maximum you can read 11 characters. Since the 12 th charcter will be used by the '\0'.

Then next question why the wiered character is coming out through printf ? I think printf is trying to print a nonprintable charcter while searching for '\0' ; since in its format string, the first one is "%s". It means printf should print a string. By definition string should have a charcter '\0' at end . If it is not there we cannot assure what it wil print. The decision is not able to decide even by using Heisenberg's Uncertainity principle. So believe in Gods grace!!