This question already has answers here:
What is the use of the %n format specifier in C?
(12 answers)
Closed 7 years ago.
So I have looked around and I have found little information on %n in general and no info on how to use it with a variable.
As far as I can tell the code I am using should work but I do not know what it is not. The particular line that I am having trouble with is:
printf("%d %n", num[x], &c);
Below is the entire code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
//seed rand, declare arrays, declare variables
srand(time(NULL));
int num[10];
int c = 0;
int total = 0;
int x;
printf( "%s%14s%20s\n", "Value", "Characters", "Total Characters" );
//Loads the num array with random numbers.
for(x = 1; x < 10; x++)
{
num[x] = 1 + rand() % 1000;
}
for (x = 1; x < 10; x++)
{
printf("%d %n", num[x], &c);
printf("%14d", c);
total = total + c;
printf("%20d\n", total);
}
}
From the C Standard
n The argument shall be a pointer to signed integer into which is
written the number of characters written to the output stream so far
by this call to fprintf. No argument is converted, but one is
consumed. If the conversion specification includes any flags, a field
width, or a precision, the behavior is undefined.
The same is valid for printf
Here is a demonstrative program
#include <stdio.h>
int main(void)
{
int n1, n2;
printf( "%s%n%s%n\n", "Hello", &n1, " World", &n2 );
printf( "%d\t%d\n", n1, n2 );
return 0;
}
The program output is
Hello World
5 11
Related
I have compiled this code and it works just fine up to value 4 then it starts returning character instead of integer. I am talking about first equation => x= num*2; Here when I enter num value as 5 the output returns a.
#include <stdio.h>
int main(void)
{
int num;
int x; This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num); //
//printf("%d\n", num);
x = num * 2 ;
printf("%x\n", x);
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
}
please tell me if there is a mistake I am a newbie to coding.
As I see you are learning C language, and after reading your explanation, I feel that you want to print the integer value of variable x.
Kindly replace %x with %d in the print statement of variable x,
and you will be successfully able to print the value.
#include <stdio.h>
int main(void)
{
int num;
int x; // This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num);
x = num * 2 ;
printf("%d\n", x); // %d for integer and %x for hexadecimal values
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
return 0;
}
Finally, do read more about format specifiers in scanf and
printf statements.
This question already has answers here:
Returning an array using C
(8 answers)
Closed 3 years ago.
Here is the question:
Write a function that takes a file handle f as a parameter and returns
the number of occurrences of all digits in the file found in the file.
Write a program to show how this function can be used to find the
number of digits in the file "abd.dat".
After I finished this question, I noticed it need a return in the function, I just do the print inside of the function. I made some try to modify the code to do the print in main() function, but it seems not work, the output becomes weird.
#include <stdio.h>
#include <stdlib.h>
#define BASE 10
int digits(FILE *);
int main()
{
FILE *fp = fopen("abc.dat", r);
digits(fp);
fclose(fp);
return EXIT_SUCCESS;
}
int digits(FILE *f)
{
long long num, digits;
int i, lastDigit;
int occu[BASE];
fscanf(f, "%lld", &num);
for (i = 0; i < BASE; i++)
{
occu[i] = 0;
}
digits = num;
while (digits != 0)
{
lastDigit = digits % 10;
digits /= 10;
occu[lastDigit]++;
}
printf(Occurrences of each digit in %lld is: \n, num);
for (i = 0; i < BASE; i++)
{
printf("Occurrences of %d = %d\n", i, occu[i]);
}
}
you can define your function to return int* instead of int
In this case you need to use malloc for not losing the array at the end of the function.
after this you can print it from the main
i am trying to write a program such that if a % 2^j = 0 then it prints 1 , else it prints 0 so at the end i get a 16 bit binary code. But i get no error and after i enter an input number (a) terminal crashes.
Thank you for your help.
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
uint16_t a, j, b, mod;
printf(" Please insert a number between 0 and 65535 \n ");
scanf("%d", &a);
j = 16;
while (j > 0)
;
{
b = pow(j, 2);
mod = a % b;
if (mod == 0) {
printf("%d", 1);
} else {
printf("%d", 0);
}
j = j - 1;
}
return 0;
}
You have undefined behavior as you use the %d format for scanf. This format specifier expects the argument to be a pointer to int.
Mismatching format specifier and argument type leads to UB.
For uint16_t use the macro SCNu16 (as documented in e.g. this reference):
scanf("%" SCNu16, &a);
This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 4 years ago.
So I'm writing a program about finding the sum number of all ranges for instance, if I enter a 1 and then a 10 it should display 55, but instead it display some long random number. Does anyone have a clue what's wrong with this code? Thanks!
#define <stdio.h>
calculateSum(int lowNumber, int highNumber);
int main()
{
int lowerNumber,
higherNumber;
scanf("%d", lowerNumber);
scanf("%d", higherNumber);
printf("The sum of all ranges is: %d", calculateSum(lowerNumber, higherNumber));
int calculateSum(int lowNumber, int highNumber)
{
int total;
for(int x = lowNumber; x <= highNumber; x++)
{
total = total + x;
}
return total;
}
You're not initializing total before its first use as a rvalue (which is the RHS of assignment total = total + x;) thus its value remains undefined (and indeterminate.)
In addition to #biplls answer there is also another issue:
int lowerNumber,
higherNumber;
scanf("%d", lowerNumber);
scanf("%d", higherNumber);
is UB (produces a warning on compilation and a SEGFAULT during runtime). printf expects an int*, not int. The correct code would be
int lowerNumber,
higherNumber;
scanf("%d", &lowerNumber);
scanf("%d", &higherNumber);
A few errors here
it's #include <stdio.h> not #define
When using scanf you have to add an & before the var you read into (in this case!! )
The main fn doesn't return anything, it should be returning 0 (normally) (apparently I'm wrong on this , main returns 0 implicitly from C99)
Main fn isn't closed before you write the next fn
total should be set to 0
for loop initialization is allowed only in C99
It is a good idea to declare the return type of the prototype
fixing all this
#include <stdio.h>
int calculateSum(int lowNumber, int highNumber);
int main()
{
int lowerNumber,
higherNumber;
scanf("%d", &lowerNumber);
scanf("%d", &higherNumber);
printf("The sum of all ranges is: %d\n", calculateSum(lowerNumber, higherNumber));
return 0;
}
int calculateSum(int lowNumber, int highNumber)
{
int total;
total = 0;
for(int x = lowNumber; x <= highNumber; x++)
{
total = total + x;
}
return total;
}
Running the above
$ ./sum
1
10
The sum of all ranges is: 55
This question already has answers here:
Why does scanf() need & operator (address-of) in some cases, and not others? [duplicate]
(5 answers)
Ampersand(&) and scanf in C?
(5 answers)
errors and warning message when using pointer
(3 answers)
Closed 5 years ago.
It's an extremely simple code, when I run it, it lets me enter the int, but when I enter it, the program crashes. I tried putting a printf right after the scanf to check if the program reads the int correctly, it seems it doesn't. I can't output the integer that I input.
#include <stdio.h>
#include <math.h>
void coolfunction(int x, int *y)
{
*y = exp(x);
}
int main()
{
int n;
double result = 0;
printf("Enter an integer: ");
scanf("%d", n);
coolfunction(n, &result);
printf("\n e^%d = %lf \n", n, result);
return 0;
}
Here's a version using a double parameter:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void coolfunction(int x, double *y)
{
*y = exp(x);
}
int main()
{
int n = 0, scanned = 0;
double result = 0;
printf("Enter an integer: ");
scanned = scanf("%d", &n);
if (scanned < 1) {
fprintf(stderr, "value entered not a valid integer\n");
exit(EXIT_FAILURE);
}
coolfunction(n, &result);
printf("\n e^%d = %g \n", n, result);
return 0;
}
You also missed the & in your scanf call: the function needs to know the address of your variable, not its value.