force length of int from a sensor [duplicate] - c

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 6 years ago.
I get data from a sensor in int type but the data range from 0 to 999.
I need a simple way for the var to always be the same length.
Like if I get 123 I don't need to change it but if it's 43 I need it to be 043.
Is there a simple way to do that?
Thanks.

Try with:
printf("%03d", var);

Related

Very simple multi array c program - problem noob [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I can't figure out where the problem is in my program?
#include <stdio.h>
int main()
{
int a[2][2]={{1,2},{3,4}};
printf("The value of a[2][1] is %d",a[2][1]);
return(0);
}
I expected the answer to be 3, it's actually wow! 32765 wait! what!? I'm pretty confused.
Can someone help?
You don't have anything in the spot a[2][1]. I think what you meant to put is a[1][0]. Remember that the index starts at 0 not at 1.
The reason why you are getting that big number is because that number was already sitting there in that memory location. It has nothing to do with the array you created.

0 at the begining of value isnt shown in the output [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 2 years ago.
my question is short, if I have the following 2 lines of code:
int var = 01;
printf("%d", var);
the output is : 1
how do I get 01 rather than 1?
Use left padded format string.
Solution
int var = 1;
printf("%02d", var);

A simple question from a C beginner, about arrays [duplicate]

This question already has answers here:
read int with scanf until new line
(3 answers)
How to get integer input in an array using scanf in C?
(4 answers)
Closed 2 years ago.
I'm new to C language and I'm trying to make a program that includes an array of type int with the max length of 10.
I want to enter a random number of 1-digit numbers and set all the other values to 0.
For example, the console will look like this:
Enter B Values : 1 2 5 7 8
and the B array will look like this 1 2 5 7 8 0 0 0 0 0
I'm trying to add the numbers at the same line without using Enter.
Could anyone help?
Could the answer be EOF related?

How to randomize numbers with no repeat in C? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?

I am getting only 2 as op for the below program [duplicate]

This question already has answers here:
Is uninitialized data behavior well specified?
(7 answers)
What will be the value of uninitialized variable? [duplicate]
(6 answers)
Closed 8 years ago.
Please explain why I am getting output 2 here. My expected o/p is 5 or 7. Please throw some light. Thank you!
#include<stdio.h>
typedef enum {a=3, b, c, d, j}e;
void f(e *e1) {
printf("%ld", (int)*e1);
}
main(){
e es;
f(&es);
}
You haven't initialized es, so your program is just printing the random value that happens to be on the stack when the program runs.
You need to say something like:
e es = c;
That will give you the 5 output you seek.

Resources