What does “%d! = %ld'n” mean in this code?
I'm still a beginner at C, so I'm finding difficulty in understanding "%d! = %ld".
I know that %d and %ld are respectively used for an integer and long, so "! =" is confusing me.
#include<stdio.h>
long factorial(int);
int main() {
int n;
long f;
printf("Enter an non-negative integer: ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.n");
else {
f = factorial(n);
printf("%d! = %ldn", n, f); //what does this mean?
}
return 0; }
long factorial(int n) {
if (n == 0)
return 1;
else
return(n * factorial(n-1)); }
c
add a comment |
I'm still a beginner at C, so I'm finding difficulty in understanding "%d! = %ld".
I know that %d and %ld are respectively used for an integer and long, so "! =" is confusing me.
#include<stdio.h>
long factorial(int);
int main() {
int n;
long f;
printf("Enter an non-negative integer: ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.n");
else {
f = factorial(n);
printf("%d! = %ldn", n, f); //what does this mean?
}
return 0; }
long factorial(int n) {
if (n == 0)
return 1;
else
return(n * factorial(n-1)); }
c
That's just the mathematical notation for the factorial function. It doesn't affect the%dformat specifier in any way.
– Govind Parmar
Nov 22 at 18:01
3
have you looked at the output for clues? like the fact that the=is actually printed in the statement?
– Claies
Nov 22 at 18:02
1
"! ="ofprintf("%d! = %ldn"will print 3 characters:'!',' ','='.
– chux
Nov 22 at 18:05
add a comment |
I'm still a beginner at C, so I'm finding difficulty in understanding "%d! = %ld".
I know that %d and %ld are respectively used for an integer and long, so "! =" is confusing me.
#include<stdio.h>
long factorial(int);
int main() {
int n;
long f;
printf("Enter an non-negative integer: ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.n");
else {
f = factorial(n);
printf("%d! = %ldn", n, f); //what does this mean?
}
return 0; }
long factorial(int n) {
if (n == 0)
return 1;
else
return(n * factorial(n-1)); }
c
I'm still a beginner at C, so I'm finding difficulty in understanding "%d! = %ld".
I know that %d and %ld are respectively used for an integer and long, so "! =" is confusing me.
#include<stdio.h>
long factorial(int);
int main() {
int n;
long f;
printf("Enter an non-negative integer: ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.n");
else {
f = factorial(n);
printf("%d! = %ldn", n, f); //what does this mean?
}
return 0; }
long factorial(int n) {
if (n == 0)
return 1;
else
return(n * factorial(n-1)); }
c
c
asked Nov 22 at 17:59
Charles Abou Haidar
111
111
That's just the mathematical notation for the factorial function. It doesn't affect the%dformat specifier in any way.
– Govind Parmar
Nov 22 at 18:01
3
have you looked at the output for clues? like the fact that the=is actually printed in the statement?
– Claies
Nov 22 at 18:02
1
"! ="ofprintf("%d! = %ldn"will print 3 characters:'!',' ','='.
– chux
Nov 22 at 18:05
add a comment |
That's just the mathematical notation for the factorial function. It doesn't affect the%dformat specifier in any way.
– Govind Parmar
Nov 22 at 18:01
3
have you looked at the output for clues? like the fact that the=is actually printed in the statement?
– Claies
Nov 22 at 18:02
1
"! ="ofprintf("%d! = %ldn"will print 3 characters:'!',' ','='.
– chux
Nov 22 at 18:05
That's just the mathematical notation for the factorial function. It doesn't affect the
%d format specifier in any way.– Govind Parmar
Nov 22 at 18:01
That's just the mathematical notation for the factorial function. It doesn't affect the
%d format specifier in any way.– Govind Parmar
Nov 22 at 18:01
3
3
have you looked at the output for clues? like the fact that the
= is actually printed in the statement?– Claies
Nov 22 at 18:02
have you looked at the output for clues? like the fact that the
= is actually printed in the statement?– Claies
Nov 22 at 18:02
1
1
"! =" of printf("%d! = %ldn" will print 3 characters: '!', ' ', '='.– chux
Nov 22 at 18:05
"! =" of printf("%d! = %ldn" will print 3 characters: '!', ' ', '='.– chux
Nov 22 at 18:05
add a comment |
4 Answers
4
active
oldest
votes
This will print:
%d, i.e. the decimal value ofint n
! =, i.e. the literal character sequence
%ld, i.e. the decimal value oflong f
add a comment |
%d and %ld are the formatting placeholders for int and long int in printf. The exclamation point is just the factorial symbol, as mentioned in the comment.
add a comment |
printf() allows you to print a string with variables inside of it. Let's say you have a variable i, containing an integer, 7.
printf("My variable is %d", i);
Will print
My variable is 7
to the console! That's because %d is how you tell printf(), "Hey, put an integer variable here!". The integer is then supplied as the next argument to the function. In your case, %d represents the integer n, and %ld represents the long integer f. Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
printf("Factorial of %d equals %ldn", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and n just means print a newline afterwords!
add a comment |
printf("%d! = %ldn", n, f); //what does this mean?
%d - print an integer as a signed decimal number.
l - specifies that the argument is a long int or unsigned long int as appropriate. %ld then prints a long int or unsigned long int
The printed text will become something like
n! = f
(factorial notation n!)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436191%2fwhat-does-d-ldn-mean-in-this-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will print:
%d, i.e. the decimal value ofint n
! =, i.e. the literal character sequence
%ld, i.e. the decimal value oflong f
add a comment |
This will print:
%d, i.e. the decimal value ofint n
! =, i.e. the literal character sequence
%ld, i.e. the decimal value oflong f
add a comment |
This will print:
%d, i.e. the decimal value ofint n
! =, i.e. the literal character sequence
%ld, i.e. the decimal value oflong f
This will print:
%d, i.e. the decimal value ofint n
! =, i.e. the literal character sequence
%ld, i.e. the decimal value oflong f
answered Nov 22 at 18:02
DevSolar
47.6k1294167
47.6k1294167
add a comment |
add a comment |
%d and %ld are the formatting placeholders for int and long int in printf. The exclamation point is just the factorial symbol, as mentioned in the comment.
add a comment |
%d and %ld are the formatting placeholders for int and long int in printf. The exclamation point is just the factorial symbol, as mentioned in the comment.
add a comment |
%d and %ld are the formatting placeholders for int and long int in printf. The exclamation point is just the factorial symbol, as mentioned in the comment.
%d and %ld are the formatting placeholders for int and long int in printf. The exclamation point is just the factorial symbol, as mentioned in the comment.
answered Nov 22 at 18:02
ahota
37128
37128
add a comment |
add a comment |
printf() allows you to print a string with variables inside of it. Let's say you have a variable i, containing an integer, 7.
printf("My variable is %d", i);
Will print
My variable is 7
to the console! That's because %d is how you tell printf(), "Hey, put an integer variable here!". The integer is then supplied as the next argument to the function. In your case, %d represents the integer n, and %ld represents the long integer f. Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
printf("Factorial of %d equals %ldn", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and n just means print a newline afterwords!
add a comment |
printf() allows you to print a string with variables inside of it. Let's say you have a variable i, containing an integer, 7.
printf("My variable is %d", i);
Will print
My variable is 7
to the console! That's because %d is how you tell printf(), "Hey, put an integer variable here!". The integer is then supplied as the next argument to the function. In your case, %d represents the integer n, and %ld represents the long integer f. Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
printf("Factorial of %d equals %ldn", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and n just means print a newline afterwords!
add a comment |
printf() allows you to print a string with variables inside of it. Let's say you have a variable i, containing an integer, 7.
printf("My variable is %d", i);
Will print
My variable is 7
to the console! That's because %d is how you tell printf(), "Hey, put an integer variable here!". The integer is then supplied as the next argument to the function. In your case, %d represents the integer n, and %ld represents the long integer f. Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
printf("Factorial of %d equals %ldn", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and n just means print a newline afterwords!
printf() allows you to print a string with variables inside of it. Let's say you have a variable i, containing an integer, 7.
printf("My variable is %d", i);
Will print
My variable is 7
to the console! That's because %d is how you tell printf(), "Hey, put an integer variable here!". The integer is then supplied as the next argument to the function. In your case, %d represents the integer n, and %ld represents the long integer f. Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
printf("Factorial of %d equals %ldn", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and n just means print a newline afterwords!
answered Nov 22 at 18:09
Alejandro Alvarado
18913
18913
add a comment |
add a comment |
printf("%d! = %ldn", n, f); //what does this mean?
%d - print an integer as a signed decimal number.
l - specifies that the argument is a long int or unsigned long int as appropriate. %ld then prints a long int or unsigned long int
The printed text will become something like
n! = f
(factorial notation n!)
add a comment |
printf("%d! = %ldn", n, f); //what does this mean?
%d - print an integer as a signed decimal number.
l - specifies that the argument is a long int or unsigned long int as appropriate. %ld then prints a long int or unsigned long int
The printed text will become something like
n! = f
(factorial notation n!)
add a comment |
printf("%d! = %ldn", n, f); //what does this mean?
%d - print an integer as a signed decimal number.
l - specifies that the argument is a long int or unsigned long int as appropriate. %ld then prints a long int or unsigned long int
The printed text will become something like
n! = f
(factorial notation n!)
printf("%d! = %ldn", n, f); //what does this mean?
%d - print an integer as a signed decimal number.
l - specifies that the argument is a long int or unsigned long int as appropriate. %ld then prints a long int or unsigned long int
The printed text will become something like
n! = f
(factorial notation n!)
answered Nov 22 at 18:32
Andreas DM
6,21352450
6,21352450
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436191%2fwhat-does-d-ldn-mean-in-this-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That's just the mathematical notation for the factorial function. It doesn't affect the
%dformat specifier in any way.– Govind Parmar
Nov 22 at 18:01
3
have you looked at the output for clues? like the fact that the
=is actually printed in the statement?– Claies
Nov 22 at 18:02
1
"! ="ofprintf("%d! = %ldn"will print 3 characters:'!',' ','='.– chux
Nov 22 at 18:05