Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print part of char array

Tags:

c

So:

char *someCArray; # something

I want to print out "ethin" from "something".

like image 511
user461316 Avatar asked Mar 04 '11 02:03

user461316


People also ask

How do I print part of an array?

You can access an array element using an expression which contains the name of the array followed by the index of the required element in square brackets. To print it simply pass this method to the println() method.

How do I print a character?

yes, %c will print a single char: printf("%c", 'h'); also, putchar / putc will work too.

What is a char * in C?

The abbreviation char is used as a reserved keyword in some programming languages, such as C, C++, C#, and Java. It is short for character, which is a data type that holds one character (letter, number, etc.) of data. For example, the value of a char variable could be any one-character value, such as 'A', '4', or '#'.


1 Answers

You can use the printf precision specifier

#include <stdio.h>

int main(void) {
  char s[] = "something";
  int start = 3;
  int len = 5;

  printf("%.*s\n", len, s + start);

  return 0;
}

Code also posted to codepad: http://codepad.org/q5rS81ox

like image 186
pmg Avatar answered Sep 19 '22 19:09

pmg