Compilation in C - c

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.

Related

Cast: functions fun1 and fun2 should the same value but the output is different. Can you explain why is it so?

It seems like the functions fun1 and fun2 should return the same values but the outputs are different. Can you explain why is it so?
#include <iostream>
using namespace std;
long long fun1(int, int, int );
long long fun2(int, int, int );
int main(){
int l = 1039, b = 3749, h =8473;
cout<<"Volume is equal to "<<fun1(l,b,h)<<endl;
cout<<"Volume is equal to "<<fun2(l,b,h)<<endl;
}
long long fun1(int length, int breadth, int height){
long long volume = length * breadth * height;
return volume;
}
long long fun2(int length, int breadth, int height){
return (long long)length * breadth * height;
}
output:
Volume is equal to -1355615565
Volume is equal to 33004122803
First case:
You are calculating everything in int type, and then casting to long long. Unfortunately the result is too big to fit in an int, so you have an overflow. Even if you cast it to a bigger type, that is too late, the overflow has already occured.
Second case:
You are casting to long long before actually doing the calculation. Thus, the calculation will be done using implicit conversion from int to long long and the result does fit in your container (which is long long now)… No overflow, life is beautiful!

assign zero in unsigned short int in c

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.

Why won't this C program print the unsigned int?

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.

Incorrect results from function which is using unsigned long long int

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"

how to convert a double into int to use it as argument in usleep?

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;
}

Resources