Format specifies type “int*” but argument has type “int”What is the argument for printf that formats a long?What is the difference between const int*, const int * const, and int const *?Improve INSERT-per-second performance of SQLite?convert int to float to hexCorrect format specifier for double in printfTrouble with input using scanf()Format specifier for unsigned charData structure and patients recordsC Printfs are expecting a Type Specifiergetting values for an array using scanf function in C
Project Euler problem #112
Two men on a road
How do draw effects during the discard phase work?
Problem with listing a directory to grep
Complex conjugate and transpose "with respect to a basis"
Supervisor wants me to support a diploma-thesis SW tool after I graduated
If every star in the universe except the Sun were destroyed, would we die?
What is the purpose of the rotating plate in front of the lock?
Why are there no wireless switches?
Dragons and gems
The Green Glass Door, Revisited
Relationship between speed and cadence?
Contour plot of a sequence of spheres with increasing radius
How can I hint that my character isn't real?
Is this ram compatible with iMac 27"?
Do you need to burn fuel between gravity assists?
How to improvise or make pot grip / pot handle
Short story: Interstellar inspector senses "off" nature of planet hiding aggressive culture
What's the biggest difference between these two photos?
I multiply the source, you (probably) multiply the output!
Entering the US with dual citizenship but US passport is long expired?
Project Euler Problem 45
Examples where "thin + thin = nice and thick"
Why is Western European music harmonically driven?
Format specifies type “int*” but argument has type “int”
What is the argument for printf that formats a long?What is the difference between const int*, const int * const, and int const *?Improve INSERT-per-second performance of SQLite?convert int to float to hexCorrect format specifier for double in printfTrouble with input using scanf()Format specifier for unsigned charData structure and patients recordsC Printfs are expecting a Type Specifiergetting values for an array using scanf function in C
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Creating a code that prints out Body Mass Index
printf("What is your height in inches?n");
scanf("%d", height);
printf("What is your weight in pounds?n");
scanf("%d", weight);
I have height and weight as initialized as int height, int weight, but the program is not letting me run it because it says the format is type int* on both scanf lines. What am I doing wrong to get this program to run?
c
add a comment |
Creating a code that prints out Body Mass Index
printf("What is your height in inches?n");
scanf("%d", height);
printf("What is your weight in pounds?n");
scanf("%d", weight);
I have height and weight as initialized as int height, int weight, but the program is not letting me run it because it says the format is type int* on both scanf lines. What am I doing wrong to get this program to run?
c
1
Note: Good code checks the return value fromscanf().
– chux
Aug 29 '14 at 19:49
1
@chux good code doesn't usescanf()to read user input.
– The Paramagnetic Croissant
Aug 30 '14 at 5:33
1
@The Paramagnetic Croissant Mostly agree and more. IMO good code checksscanf(), better code usesfgets()and checks/parses that result instead. Robust code usesfgets()with all that and also handles file IO errors. All steps may be much for OP, so promoted the first.
– chux
Aug 30 '14 at 17:12
1
@chux "Robust code usesfgets()" - exactly.
– The Paramagnetic Croissant
Aug 30 '14 at 17:18
@TheParamagneticCroissant, I am sorry butscanfis good enough for scanning integers if you check the return value.
– Ajay Brahmakshatriya
Jun 15 '17 at 3:05
add a comment |
Creating a code that prints out Body Mass Index
printf("What is your height in inches?n");
scanf("%d", height);
printf("What is your weight in pounds?n");
scanf("%d", weight);
I have height and weight as initialized as int height, int weight, but the program is not letting me run it because it says the format is type int* on both scanf lines. What am I doing wrong to get this program to run?
c
Creating a code that prints out Body Mass Index
printf("What is your height in inches?n");
scanf("%d", height);
printf("What is your weight in pounds?n");
scanf("%d", weight);
I have height and weight as initialized as int height, int weight, but the program is not letting me run it because it says the format is type int* on both scanf lines. What am I doing wrong to get this program to run?
c
c
edited Aug 29 '14 at 19:56
user1336087
4,2057 gold badges30 silver badges37 bronze badges
4,2057 gold badges30 silver badges37 bronze badges
asked Aug 29 '14 at 19:27
Josh FairJosh Fair
261 gold badge1 silver badge2 bronze badges
261 gold badge1 silver badge2 bronze badges
1
Note: Good code checks the return value fromscanf().
– chux
Aug 29 '14 at 19:49
1
@chux good code doesn't usescanf()to read user input.
– The Paramagnetic Croissant
Aug 30 '14 at 5:33
1
@The Paramagnetic Croissant Mostly agree and more. IMO good code checksscanf(), better code usesfgets()and checks/parses that result instead. Robust code usesfgets()with all that and also handles file IO errors. All steps may be much for OP, so promoted the first.
– chux
Aug 30 '14 at 17:12
1
@chux "Robust code usesfgets()" - exactly.
– The Paramagnetic Croissant
Aug 30 '14 at 17:18
@TheParamagneticCroissant, I am sorry butscanfis good enough for scanning integers if you check the return value.
– Ajay Brahmakshatriya
Jun 15 '17 at 3:05
add a comment |
1
Note: Good code checks the return value fromscanf().
– chux
Aug 29 '14 at 19:49
1
@chux good code doesn't usescanf()to read user input.
– The Paramagnetic Croissant
Aug 30 '14 at 5:33
1
@The Paramagnetic Croissant Mostly agree and more. IMO good code checksscanf(), better code usesfgets()and checks/parses that result instead. Robust code usesfgets()with all that and also handles file IO errors. All steps may be much for OP, so promoted the first.
– chux
Aug 30 '14 at 17:12
1
@chux "Robust code usesfgets()" - exactly.
– The Paramagnetic Croissant
Aug 30 '14 at 17:18
@TheParamagneticCroissant, I am sorry butscanfis good enough for scanning integers if you check the return value.
– Ajay Brahmakshatriya
Jun 15 '17 at 3:05
1
1
Note: Good code checks the return value from
scanf().– chux
Aug 29 '14 at 19:49
Note: Good code checks the return value from
scanf().– chux
Aug 29 '14 at 19:49
1
1
@chux good code doesn't use
scanf() to read user input.– The Paramagnetic Croissant
Aug 30 '14 at 5:33
@chux good code doesn't use
scanf() to read user input.– The Paramagnetic Croissant
Aug 30 '14 at 5:33
1
1
@The Paramagnetic Croissant Mostly agree and more. IMO good code checks
scanf(), better code uses fgets() and checks/parses that result instead. Robust code uses fgets() with all that and also handles file IO errors. All steps may be much for OP, so promoted the first.– chux
Aug 30 '14 at 17:12
@The Paramagnetic Croissant Mostly agree and more. IMO good code checks
scanf(), better code uses fgets() and checks/parses that result instead. Robust code uses fgets() with all that and also handles file IO errors. All steps may be much for OP, so promoted the first.– chux
Aug 30 '14 at 17:12
1
1
@chux "Robust code uses
fgets()" - exactly.– The Paramagnetic Croissant
Aug 30 '14 at 17:18
@chux "Robust code uses
fgets()" - exactly.– The Paramagnetic Croissant
Aug 30 '14 at 17:18
@TheParamagneticCroissant, I am sorry but
scanf is good enough for scanning integers if you check the return value.– Ajay Brahmakshatriya
Jun 15 '17 at 3:05
@TheParamagneticCroissant, I am sorry but
scanf is good enough for scanning integers if you check the return value.– Ajay Brahmakshatriya
Jun 15 '17 at 3:05
add a comment |
4 Answers
4
active
oldest
votes
scanf requires the format (your "%d") and also a memory address of the variable where it should put the value that was read. height and weight are int, not the memory address of an int (this is what int * type 'says': a pointer to a memory address of an int). You should use the operator & to pass the memory address to scanf.
Your code should be:
printf("What is your height in inches?n");
scanf("%d", &height);
printf("What is your weight in pounds?n");
scanf("%d", &weight);
Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.
1
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
add a comment |
taking into account what the other users have said, try something like this;
int height; <---- declaring your variables
int weight;
float bmi; <---- bmi is a float because it has decimal values
printf("What is your height in inches?n");
scanf("%d", &height); <----- don't forget to have '&' before variable you are storing the value in
printf("What is your weight in pounds?n");
scanf("%d", &weight);
bmi = (weight / pow(height, 2)) * 703; <---- the math for calculating BMI
printf("The BMI is %fn", bmi);
(for this you will need to include the math.h library.)
add a comment |
scanf reads characters from the standard input, interprets them according to the format specifiers here "%d" for integers and stores them in corresponding arguments.
To store them you must specify &variable_name, it will specify the address location where the input should be stored.
Your scanf statement should be:
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);
add a comment |
Like others have said, it's because when you have a value that is a primitive (like an int) that is being read with scanf you have to pass the memory address to that primitive value. However, just to add something that has not been mentioned yet, the same is not true for strings.
Example 1. Using with a primitive value
#include <stdio.h>
int main()
int primitiveInteger; // integer (i.e. no decimals allowed)
scanf("%i", &primitiveInteger); // notice the address of operator getting the address of the primitiveInteger variable by prepending the variable name with the & (address of) operator.
printf("Your integer is %i", primitiveInteger); simply printing it out here to the stdout stream (most likely your terminal.)
Example 2. Not using a primitive value
#include <stdio.h>
int main()
char *nonPrimitiveString; // initialize a string variable which is NOT a primitive in C
scanf("%s", nonPrimitiveString); // notice there is no address of operator here
printf("%sn", nonPrimitiveString); // prints string
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/4.0/"u003ecc by-sa 4.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%2f25574981%2fformat-specifies-type-int-but-argument-has-type-int%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
scanf requires the format (your "%d") and also a memory address of the variable where it should put the value that was read. height and weight are int, not the memory address of an int (this is what int * type 'says': a pointer to a memory address of an int). You should use the operator & to pass the memory address to scanf.
Your code should be:
printf("What is your height in inches?n");
scanf("%d", &height);
printf("What is your weight in pounds?n");
scanf("%d", &weight);
Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.
1
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
add a comment |
scanf requires the format (your "%d") and also a memory address of the variable where it should put the value that was read. height and weight are int, not the memory address of an int (this is what int * type 'says': a pointer to a memory address of an int). You should use the operator & to pass the memory address to scanf.
Your code should be:
printf("What is your height in inches?n");
scanf("%d", &height);
printf("What is your weight in pounds?n");
scanf("%d", &weight);
Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.
1
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
add a comment |
scanf requires the format (your "%d") and also a memory address of the variable where it should put the value that was read. height and weight are int, not the memory address of an int (this is what int * type 'says': a pointer to a memory address of an int). You should use the operator & to pass the memory address to scanf.
Your code should be:
printf("What is your height in inches?n");
scanf("%d", &height);
printf("What is your weight in pounds?n");
scanf("%d", &weight);
Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.
scanf requires the format (your "%d") and also a memory address of the variable where it should put the value that was read. height and weight are int, not the memory address of an int (this is what int * type 'says': a pointer to a memory address of an int). You should use the operator & to pass the memory address to scanf.
Your code should be:
printf("What is your height in inches?n");
scanf("%d", &height);
printf("What is your weight in pounds?n");
scanf("%d", &weight);
Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.
edited Jun 15 '17 at 0:12
Bruno Lange
6908 silver badges11 bronze badges
6908 silver badges11 bronze badges
answered Aug 29 '14 at 19:33
braindfbraindf
5342 silver badges10 bronze badges
5342 silver badges10 bronze badges
1
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
add a comment |
1
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
1
1
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
What "references"? There ain't no references in C. Those are pointers.
– The Paramagnetic Croissant
Aug 30 '14 at 5:34
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@The Paramagnetic Croissant C does have a referenced type. "A pointer type may be derived from a function type or an object type, called the referenced type" C11dr §6.2.5 20. Perhaps this answer is using reference in that context.
– chux
Aug 30 '14 at 17:19
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
@chux I know about that, but the question using it is unfortunate and incorrect, since a referenced type is not the same thing as a reference, let alone a pointer.
– The Paramagnetic Croissant
Aug 30 '14 at 17:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
Yeah, the term reference was wrong. I changed to memory address. Thanks
– braindf
Aug 30 '14 at 18:20
add a comment |
taking into account what the other users have said, try something like this;
int height; <---- declaring your variables
int weight;
float bmi; <---- bmi is a float because it has decimal values
printf("What is your height in inches?n");
scanf("%d", &height); <----- don't forget to have '&' before variable you are storing the value in
printf("What is your weight in pounds?n");
scanf("%d", &weight);
bmi = (weight / pow(height, 2)) * 703; <---- the math for calculating BMI
printf("The BMI is %fn", bmi);
(for this you will need to include the math.h library.)
add a comment |
taking into account what the other users have said, try something like this;
int height; <---- declaring your variables
int weight;
float bmi; <---- bmi is a float because it has decimal values
printf("What is your height in inches?n");
scanf("%d", &height); <----- don't forget to have '&' before variable you are storing the value in
printf("What is your weight in pounds?n");
scanf("%d", &weight);
bmi = (weight / pow(height, 2)) * 703; <---- the math for calculating BMI
printf("The BMI is %fn", bmi);
(for this you will need to include the math.h library.)
add a comment |
taking into account what the other users have said, try something like this;
int height; <---- declaring your variables
int weight;
float bmi; <---- bmi is a float because it has decimal values
printf("What is your height in inches?n");
scanf("%d", &height); <----- don't forget to have '&' before variable you are storing the value in
printf("What is your weight in pounds?n");
scanf("%d", &weight);
bmi = (weight / pow(height, 2)) * 703; <---- the math for calculating BMI
printf("The BMI is %fn", bmi);
(for this you will need to include the math.h library.)
taking into account what the other users have said, try something like this;
int height; <---- declaring your variables
int weight;
float bmi; <---- bmi is a float because it has decimal values
printf("What is your height in inches?n");
scanf("%d", &height); <----- don't forget to have '&' before variable you are storing the value in
printf("What is your weight in pounds?n");
scanf("%d", &weight);
bmi = (weight / pow(height, 2)) * 703; <---- the math for calculating BMI
printf("The BMI is %fn", bmi);
(for this you will need to include the math.h library.)
edited Aug 30 '14 at 9:09
answered Aug 30 '14 at 5:19
Xavier DassXavier Dass
4268 silver badges17 bronze badges
4268 silver badges17 bronze badges
add a comment |
add a comment |
scanf reads characters from the standard input, interprets them according to the format specifiers here "%d" for integers and stores them in corresponding arguments.
To store them you must specify &variable_name, it will specify the address location where the input should be stored.
Your scanf statement should be:
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);
add a comment |
scanf reads characters from the standard input, interprets them according to the format specifiers here "%d" for integers and stores them in corresponding arguments.
To store them you must specify &variable_name, it will specify the address location where the input should be stored.
Your scanf statement should be:
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);
add a comment |
scanf reads characters from the standard input, interprets them according to the format specifiers here "%d" for integers and stores them in corresponding arguments.
To store them you must specify &variable_name, it will specify the address location where the input should be stored.
Your scanf statement should be:
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);
scanf reads characters from the standard input, interprets them according to the format specifiers here "%d" for integers and stores them in corresponding arguments.
To store them you must specify &variable_name, it will specify the address location where the input should be stored.
Your scanf statement should be:
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);
edited Aug 29 '14 at 19:57
answered Aug 29 '14 at 19:46
user1336087user1336087
4,2057 gold badges30 silver badges37 bronze badges
4,2057 gold badges30 silver badges37 bronze badges
add a comment |
add a comment |
Like others have said, it's because when you have a value that is a primitive (like an int) that is being read with scanf you have to pass the memory address to that primitive value. However, just to add something that has not been mentioned yet, the same is not true for strings.
Example 1. Using with a primitive value
#include <stdio.h>
int main()
int primitiveInteger; // integer (i.e. no decimals allowed)
scanf("%i", &primitiveInteger); // notice the address of operator getting the address of the primitiveInteger variable by prepending the variable name with the & (address of) operator.
printf("Your integer is %i", primitiveInteger); simply printing it out here to the stdout stream (most likely your terminal.)
Example 2. Not using a primitive value
#include <stdio.h>
int main()
char *nonPrimitiveString; // initialize a string variable which is NOT a primitive in C
scanf("%s", nonPrimitiveString); // notice there is no address of operator here
printf("%sn", nonPrimitiveString); // prints string
add a comment |
Like others have said, it's because when you have a value that is a primitive (like an int) that is being read with scanf you have to pass the memory address to that primitive value. However, just to add something that has not been mentioned yet, the same is not true for strings.
Example 1. Using with a primitive value
#include <stdio.h>
int main()
int primitiveInteger; // integer (i.e. no decimals allowed)
scanf("%i", &primitiveInteger); // notice the address of operator getting the address of the primitiveInteger variable by prepending the variable name with the & (address of) operator.
printf("Your integer is %i", primitiveInteger); simply printing it out here to the stdout stream (most likely your terminal.)
Example 2. Not using a primitive value
#include <stdio.h>
int main()
char *nonPrimitiveString; // initialize a string variable which is NOT a primitive in C
scanf("%s", nonPrimitiveString); // notice there is no address of operator here
printf("%sn", nonPrimitiveString); // prints string
add a comment |
Like others have said, it's because when you have a value that is a primitive (like an int) that is being read with scanf you have to pass the memory address to that primitive value. However, just to add something that has not been mentioned yet, the same is not true for strings.
Example 1. Using with a primitive value
#include <stdio.h>
int main()
int primitiveInteger; // integer (i.e. no decimals allowed)
scanf("%i", &primitiveInteger); // notice the address of operator getting the address of the primitiveInteger variable by prepending the variable name with the & (address of) operator.
printf("Your integer is %i", primitiveInteger); simply printing it out here to the stdout stream (most likely your terminal.)
Example 2. Not using a primitive value
#include <stdio.h>
int main()
char *nonPrimitiveString; // initialize a string variable which is NOT a primitive in C
scanf("%s", nonPrimitiveString); // notice there is no address of operator here
printf("%sn", nonPrimitiveString); // prints string
Like others have said, it's because when you have a value that is a primitive (like an int) that is being read with scanf you have to pass the memory address to that primitive value. However, just to add something that has not been mentioned yet, the same is not true for strings.
Example 1. Using with a primitive value
#include <stdio.h>
int main()
int primitiveInteger; // integer (i.e. no decimals allowed)
scanf("%i", &primitiveInteger); // notice the address of operator getting the address of the primitiveInteger variable by prepending the variable name with the & (address of) operator.
printf("Your integer is %i", primitiveInteger); simply printing it out here to the stdout stream (most likely your terminal.)
Example 2. Not using a primitive value
#include <stdio.h>
int main()
char *nonPrimitiveString; // initialize a string variable which is NOT a primitive in C
scanf("%s", nonPrimitiveString); // notice there is no address of operator here
printf("%sn", nonPrimitiveString); // prints string
edited Mar 28 at 7:04
answered Mar 28 at 6:33
brff19brff19
1011 silver badge11 bronze badges
1011 silver badge11 bronze badges
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.
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%2f25574981%2fformat-specifies-type-int-but-argument-has-type-int%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
1
Note: Good code checks the return value from
scanf().– chux
Aug 29 '14 at 19:49
1
@chux good code doesn't use
scanf()to read user input.– The Paramagnetic Croissant
Aug 30 '14 at 5:33
1
@The Paramagnetic Croissant Mostly agree and more. IMO good code checks
scanf(), better code usesfgets()and checks/parses that result instead. Robust code usesfgets()with all that and also handles file IO errors. All steps may be much for OP, so promoted the first.– chux
Aug 30 '14 at 17:12
1
@chux "Robust code uses
fgets()" - exactly.– The Paramagnetic Croissant
Aug 30 '14 at 17:18
@TheParamagneticCroissant, I am sorry but
scanfis good enough for scanning integers if you check the return value.– Ajay Brahmakshatriya
Jun 15 '17 at 3:05