How to print in C
Very much a beginner to C, in fact this is my first 开发者_Go百科tester program.
I can't actually figure out how to print this number out to the terminal.
#include <stdio.h>
int addNumbers(int a, int b)
{
int sum = a + b;
return sum;
}
int main(void)
{
int a = 4;
int b = 7;
printf(addNumbers(a,b));
return 0;
}
I am sure that in Java I could just replace the printf
with System.out
and it would have worked. I tried searching the answer earlier but if you don't know what to search it's hard to find an answer.
try this:
printf("%d", addNumber(a,b))
Here's the documentation for printf.
The first argument to printf()
is always a string value, known as a format control string. This string may be regular text, such as
printf("Hello, World\n"); // \n indicates a newline character
or
char greeting[] = "Hello, World\n";
printf(greeting);
This string may also contain one or more conversion specifiers; these conversion specifiers indicate that additional arguments have been passed to printf()
, and they specify how to format those arguments for output. For example, I can change the above to
char greeting[] = "Hello, World";
printf("%s\n", greeting);
The "%s" conversion specifier expects a pointer to a 0-terminated string, and formats it as text.
For signed decimal integer output, use either the "%d" or "%i" conversion specifiers, such as
printf("%d\n", addNumber(a,b));
You can mix regular text with conversion specifiers, like so:
printf("The result of addNumber(%d, %d) is %d\n", a, b, addNumber(a,b));
Note that the conversion specifiers in the control string indicate the number and types of additional parameters. If the number or types of additional arguments passed to printf()
don't match the conversion specifiers in the format string then the behavior is undefined. For example:
printf("The result of addNumber(%d, %d) is %d\n", addNumber(a,b));
will result in anything from garbled output to an outright crash.
There are a number of additional flags for conversion specifiers that control field width, precision, padding, justification, and types. Check your handy C reference manual for a complete listing.
printf is a fair bit more complicated than that. You have to supply a format string, and then the variables to apply to the format string. If you just supply one variable, C will assume that is the format string and try to print out all the bytes it finds in it until it hits a terminating nul (0x0).
So if you just give it an integer, it will merrily march through memory at the location your integer is stored, dumping whatever garbage is there to the screen, until it happens to come across a byte containing 0.
For a Java programmer, I'd imagine this is a rather rude introduction to C's lack of type checking. Believe me, this is only the tip of the iceberg. This is why, while I applaud your desire to expand your horizons by learning C, I highly suggest you do whatever you can to avoid writing real programs in it.
(This goes for everyone else reading this too.)
you have to use a placeholder in c, it is not like java where you can just print the value. So the placeholder of an integer in c is d. Also, I advise you to pick different characters for your function arguments just for better readability as it is recommended for better and safer code. Finally, you can just skip int sum = a + b;
in the function and have the function return a+b;
. Therefore, the final code would be :
#include <stdio.h>
int addNumbers(int a, int b)
{
return a+b;
}
int main()
{
int num1 = 4;
int num2 = 7;
printf("%d",addNumbers(num1,num2));
return 0;
}
精彩评论