This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
printf("%c","abcdefgh"[4]);
The output of the following program is e. But I don't know how it is? I searched a lot in google and here but didn't find the correct explanation for this code. The explanation I am getting for this code:
"This is for getting one char only. i.e, the 4th char starts from 0. So the answer is 'e'".
String literals are arrays, and as such can be indexed into: "abcdefgh"[0] is 'a', for example.
you could write it like this:
char *tmp="abcdefgh";
printf("%c",tmp[4]);
printf("%c","abcdefgh"[4]);
The format string is "%c", which means that printf shall print one character. The character it shall print is
"abcdefgh"[4]
i.e. the character at index 4 of the char[9] "abcdefgh", which is the character 'e'.
In C, a string is represented as array so this statement
printf("%c","abcdefgh"[4]);
is equivalent to
char a[] = "abcdefgh";
printf("%c",a[4]);
The %c prints one character. So to explain why you get "e" as the output a has index 0, b has index 1, c has index 2, d has index 3, and finally e has index 4. Which is what you selected to print ([4]). Which is why "e" is the output.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Problem : Rewrite small numbers from input to output. Stop processing
input after reading in the number 42. All numbers at input are
integers of one or two digits.
Example
Input : 1 2 88 42 99
Output : 1 2 88
My solution :
#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
while(i!=42)
{
printf("%d",i);
scanf("%d",&i);
}
return 0;
}
Correct Solution :
#include <stdio.h>
int main( void ) {
int i;
while( 1 ) {
scanf( "%d", &i );
if( i == 42 ) break;
printf( "%d\n", i );
}
return 0;
}
Both the programs end if the input is 42, then what is the difference between the two?
EDIT : I just realized that this should have been posted at codereview.stackexchange.com Admins please do the needful.
Not too much. The only differences I see are that:
You don't output a newline "\n" after printing each number.
Your code isn't completely "DRY" (you repeat the line scanf("%d",&i);).
Otherwise they're (functionally) identical. But I feel compelled to add:
Your code is slightly uglier... Use proper indentation!
In the first program you enter the cycle after the user has entered the first value, so you have to check the value in the while condition.
The second program, instead, checks input always inside the cycle, and is therefore clearer.
From the question itself I can say that it is from one of the programming contest website practice questions.
These sites also match the format in which you are answering your question. So a newline(\n) is necessary.
They are functionally equivalent, and personally I much prefer your solution: while(1) is considered by some, including me, to be bad practice. It is easier to see when the while loop terminates just by looking at the single line, rather than having to trace through the code. A few differences otherwise:
You need to indent your code, or it is hard to read
You are not printing a newline (\n)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In a program I am writing I have declared this variable:
#define SIZE 5
long int clockNum [SIZE] = {98401, 526488, 765349, 34645, 127615};
If I would like to print this so that the output was:
098401
526488
765349
034645
127615
How Would I do this? I tried the statement below but it doesnt seem to work..
for(i = 0; i < SIZE; i++)
{
printf ("%li\n",&clockNum[i]);
}
return(0);
The output I get looks like:
-1076048964
-1076048960
-1076048956
-1076048952
-1076048948
remove the & from printf and let see
for(i = 0; i < SIZE; i++)
{
printf ("%li\n",clockNum[i]);
}
here in printf & give the address value of the variable
Remove & sign from line printf ("%li\n",&clockNum[i]); because it gives address of given value or varible not actual value.
Change your call to printf to something like: printf ("%li\n",clockNum[i]);.
As it is right now, you're trying to print the address instead of the value of the number (but since the type you're passing doesn't match the type you specified in the format string, result is undefined behavior).
If you really need to print the leading zero(s) so all the values come out as 6 digits, you also want to change your format string to something like %6.6li.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
How does the following code work?
void main()
{
printf("%d", printf("earth"));
}
This gives as output: earth5.
The return value of printf is the number of characters printed. The inner printf is called first. Equivalent to:
int rc = printf("earth");
printf("%d", rc);
This is absolutely fine :-)
The print("earth") outputs earth and return 5 (the number of characters printed).
The other printf gets the 5 as a parameter and outputs it as an integer (because of the %d)
%d is expecting an integer to print it. printf returns the number of printed chars, and you're printing a 5 char string.
It evaluates first the inner print to find out how many character were printed and then it evaluates the outer one printing 5.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I was asked this on a practice test, that has no answers posted. I have no way to test the code, but it has confused me. Can you please help me out with not only understanding the answer, but why.
int foo() {
int a = 1;
char b[] = "zapples";
a = *(b + 1);
if (a == 'a') return 1;
else return 0;
}
What does foo return? ____
Have they taught you how C pointers work?
I'm not going to give you a straight answer, but think about this:
b has the starting address of string "zapples". This means that b[0] points to "z". Another notation for this is *(b + 0), that is, "the value contained at address b, with an offset of 0). With this information, what is the value of *(b + 1)?
This should be enough to solve the exercise.
It will return 1.
The line a = *(b + 1) is the important one. It takes b as a pointer to the first element in the array and adds one so it points to the second. It is then dereferenced so that the value at that address 'a' is assigned to the variable a.