Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
//(1)
#include <stdio.h>
int main() {
printf("%d", 5);
}
//(2)
#include <stdio.h>
int main() {
printf("5");
}
What are the difference between (1) code block, and (2) code block?
The difference is that printf("%d", 5) is used to print (to stdout) the value of a variable of type int.
Whereas printf("5") is used to print (to stdout) a string. To get more clear, let's modify the code slightly.
//(1)
#include <stdio.h>
int var = 5;
int main() {
printf("%d", var);
}
//Output : 5
//(2)
#include <stdio.h>
int var = 5;
int main() {
printf("var");
}
Output : var
As you can see, printf() in (1) interprets var as a variable of type int and then prints its value.
Whereas printf() in (2) interprets var as a string and prints it as it is.
Are you talking about printf or main? I think that you are talking about printf.
Return value
Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).
The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.)
If an output error is encountered, a negative value is returned.
Both of those calls should return the number one.
The answer to your question is that they both return an integer. However, you can treat an integer like a char. The ascii table shows you the mapping of integers to chars.
Both the code block will give output as 5 .
Please check the image file.
enter image description here
If you have doubts, just try to use typeof keyword of that variable and check according to that
Related
This question already has answers here:
How to repeat a char using printf?
(12 answers)
Closed 2 years ago.
I want to print a char '*' repeatedly where I give the no. of times the asterisk should be repeated.
Example: count = 20 and I want to print ******************** using printf() and format specifiers.
There is certainly no way to achieve that using only format specifier. You could hide your loop in a macro maybe but you'll definitely need a loop somewhere.
You cannot do that with standard format specifiers provided by printf(). However, there's a hacky solution (assuming the maximum padding length is reasonable), if you are willing to waste some space to store a filler string in your program.
#include <stdio.h>
int main(void) {
const char *fill = "********************"; // 20 chars
printf("%.*s\n", 10, fill);
printf("%.*s\n", 15, fill);
int n = 20;
printf("%.*s\n", n, fill);
return 0;
}
This works using .* to provide the maximum length of the string to print as first parameter.
Output:
**********
***************
********************
NOTE: you will only get up to strlen(fill) characters of padding (20 in the above example), anything more and printf will stop at the \0 terminator of fill.
I hope this is what you are looking for:
#include <stdio.h>
int main()
{
printf("%.*s", 20, "********************************");;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi i am trying to read two characters from a file and want to send it to uint8_t* as hexadecimal .
Code:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int file_handling();
int main()
{
uint8_t *output ;
output=file_handling() ;
printf("\noutput_main --> %02x",output);
}
int file_handling()
{
uint8_t *output_hand ;
char c;
FILE *f_gets = fopen("filename", "r");
if(f_gets==NULL)
{
printf("Please point to a valid key file!\n");
fclose(f_gets);
return 0;
}
char str[3];
if( fgets (str, 3, f_gets)!=NULL )
{
/* writing content to stdout */
puts(str);
output_hand = (uint8_t *)(str);
puts(output_hand);
printf("\noutput %s --- %02x --> size --> %lu",str,*output_hand,sizeof(*output_hand));
}
fclose(f_gets);
return *output_hand;
}
following is output
we we
output we --- 77 --> size --> 1 output_main --> 65
what i can understand is 77 is ascii for w and 65 is ascii for e
but i want to put "we" which i suppose is a hex in uint8_t *output
where is the problem
in main ,if i use pointer "*output=file_handling()" instead of just output i get segmentation fault.
How to read value from a file and put it into uint8_t , where file is having hex characters,how fget identifies it as hex or char.
Thanks
file is a text file
ab
fe
ea
ce
1d
Basically
uint8_t *output;
*output = 0xFA ;
it works
but i want to read from above file and put it into output variable
You have plenty of problems, here's three of them:
You define str to be an array of two characters, which means it can only contain a single-character string. You then call fgets telling it that str it three characters, which means it can and will write out of bounds of your array.
You have declared file_handling to return an int value. You return the first character in the array string, and assign that to the pointer variable output in the main function. You then treat this pointer as a single int value.
In the main function you pass the pointer output but print it as an int value. There's a mismatch between the format specifier and the argument.
The first and third issues lead to undefined behavior.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have been trying to create a function in C that reads in doubles that are then stored into an array. I want to be able to return the size of the array so that I can use it in main. So the purpose of this function is to ask the user to input values into an array and type in ^d (ctrl+d) or EOF to end the loop.
#include <stdio.h>
//Prototype Declaration
int getdata(double[], int);
int main(int argc, char* argv[]) {
double array[20];
int count = 0, max = 20;
//Calls the getdata function
count = getdata(array, max);
printf("%s%lf%s","Array1: " ,array[1], "\n");
printf("%s%d%s", "Count is: ", count, "\n");
return 0;
}
----- (I'm linking modules so these are in different files) -------
#include <stdio.h>
//Define getdata.c
int getdata(double values[], int limit){
printf("%s","Please enter your values into the array.\n");
int count = 0;
double n;
while ((count < limit) && (scanf("%lf",&n) != EOF)) {
values[count] = n;
count++;
}
return count;
}
What happens, though, is that if I break the loop early, the array values pass by fine but the count would just print out 20. I am assuming that the program automatically fills in the rest of the empty indexes with something and continues to increment the count value after I type in EOF. What can I do so that the program can correctly get the amount of values inputted? I do not code much in C. I mostly do my work in C++. I am familiar with passing by reference but using C.
You need to be aware that scanf() won't swallow input that can't be converted. If there is a problem then it leaves the (faulty) input in the stream for you to fail to convert on the next iteration of the loop. So it's repeatedly trying to match the same invalid thing as a double.
I suggest using fgets() plus sscanf() as a better solution than using scanf(), or (at minimum) check the return value from scanf() to ensure you got the expected number of fields read (which is 1 in this case). I bet it is returning 0 rather than EOF, so your loop continues until count matches limit.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
static char string[12];
int length,c,d;
printf("Enter a string :");
gets(string);
length=strlen(string);
printf("\nLength of the string is %d",length);
for(c=0;c<=length-2;c++)
{
d=c+1;
printf("\t%.*s\n",d,string);
}
for(c=length;c>=0;c--)
{
d=c+1;
printf("\t%.*s\n",d,string);
}
}
I am very much confused about the usage of %.*s in the printf statement. I know %s is used for displaying strings, but I am confused the usage of .* before s in this program. Also there is only one datatype (%s) mentioned inside the quotation marks in the printf statement, but there are two variables mentioned in the printf statement.
It is a precision component, which specifies maximum number of bytes for string conversions. Asterisk (*), uses an integer argument, which specifies the value (for precision) to be used.
As an example, the following code:
#include <stdio.h>
int main(int argv, char **argc)
{
char *s = "hello, world";
printf("%.*s\n", 4, s);
return 0;
}
gives output:
hell
The format statement can allow a width and precision value. So, to print a string for a variable length then specify printf("%.*s", length, string). The length is substituted for the asterisk.
Now this is a silly puzzle I got from some exam paper,sadly I am unable to figure it out from last 15 minutes.
#include <stdio.h>
int main(void){
/* <something> */
putchar(*(wer[1]+1));
return 0;
}
What should we replace in place of something in order to get the output e.Now we know putchar takes a int as argument but this code assumes to give a pointer.Does this question is even valid ?
const char *wer[2] = { "failed", "test" };
Since a[i] is the same as *(a + i) by definition, you can transform the putchar() argument into wer[1][1]. So, something like char *wer[2] would be a satisfactory definition, and any values such that wer[1][1] == 'e' will work.
char * wer[] = { "foobar", "he"};
First, in the code you have mentioned the argument to putchar() is
*(wer[1]+1)
which is not a pointer. It seems that wer[1] is some pointer and that address pointed by wer[1] + 1 is dereferenced. So if wer is an array of pointers to int, then putchar argument should be int which is fine.
Now the code in place of something can be
You have not mentioned clearly what does e mean, is e a char or e is 2.71... (Natural logarithm base) In either case it should be easy to get that output with this code.
-AD
An easy answer is:
char **wer;
putchar('e');
return 0;
For example, the complete code would like like:
#include <stdio.h>
int main(int argc, char **argv)
{
/* Something starts here */
char **wer;
putchar('e');
return 0;
/* Something ends here */
putchar(*(wer[1] + 1));
return 0;
}
The output is:
susam#swift:~$ gcc really-silly-puzzle.c && ./a.out && echo
e
susam#swift:~$
A more interesting question would have been: What is the shortest code that can replace /* */ to get the output 'e'?
From the very pedantic point of view, there's no correct answer to the question. The question is invalid. C language itself makes no guarantees about the output of a program if the output does not end in a newline character. See 7.19.2/2
A text stream is an ordered sequence
of characters composed into lines,
each line consisting of zero or more
characters plus a terminating new-line
character. Whether the last line
requires a terminating new-line
character is implementation-defined.
This program output to the standard output, which is a text stream. The output of this program is implementation-dependent, regardless of what you put in place of /* <something> */, meaning that the question might make sense for some specific platform, but it makes no sense as an abstract C language question.
I highly doubt though that your examiners are expecting this kind of pedantry from you :)))