Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
magnusi5838yA file only containing the words Hello world will show the two words (among other things) when you try the file with a compiler. Or at least that‘s how I would do it. With gcc, the words will even have their own line 😀
-
@KartikShetty cool! I'll b honest i did verify on Net that printf does return a value so... Yea...
Btw, my faculty asked me once to do the same except that there shud b no semicolon in the main() function 😉 -
@noobSam use a macro...
#define HELLO_WORLD printf("hello world");
void main()
{
HELLO_WORLD
} -
magnusi5838y@Electrux printf does return a value. The printf family functions returns length of the resulting string
-
@Electrux, Here's another one find out the size of structure without using sizeof operator. (Just because you are a C/C++ enthusiast like me).
-
magnusi5838y@KartikShetty make an array of two, subtract addresses. maybe there is a better way with only one, but I cant figure out anything better. Or make a struct containing the struct, with an element above and below, subtract addresses and padding
-
@KartikShetty well... I dunno that... But i have an intuition that it has something to do with pointers perhaps?
Btw... Glad to find a C/C++ fellow 😉😊 -
@magnusi tried that not the correct solution. Gives an integer without computing the memory occupied by the last element in structure.
-
@Electrux Not that I know of maybe using pointers is possible but the solution is quite simple.
-
magnusi5838y@KartikShetty which solution? the second one? I think the element above and below should be something like char, where you know the size beforehand
-
@KartikShetty what if u create 2 pointers... Malloc one to the struct, immediately malloc the other to the struct... Get their hex addresses, convert to integer, and subtract?
-
@magnusi you dont have any control on the elements in the structure. Should work for any structure of any size.
-
@magnusi then ima go C++ style and use new operator 😁
PS. Lol cant believe i forgot that u need sizeof to use malloc 😂😂😂😂 -
magnusi5838y@KartikShetty I see, well, I meant making another, different structure, with three elements, first char, second struct whose size we need to know, third char. Optionally also make that structure packed
-
@magnusi OK now how do you calculate the size of this second structure to find out the size of first.
-
@KartikShetty ah... Lol well but wat about the solution? If ur using c++, it shud b correct shudnt it?
-
@KartikShetty oh i jst thought of something! What if i cast the struct to a character pointer and printf that? The return value shud b the number of characters?
-
@Electrux A structure can have int, long, float any datatype variable that too of n size. How would you do this without losing out value if it is possible.
-
@KartikShetty according to my thought, a character is 1 byte and all others r more than 1 byte and ofc not in decimals... So it will probably throw garbage in printf but number of characters = number of bytes therefore size of struct shud b retrieved correctly... Probably m wrong haha... Do tell the solution 😁
-
@magnusi Yup it does. Thought you were suggesting about using arrays. Great job.
-
magnusi5838y@KartikShetty thanks, I suggested two solutions in my first comment, this one and the one you revealed a while ago 😁
-
@magnusi @KartikShetty man these are awesome! Lol i was uselessly trying to delve into pointers haha... Btw... Whatsup with that (packed) thing in that code? I'll admit i never saw that before 😅
-
magnusi5838y@Electrux by default, compilers insert some spaces between struct members. The space is called padding. The packed attribute temoves any space between, the elements are directly next to each other
-
@magnusi Well yeah
Find out the mid of a linked list when you dont know its size using just one loop without any other form of control transfer like recursion for that loop. -
Well... U 2 r on a whole different level... So much experienced... Someday i'll b like u 2 😊
-
Lucurious368y
Related Rants
Lets check out the dev in you.
Problem: Print "Hello World" in C in such a way that you cannot use any semicolon anywhere in the program.
Try this without using internet.
Hint: You only need to know basics of C.
undefined
coding challenge
devs
problem