Questions tagged [char]

char refers to the character data-type, representing letters, digits, punctuation marks, control characters, etc. Use this tag for questions relating to and usage of the character data-type.

The keyword char is used in many programming languages, often defining a 1-byte memory location representing ASCII, UTF-8, UTF-16, UTF-32, and ISO-8895-1 character encodings. Arrays of characters can create Strings used to form words, descriptive text, or even extremely large numbers unable to be represented exactly by 32 or 64 bits.

13281 questions
63
votes
14 answers

Difference between "char" and "String" in Java

I am reading a book for Java that I am trying to learn, and I have a question. I can't understand what is the difference between the variable type char and String. For example, there is a difference between int and short, the bytes at the memory and…
Android Girl
  • 2,074
  • 3
  • 22
  • 29
62
votes
4 answers

Reasons to use (or not) stdint

I already know that stdint is used to when you need specific variable sizes for portability between platforms. I don't really have such an issue for now, but what are the cons and pros of using it besides the already shown fact above? Looking for…
Sassa
  • 1,673
  • 2
  • 16
  • 30
62
votes
5 answers

Is it possible to convert char[] to char* in C?

I'm doing an assignment where we have to read a series of strings from a file into an array. I have to call a cipher algorithm on the array (cipher transposes 2D arrays). So, at first I put all the information from the file into a 2D array, but I…
tparf
  • 657
  • 1
  • 6
  • 8
62
votes
7 answers

In Java how does one turn a String into a char or a char into a String?

Is there a way to turn a char into a String or a String with one letter into a char (like how you can turn an int into a double and a double into an int)? (please link to the relevant documentation if you can). How do I go about finding something…
David
  • 14,569
  • 34
  • 78
  • 107
62
votes
9 answers

What is the correct way to compare char ignoring case?

I'm wondering what the correct way to compare two characters ignoring case that will work for all cultures. Also, is Comparer.Default the best way to test two characters without ignoring case? Does this work for surrogate-pairs? EDIT: Added…
Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
60
votes
3 answers

Deprecated conversion from string literal to 'char*'

I have a program which declares an array of strings like this: char *colors[4] = {"red", "orange", "yellow", "blue"}; But I get the above compiler warning. It compiles but I'd rather use the non-deprecated way(if there is one). I've tried to find…
Matt
  • 715
  • 1
  • 5
  • 6
60
votes
11 answers

C job interview - casting and comparing

I was confronted with a tricky (IMO) question. I needed to compare two MAC addresses, in the most efficient manner. The only thought that crossed my mind in that moment was the trivial solution - a for loop, and comparing locations, and so I did,…
Itzik984
  • 15,968
  • 28
  • 69
  • 107
59
votes
6 answers

Why does the string Remove() method allow a char as a parameter?

Consider this code: var x = "tesx".Remove('x'); If I run this code, I get this exception: startIndex must be less than length of string. Why can I pass a char instead of an int to this method? Why don't I get a compilation error? Why does the…
user1968030
58
votes
3 answers

Difference between char and char[1]

In C++ what is the difference (if any) between using char and char[1]. examples: struct SomeStruct { char x; char y[1]; }; Do the same reasons follow for unsigned char?
Matthieu N.
57
votes
7 answers

Does C have a string type?

I have recently started programming in C, coming from Java and Python. Now, in my book I have noticed that to make a "Hello World" program, the syntax is something like this: char message[10] strcpy(message, "Hello, world!") printf("%s\n",…
rel-s
  • 6,108
  • 11
  • 38
  • 50
57
votes
6 answers

Getting an ASCII character code in Ruby using `?` (question mark) fails

I'm in a situation where I need the ASCII value of a character (for Project Euler question #22, if you want to get specific) and I'm running into an issue. Being new to ruby, I googled it, and found that ? was the way to go: ?A or whatever. But when…
dorr
  • 816
  • 1
  • 6
  • 11
56
votes
4 answers

Understanding the difference between null and '\u000' in Java

I read in a book (Thinking in Java by Bruce Eckel, 4th edition, page 47) that null is equal to '\u000'. And then I was wondering what exactly does '\u000' really mean. As per my understanding null was nothing or absence of anything. And '\u000'…
ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56
53
votes
0 answers

Preferred conversion from char (not char*) to std::string

I have a char, a plain old character, that I would like to turn into an std::string. std::string(char) doesn't exist of course. I could create a char array and copy it in, I could go through string streams, or many other little roundabout routes. …
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
53
votes
7 answers

In C - check if a char exists in a char array

I'm trying to check if a character belongs to a list/array of invalid characters. Coming from a Python background, I used to be able to just say : for c in string: if c in invalid_characters: #do stuff, etc How can I do this with…
Amarok
  • 900
  • 2
  • 7
  • 15
52
votes
2 answers

C char* to int conversion

How would I go about converting a two-digit number (type char*) to an int?
Niek
  • 1,000
  • 2
  • 9
  • 19