I wrote this function to reverse a number. I will have test cases that are up to 2^32. I need the function to return unsigned ints. My question is this: why wont this print?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
unsigned int reverse(unsigned int a);
int main(){
printf("%u\n",reverse(987654321));
//printf("%ui\n",reverse(987654321)); //this wont work either
return 0;
}
unsigned int reverse(unsigned int a)
{
int temp=0;
while(a>0)
{
temp=10*temp+a%10;
a/=10;
}
return temp;
}
Yes I did forget about the proto-type... Bear with me I have been doing Java and Lisp lately. However, my compiler keeps giving me a warning saying I have extra characters in the format string. It also does this if I have type "long long int" and I use "%lli% in the format string, which I also tried.
You forgot to include prototype for your function before main.
unsigned int reverse(unsigned int a);
The number probably is just too big for an unsigned on your architecture.
Remember to always enable maximum warning level for your compiler. This should have told you that reverse is not known where you use it, that you are passing an int to the %u format and also that your number ist too big.
Related
I try to assign zero to a field in a structure called list
list.ultimo = 0;
but when I use printf
printf("%d", list.ultimo);
I get the result
32766
but when I add
unsigned short int cero = 0;
the result of printf is correct, no matter that does not use cero. why?
lista.h
#ifndef LISTA_H
#define LISTA_H
typedef struct{
elemento lista[TAMANOMAX];
unsigned short int ultimo;
}LISTA;
void Insertar(elemento, unsigned short int posicion, LISTA);
#endif
lista.c
#include<stdio.h>
typedef short int elemento;
#ifndef TAMANOMAX
#define TAMANOMAX 10
#endif
#include"lista.h"
void Insertar(elemento x, unsigned short int p, LISTA lista){
if (lista.ultimo >= TAMANOMAX){
puts("full list");
printf("%hu",lista.ultimo);
}
}
main.c
#include<stdio.h>
typedef unsigned short int elemento;
#define TAMANOMAX 4
#include"lista.h"
int main(){
LISTA esferas;
esferas.ultimo = 0;
Insertar(2,0,esferas);
printf("\n%hu\n", esferas.ultimo);
return 0;
}
the result is
$gcc -Wall lista.c main.c
full list
32765
0
I'm new to this, I'm slow for now, I regret the delay
Use %hd to print a short int and %hu to print an unsigned short int. %d is for int.
EDIT: Now that you have posted the full code, it appears that TAMANOMAX is set to 10 in lista.c, but set to 4 in main.c, leading to an incompatibilty between your main() function and the Insertar one. You should have the same value in all files. And if you want to work with different array lengths, add a length member in your LISTA structure. But TAMANOMAX should absolutely be the same everywhere, else you're in fact working with different data types and your program won't work.
Using mismatched format specifier with the type of the argument invokes undefined behavior.
Quoting C11 standard, chapter ยง7.21.6.1, fprintf() function
[..] If any argument is
not the correct type for the corresponding conversion specification, the behavior is
undefined.
and about %d format specifier,
d,i The int argument [....]
so, %d expects an int.
As per your question title, ultimo is unsigned short int and you're using %d to print the value.
You should use %hu to print unsigned short int.
What will be output if you will compile and execute the following c code?
void main(){
int huge*p=(int huge*)0XC0563331;
int huge*q=(int huge*)0xC2551341;
*p=200;
printf("%d",*q);
}
Assuming you meant to write the following:
#include <stdio.h>
int main(void){
int *p=(int *)0XC0563331;
int *q=(int *)0xC2551341;
*p=200;
printf("%d",*q);
return 0;
}
then the output should be whatever integer value is stored starting at address 0xC2551341.
I'm not sure what int huge is supposed to represent; wider integer types are long int and long long int (or just long and long long). Note that these wider types use the %ld and %lld conversion specifiers for output, respectively.
I expect ouput something like \9b\d9\c0... from code below, but I'm getting \ffffff9b\ffffffd9\ffffffc0\ffffff9d\53\ffffffa9\fffffff4\49\ffffffb0\ffff
ffef\ffffffd9\ffffffaa\61\fffffff7\54\fffffffb. I added explicit casting to char, but it has no effect. What's going on here?
typdef struct PT {
// ... omitted
char GUID[16];
} PT;
PT *pt;
// ... omitted
int i;
for(i=0;i<16;i++) {
printf("\\%02x", (char) pt->GUID[i]);
}
Edit: only casting to (unsigned char) worked for me. Compiler spits warnings on me when using %02hhx (gcc -Wall). (unsigned int) had no effect.
The reason why this is happening is that chars on your system are signed. When you pass them to functions with variable number of arguments, such as printf (outside of fixed-argument portion of the signature) chars get converted to int, and they get sign-extended in the process.
To fix this, cast the value to unsigned char:
printf("\\%02hhx", (unsigned char) pt->GUID[i]);
Demo.
Use:
printf("\%02hhx", pt->GUID[i]);
Because printf() is a variadic function, its arguments are promoted to int. The hh modifier tells printf() that the type of the corresponding value is unsigned char and not int.
Cast to unsigned char instead, to avoid a leading 1 bit being interpreted as a negative value.
I noticed that you were getting the F's when the number was larger than 99x.
I wrote this to test it out and discovered the hh prefix at http://www.cplusplus.com/reference/cstdio/printf/
#include "stdio.h"
char GUID[16];
int main() {
int i;
for(i=0;i<16;i++) {
GUID[i]=i*i;
}
for(i=0;i<16;i++) {
printf("\\%02.2hhx\n", GUID[i]);
}
return 0;
}
Use a small own implementation to solve this problem on all platforms:
char hex[] = "0123456789abcdef";
void printHex(unsigned char byte) {
printf("%c%c", hex[byte>>4], hex[byte&0xf]);
}
The following is the code which I typed in c
unsigned long long int Je=23;
int col=2,row=2;
void mod(unsigned long long int mat1[][col],unsigned long long int mat2[][col],int r)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<col;j++)
{
mat1[i][j]=mat2[i][j]%Je;
printf("Value mat1=%u mat2=%u Je=%u\n",mat1[i][j],mat2[i][j],Je);
}
}
I call this function with the following matrix
t1[2][2]={1036,1090,1526,1472};
mod(t2,t1,row);
But in the console screen I get the following results for the "printf" statement I included inside the function definition
Value mat1=5 mat2=0 Je=1036
Value mat1=2 mat2=0 Je=1090
Value mat1=16 mat2=0 Je=1526
Value mat1=9 mat2=0 Je=1472
How come the values are printed like this? I am using Dev Cpp compiler.
The format specifiers "%u" in the printf statement are incorrect. For an unsigned long long, the specifier should be "%llu"
I have a number like 56789098.9899 and i want to use it in usleep() which accepts only an unsigned integer as argument.how to solve this problem?
sleeptime = time_alloted - time_taken.
Here sleeptime is an unsigned int variable.
First, usleep accept a usenconds_t arguement, and useconds_t doesn't have to be an unsigned int. See usleep spec here.
http://pubs.opengroup.org/onlinepubs/7908799/xsh/usleep.html
Second, note that the arguments must not larger than 1,000,000. And also remember to check the return value of usleep since it may fail.
Then just use a type cast will be fine enough.
it should work "as is"
So the following code works fine for me:
#include "stdio.h"
void func(unsigned int i)
{
printf("Going to sleep for %d\n", i);
usleep(i);
}
int main(void)
{
printf("hello world\n");
unsigned int sleep;
sleep = 56789098.9888 - 124234.45345;
func(sleep);
return 0;
}