Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
11-06-2010, 07:58 PM
|
#1
|
|
Member
Registered: May 2008
Distribution: Slackware
Posts: 949
Rep:
|
Is there a way to print a range of a char array without a for loop?
Program in C
Say I have a char array of 1024 bytes called buf1.
But I only want to print the chars in index 0 up to index 30. I know I could do this with a for loop. But is there any other way? What about maybe storing from 31-1024 to another char array say buf2 with strcpy and somehow popping 31+ out of the buf1 char array?
What if I wanted to print chars in index 30 to 36? Any quick way without a for loop?
Last edited by trist007; 11-06-2010 at 08:04 PM.
|
|
|
|
11-06-2010, 08:13 PM
|
#2
|
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
Code:
#include <stdio.h>
int main()
{
char a[1024] = "abcdefghijklmnopqrstuvwxyz";
printf("Characters 0 through 2 of a[]: %.3s\n", a);
printf("Characters 10 through 15 of a[]: %.6s\n", &a[10]);
return 0;
}
Prints this:
Code:
Characters 0 through 2 of a[]: abc
Characters 10 through 15 of a[]: klmnop
The "%.Ns" notation in the printf string is "%s" with a precision specifier. It's buried in the geek-speak in the printf(3) man page 
|
|
|
1 members found this post helpful.
|
11-06-2010, 08:21 PM
|
#3
|
|
Member
Registered: May 2008
Distribution: Slackware
Posts: 949
Original Poster
Rep:
|
That's awesome, thank you.
|
|
|
|
11-07-2010, 09:15 AM
|
#4
|
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 806
Rep: 
|
To demonstrate that the fancy format string saves not only code complexity but actual time, run this shell script. You may wish to play with its first line, and possibly its second line.
I got this output.
Code:
running with compilation optimization O0
format string is %.25s
loop way is 8 seconds
format way is 4 seconds
running with compilation optimization O1
format string is %.25s
loop way is 7 seconds
format way is 5 seconds
running with compilation optimization O2
format string is %.25s
loop way is 8 seconds
format way is 4 seconds
running with compilation optimization O3
format string is %.25s
loop way is 7 seconds
format way is 4 seconds
Here is the script.
Code:
export iterations=10000000
export length=25
rm -f O0 O1 O2 O3; cat > 1.c <<EOD; \
gcc -O0 -Werror -Wall 1.c -o O0; \
gcc -O1 -Werror -Wall 1.c -o O1; \
gcc -O2 -Werror -Wall 1.c -o O2; \
gcc -O3 -Werror -Wall 1.c -o O3; \
./O0 O0; \
./O1 O1; \
./O2 O2; \
./O3 O3
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char **argv)
{
int jndex;
long iterations;
long length;
long single_iteration;
long ticks_per_second;
char *theoretical_string;
char format_string[80];
clock_t loop_way;
clock_t format_way;
struct tms the_beginning;
struct tms the_middle;
struct tms the_end;
FILE *phyle;
phyle=fopen("/dev/null","a");
if(phyle==NULL)
{
fprintf(stderr,"couldn\'t open /dev/null\n");
exit(1);
}
printf("running with compilation optimization %s\n",
argv[1]
);
iterations=strtol(getenv("iterations"),NULL,0);
length =strtol(getenv("length" ),NULL,0);
theoretical_string=malloc(length+2);
if(theoretical_string==NULL)
{
fprintf(stderr,"string is theoretically too long\n");
exit(1);
}
memset(theoretical_string,'A',length+1);
theoretical_string[length+1]=0;
sprintf(format_string,"%%.%lds",length);
printf("format string is %s\n",format_string);
loop_way =0;
format_way=0;
for(single_iteration=0;
single_iteration<iterations;
single_iteration++
)
{
times(&the_beginning);
for(jndex=1;jndex<length+1;jndex++)
{
fprintf(phyle,"%c",theoretical_string[jndex]);
}
times(&the_middle);
fprintf(phyle,format_string,theoretical_string+1);
times(&the_end);
loop_way+=(the_middle .tms_utime+the_middle .tms_stime)
-(the_beginning.tms_utime+the_beginning.tms_stime);
format_way+=(the_end .tms_utime+the_end .tms_stime)
-(the_middle.tms_utime+the_middle.tms_stime);
}
ticks_per_second=sysconf(_SC_CLK_TCK);
printf("loop way is %ld seconds\n", loop_way/ticks_per_second);
printf("format way is %ld seconds\n",format_way/ticks_per_second);
return 0;
} /* main() */
EOD
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:53 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|