Jump to content

im new with C can someone help me with that?

Liam mallka

Why don't you just run it and find out? We're not here to do your homework.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

You need to initlize the variable a with an integer, which means add a value. a as it stands is uninitilized and has no value. You can therefore not use it.

 

Also please add return 0 at the end of every c main to end the program. There are other ways but this is best practice. It just shows that wenn your program ends with 0, than it ran successfully.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Sakuriru said:

just read whatever eight bits happen to be there, which could be anything

I assume C tries to interpret the unitilized bit value at the location as an integer? Because a string value would be an error

Link to comment
Share on other sites

Link to post
Share on other sites

int a;   defines a variable called a , but doesn't assign any value to it.   Some C compilers will auto initialize it with value 0, others won't, so when reading the value you'll simply read whatever was in computer ram where the a variable was put.

 

printf prints variables according to the pattern you give it ... %d means a decimal number,  so it will print whatever is stored in the 4 or 8 bytes the variable a reserved in memory (int size depends on compiler and architecture, could be 2 bytes on 16 bit microcontrollers, but usually it's 32 bits or 4 bytes)

 

the function doesn't return anything, and doesn't have to, because you used the void keyword in front of the function name.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Teddy07 said:

I assume C tries to interpret the unitilized bit value at the location as an integer?

Yep

3 hours ago, Teddy07 said:

Because a string value would be an error

I don't thinks so. A string in C is just a pointer.

#include <stdio.h>

int main(){    
  int a = 0x6c6c6568;
  int b = 0x6f77206f;
  int c = 0x00646c72;
  printf("\n%s", (char*)&a);

  int data[] = {0x6c6c6568, 0x6f77206f, 0x00646c72};
  printf("\n%s", (char*)&data);
  
  printf("\n%s", "hello world");
  
  return 0;
}

 

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, shadow_ray said:

Yep

I don't thinks so. A string in C is just a pointer.



#include <stdio.h>

int main(){    
  int a = 0x6c6c6568;
  int b = 0x6f77206f;
  int c = 0x00646c72;
  printf("\n%s", (char*)&a);

  int data[] = {0x6c6c6568, 0x6f77206f, 0x00646c72};
  printf("\n%s", (char*)&data);
  
  printf("\n%s", "hello world");
  
  return 0;
}

 

 

thanks man. Always good to keep learning. I was too lazy to test

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×