can someone explain me the difference between the 2 statements...
1. printf("%c",count);
2. printf("%s",&count);
count is an integer & both the statements do the same thing : print the character associated with the value of count.
can someone explain me the difference between the 2 statements...
1. printf("%c",count);
2. printf("%s",&count);
count is an integer & both the statements do the same thing : print the character associated with the value of count.
The first one prints a single character of value count... the other produces a memory access violation.
I'm seeing a lot of "what is the difference" posts at the moment.
What is this - learn C through random "facts"?
Endianness - Wikipedia, the free encyclopedia
Now consider the various internal formats that an integer can be represented in, and the likely consequences of simply pointing at it and calling it a string.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
it doesn't produce memory address violation or any other error...
I'm new to programming & I stumbled upon this & i couldn't find any explanations in my book or from my teacher so i posted here.
Sorry to disappoint you Salem with a random question.
You are treating the bytes of the integer as if it were a null terminated string, but there is no guarantee that there is a null byte in the bytes of the integer, hence you may end up accessing memory that does not belong to you.Originally Posted by Tanu
Also, the argument should be a char*, not a pointer to some other integer type. Although this might not result in an compile error, your compiler may warn you of it if you compile at a sufficiently high warning level.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
let me give the details...
I'm using the "Turbo C++" compiler.
& i cam upon this when we were making a program that prints the alphabets A-Z & a-z...
here's the code :
both the statements work fine.Code:#include<stdio.h> #include<conio.h> void main() { int count=64; while(count<122) { count++; if(count>90 && count<97) continue; printf(" %c\t", count); or {printf(" %s\t". &count); } }
what i don't understand is how are they different?
I would suggest you stumble towards some other learning resource, because you're not going to learn a lot from the one you're using at the moment.
It is a mildly interesting effect, not deep and meaningful 'C' knowledge you can use elsewhere. If you presented this code in a work environment, you would get sacked (yes, it's that bad).
Did you read the link on endianess I posted?
If your integer (say 65, corresponding to 'A' in the ASCII character set) is encoded in two bytes as 0x41 0x00, then you "win" and you see what you see when you try to print it as a string.
But on some other machine, it would be encoded as 0x00 0x41 and you "lose", nothing is printed.
> I'm using the "Turbo C++" compiler.
Yeah, we kinda knew that when we all saw your location.
I would suggest you get familiar with some modern compilers. unless you want to be part of the 97% in my sig-link.
Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
smorgasbordet - Pelles C
Code::Blocks
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Try something like this:
( I wonder what will happen when a seg-fault happens inside an emulated 16-bit program ^_^ )Code:int count = -1; printf("%s", &count);
EDIT: The test would be more successful if you make "count" global...
Devoted my life to programming...
I'm not looking to learn allot of programming. I don't want deep & meaningful knowledge of C. I'm a student of Biology & in future I'm going to be studying Genetics or Medicine. But I have to clear this one paper in order to pass school. I read the wiki about "endianess" that you suggested & frankly i didn't understand anything.
I agree that its a bad code & won't present it in a job interview.
What code would you right to print the English alphabet in upper case followed by lower case?
Its my first week doing c-programming, & I don't know what a seg-fault is.
All you guys are doing is talking about some technical stuff that is far beyond the scope of my syllabus but no-one has actually addressed the presented problem.
Well...Originally Posted by Tanu
Code:#include <stdio.h> int main(void) { puts("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well I gave you a copy/paste answer, but apparently it didn't work.
> What code would you right to print the English alphabet in upper case followed by lower case?
How about you post your answer to this, rather than worrying about broken code and ideas you found randomly scattered on the web.
If this is ALL you have to do, then just do that much, ask here if you get stuck, and stop wasting everyone's time (including yours) on trivia which you have no interest in.
> All you guys are doing is talking about some technical stuff that is far beyond the scope of my syllabus
Yes, so cut to the chase and post your actual attempt at your actual homework.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
the code i posted is the actual code i wrote in class...
the objective of the class was to practice the use of "While loop" & to print the English alphabet.
i noticed that i could do it by using both the statements:
1. printf("%c",count);
2. printf("%s",&count);
so that got me curious.
Maybe I'm in the wrong place & this forum is not for newcomers like me.
@laserlight : you really think that is a better code to present in a work environment?
from what i understand
the second statement should not work as &count would represent the address of the variable count, & the ASCII character associated with it shouldn't be the same as the one associated with the value of count.
but either statement does the job, why???
You have gotten very good explanations how those two statements differ in this thread. You complain about the answers being too technical, but that is simply because your question cannot really be explained in a simple way without having a deeper knowledge about how C and memory access/management works.