Prob.


9 replies [Last post]
raunaqrox
raunaqrox's picture
Offline
Joined: 2012-06-24
Points: 0

#include <stdio.h>

 

int main()

{

    char listOfCharacters[50];

int num = 8675309;

sprintf(listOfCharacters, "%d" , num);

printf("Our list of characters are : %s",listOfCharacters);

return 0;

}

 

In this , i didnt understand the square bracket part in char - whats the use of it

and what does 'sprintf' do , %s - what is this aswell . 

 

Thanks in advance .

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
lalorobot
lalorobot's picture
Offline
Joined: 2012-01-05
Points: 0

the square brackets in the char are to make it a string not a single character and the %s is to print the string and sprintf..... I dont remember XD gotta review that part

raunaqrox
raunaqrox's picture
Offline
Joined: 2012-06-24
Points: 0

so 50 is string lengh? or something else

cbrad
cbrad's picture
Offline
...burning like a watchful eye...I am a moderatorI've been here since the first tree. I am betaMy name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!I don't know when to shut up.We propose a toast! To you!I found a bug so fix it!
Joined: 2011-06-14
Points: 2785

by saying

 char listOfCharacters[50];

you are telling the compiler that 
listOfCharacters is an array of char types that will hold up to 50 characters.

in c and c++ the %s, %d are all placeholders for string or digit data - revise the syntax from the C videos 

(http://www.wibit.net/sites/wibit.net/files/programminginc_-_04_-_output_to_the_console.pdf)

the sprintf loads the digits of the int into the character array. The printf then prints it.

__________________

Cheers,

cbrad.)

raunaqrox
raunaqrox's picture
Offline
Joined: 2012-06-24
Points: 0

Thanks a lot . One last question , what all can i do with sprintf . i am not clear about it,havent seen any discussion so far on it in videos .

Kevin
Offline
When things break I did it. I am an admin!Enjoy my soothing baritone.Completionist. I am better than you.My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!Boot up or shut up!I don't know when to shut up.I'm not a fanboy!
Joined: 2011-03-20
Points: 2570

Great question! This is something that we didn't cover very well in our course. Not exactly sure why we overlooked this.. Perhaps we should go back and add it...

sprintf is great and it is often used in a lot of languages. Programmers will call this "S-Print-F" or "Sprint-F" of "String Print-F", and it is used as a String Formatter. You can store a string format result into a character array instead of printing to STDOUT (like in printf). Check out this code:

#include <stdio.h>

int main() {
	printf("%s %s", "Hey", "Buddy");
	return 0;
}

Above we are using printf to send a format string of "%s %s" to STDOUT and replacing the %s with two params: "Hey" and "Buddy". Therefore, the command prompt will say:

Hey Buddy

Now, take a look at this code:

#include <stdio.h>

int main() {
	char buffer[256];
	sprintf(buffer, "%s %s", "Hey", "Buddy");
	puts(buffer);
	return 0;
}

Above we are using sprintf to store the exact same result into a char[], and then I am printing that result out to STDOUT using puts.

Does this answer your question?

raunaqrox
raunaqrox's picture
Offline
Joined: 2012-06-24
Points: 0

ok thanks . Yes it does answer it. 

how in ur code the syntax is highlighted while mine isnt?

Kevin
Offline
When things break I did it. I am an admin!Enjoy my soothing baritone.Completionist. I am better than you.My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!Boot up or shut up!I don't know when to shut up.I'm not a fanboy!
Joined: 2011-03-20
Points: 2570

If your browser supports it, our forums rich text formatter has an icon with a yellow highlighter. This is the Synrax Highlighter. Click on that, paste or type your code in the text box and it will be formatted just like mine!

ashish_zarekar
ashish_zarekar's picture
Offline
Joined: 2013-04-26
Points: 0

i am on this place. i don't know why i am getting error " 'variable.exe' is not recognized as internal or external command, operable program or batch file".
any athor program work but i don't know why this is unable to run, and i notice that somtime when i put "\n" in program at that time it does not work and when i remove "\n" it work well. why ? any suggestion ????? 

nSharbz
nSharbz's picture
Offline
Joined: 2013-03-27
Points: 0

check to make sure your in the right directory and you spelled everything correctly. try typing "dir" in the command promt to see if your there otherwise just look for the executable in your search or just check the folder manually.

as far as the problem with newlines i couldnt say.

Hope  this could be of some help