Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (VI)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by ChemDog » Thu May 31, 2012 9:50 pm

m0skit0 wrote:Wrong, the average is not correctly calculated and shown. Average could be a decimal. You're rounding it down using int.
Whoops, rounded means aren't very useful :oops:
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{
     char* s1 = "New\nLine\n";
     char* s2 = "Another string!";
     char* s4 = "And another string.";
     char* s5 = "Yet another string.";
     char s3[100];
     strcpy(s3, s1);
     strcat(s3, s2);
     strcat(s3, s4);
     strcat(s3, s5);
     unsigned int len_s3 = strlen(s3);
     float x = len_s3;
     float y = 4;
     float z = x / y;
     printf("%s and the length is: %u\n",s3 , len_s3);
     printf("The average length of these four strings is: %f\n", z);

     return 0;
}
[/spoiler]
m0skit0 wrote:Wrong. The program should be able to handle any string.
Right, I was thinking that seemed too easy to be right. This took me a while, but it seems good now.
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{
     char* s1 = ("I am me, you are you.");
     char s2[20];
     int s1len = strlen(s1);  		                //string 1 length
     int ssmelen = strlen(strstr(s1, "me"));     //sub string "me" length
     strncpy(s2, s1, s1len - ssmelen);	        //copy characters equal to the difference of s1len and ssmelen 
     strcat(s2, "me");
     printf("%s\n",s2);

     return 0;
}
[/spoiler]
Advertising
Last edited by ChemDog on Fri Jun 01, 2012 8:37 am, edited 1 time in total.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Fri Jun 01, 2012 6:23 am

ChemDog wrote:Whoops, rounded means aren't very useful :oops:
Still rounded down. Keep in mind that if you perform an operation with 2 given types, the operation will be done using that type. In this case, you're dividing 2 integers, thus the result will be integer. The you assign it to a float type, which will convert the integer into float, which will result in printing the same integer but all zeroes decimals.
ChemDog wrote:Right, I was thinking that seemed too easy to be right. This took me a while, but it seems good now.
I don't see it right. This is the output of your program:

Code: Select all

I am +@me
which is definitely wrong.

PS: please format your code ;)
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by ChemDog » Fri Jun 01, 2012 8:32 am

m0skit0 wrote:Still rounded down. Keep in mind that if you perform an operation with 2 given types, the operation will be done using that type. In this case, you're dividing 2 integers, thus the result will be integer. The you assign it to a float type, which will convert the integer into float, which will result in printing the same integer but all zeroes decimals.

That makes a lot of sense, thank you for clarifying that.
m0skit0 wrote:I don't see it right...

That does seem to be wrong, but I am not sure why. When I run the program I am left with just "I am me". Is it just luck that I am getting the right outcome on my computer?
m0skit0 wrote:PS: please format your code ;)
Sorry about that. From now on it will be formatted.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Fri Jun 01, 2012 9:40 am

ChemDog wrote:When I run the program I am left with just "I am me". Is it just luck that I am getting the right outcome on my computer?
It is definitely luck, or your compiler initializes data for you, but not all compilers do this. Since this can be hard to spot, I'll explain why this is happening:

Code: Select all

char s2[20]
Here you define s2 as a string, but you don't initialize it to anything. This means this 20 bytes buffer can contain any values. For example, here's what it looks like on my computer:

Code: Select all

"BN\\300w\\000\\032@\\000X\\377\\"\\000f\\032@\\000\\000\\032@"
It will be different on each different computer, even each time you run it on the same computer.

Then you do:

Code: Select all

strncpy(s2, s1, s1len - ssmelen);
You copy 5 bytes from s1 to s2. This will only modify the first 5 bytes of s2 buffer, leaving the rest unmodified:

Code: Select all

"I am \\032@\\000X\\377\\"\\000f\\032@\\000\\000\\032@"
Then you do

Code: Select all

strcat(s2, "me");
Which concatenates "me" at the end of s2. But where s2 ends? At first NUL character (0x00 byte) from start. Taking s2, the s2 string is actually:

Code: Select all

"I am \\032@\\00"
Thus "me" will be appended to this string, resulting in:

Code: Select all

"I am \\032@me\\00"
Hope this clears things up. Now it's up to you to fix it! :D
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by ChemDog » Fri Jun 01, 2012 6:21 pm

Thank you for breaking it down like that, this helped a whole lot.
My first thought to fix this was:

Code: Select all

strncpy(s2, s1, s1len - ssmelen + 2);           //copy characters equal to the difference of s1len and ssmelen and add two to account for "me"
basically just because it wasn't adding "me" to the end, but I thought this might still show random characters from the buffer. Then I started wondering why this hasn't happened when I concatenated other strings(probably my compiler like you said). Rereading your post:
m0skit0 wrote:Which concatenates "me" at the end of s2. But where s2 ends? At first NUL character (0x00 byte) from start. Taking s2, the s2 string is actually:
made me remember your string examples when you showed:
// Setting the buffer to all zeroes (ASCII code 0, NUL character)
memset(string2, 0, 100);
So this way there are no junk characters and they are NUL. So here is my fix:
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{
	char* s1 = ("I am me, you are you.");
	char s2[20];
	memset(s2, 0 , 20);                   
	int s1len = strlen(s1);                //string 1 length
	int ssmelen = strlen(strstr(s1, "me"));  //sub string "me" length
	strncpy(s2, s1, s1len - ssmelen + 2);    //copy characters equal to the difference of s1len and ssmelen and add two to account for "me"
	printf("%s\n",s2);

return 0;
}

[/spoiler]
So I'm thinking this should be done for any uninitialized char?

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by Wdingdong » Sat Jun 02, 2012 1:43 pm

ChemDog wrote:made me remember your string examples when you showed:
m0skit0 wrote:// Setting the buffer to all zeroes (ASCII code 0, NUL character)
memset(string2, 0, 100);
So this way there are no junk characters and they are NUL.
Very good idea :geek:
// Big thanks to people who share information !!!

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by ChemDog » Mon Jun 04, 2012 4:46 am

Wdingdong wrote:Very good idea :geek:
Thank you! I was pretty happy when it popped into my head :lol:

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Mon Jun 04, 2012 7:06 am

ChemDog wrote:So I'm thinking this should be done for any uninitialized char?
Depends on what you want to do with it, but it's always good to initialize memory (this goes for variables as well) to be sure what values it holds. Also you can do it in a different way. If you want, I'll challenge you to find another solution ;) Anyway such exercises are very good to get a good grasp of C and fully understanding how stuff works :)

PS: also C has a good benefit compared to other languages: it actually shows you how the computer works internally. I know other languages like LUA, Python, Java and the like are way more "friendly" and hide all this stuff, but internally they're still doing it the way C does 8-) That's why it is commonly said that if you don't know C (or assembly) then you can't call yourself a programmer :mrgreen:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by Wdingdong » Mon Jun 04, 2012 7:59 am

m0skit0 wrote: Anyway such exercises are very good to get a good grasp of C and fully understanding how stuff works
Yes, I totally agree.
So, please, can you give us atleast exercise questions for the succeeding topics of this tutorial? So that we can practise and you can solve our doubts. So, that some good C programmers will be added to this world(by you) :lol:
// Big thanks to people who share information !!!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VI)

Post by m0skit0 » Mon Jun 04, 2012 11:21 am

Sure, but for now there are no more lessons :mrgreen: I'm looking forward to make more now that there's some activity :ugeek:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming and Security”