(a) The C programming language distinguishes character constants from string con
ID: 3566859 • Letter: #
Question
(a) The C programming language distinguishes character constants from string constants by using single quotation marks for characters and double quotation marks for strings. Thus, 'c' is the character c, while "c" is a string of length 1 consisting of the single character c. Why do you think this distinction is made? Is it useful?
(b) Python, on the other hand, uses double quotation marks for both characters and strings (thus, "c" is either a character or string, depending on context). Discuss the advantages and disadvantages of these two approaches.
Explanation / Answer
1)
A character constant like '!' represents a single character. A string literal between double quotes usually represents multiple characters. A string literal like "!" seems to represent a single character, but it actually contains two: the ! you requested, and the which terminates all strings in C.Strings are arrays
Characters in C are represented by small integers corresponding to their character set values. Strings are represented by arrays of characters; you usually manipulate a pointer to the first character of the array. It is never correct to use one when the other is expected. To append a ! to a string, use
In C, characters are represented by small integers corresponding to their values in the machine's character set. Therefore, you don't need a conversion function: if you have the character, you have its value. The following fragment:
prints
on an ASCII machine.
So it is useful for having single and double quotes in C
In python both represent the same .for more info https://docs.python.org/2.0/ref/strings.html
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.