I recently wrote a program to determine my current working directory using getcwd(). getcwd() returns the starting address of the character array supplied by the programmer. I have written a very simple program and as I am not an experienced C program can anyone mention what I can do to make this program more efficient? I have defined an array which contains a random number of characters and I pass the base address of this to the function getcwd(). I recieve the base address of the modified array and proceed to print it by incrementing the char pointer which will then point to the next char. My problem is that I want to print only that string which is meaningful and not the garbage values. How do I do that??
#include<stdio.h>
#include <unistd.h>
int main(){
char buffer[250];
char *buf=getcwd(buffer,250);
if(buf==NULL)
printf("Error!!");
else
{
int i;
for(i=0;i<250;i++)
{
printf("%c",*buf);
buf++;
}
}
return 0;
}