MikeyS9607
MikeyS9607
  • Member since: Feb. 18, 2007
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
I'm learning C Sep. 20th, 2012 @ 11:38 AM Reply

Indeed I am. I already know a bit of basic guff like ints and doubles, and even comparisons. Does anyone know any good but basic tutorials I could use?


MLP:FiM Club | My Site | YouTube | Ponies are awesome. <3. | PM Me | Sig by Skaren

BBS Signature
MikeyS9607
MikeyS9607
  • Member since: Feb. 18, 2007
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to I'm learning C Sep. 20th, 2012 @ 12:01 PM Reply

Alright. So I've made a basic program:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(void)
{
    
    char string[50];
    
    printf("HAHAHA! \n");
    
   getchar();
   
   scanf("%s", &string);
   
   printf("\"%s\", He says!", &string);
   
      system("PAUSE");
   
}

and it works for the most part, but it cuts out the first letter on the printf("\"%s\", He says!", &string); function. EG "ASS" comes out as "SS". What gives?


MLP:FiM Club | My Site | YouTube | Ponies are awesome. <3. | PM Me | Sig by Skaren

BBS Signature
MikeyS9607
MikeyS9607
  • Member since: Feb. 18, 2007
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to I'm learning C Sep. 20th, 2012 @ 12:03 PM Reply

SHIT! I had my editor set to C++. It still does that in C mode.


MLP:FiM Club | My Site | YouTube | Ponies are awesome. <3. | PM Me | Sig by Skaren

BBS Signature
pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 14
Programmer
Response to I'm learning C Sep. 20th, 2012 @ 12:43 PM Reply

I haven't gotten around to any C yet, but Stanford's Essential C (pdf) seems like a pretty decent start.


"If loving Python is crazy then I don't want to be sane." -Diki

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to I'm learning C Sep. 20th, 2012 @ 02:10 PM Reply

I cannot recommend Learn C the Hard Way enough.
It's one of the the best books I've found on C, and it's free.

The C Programming Language is another excellent book, and it's co-authored by Dennis Ritchie, the designer of the C language.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to I'm learning C Sep. 20th, 2012 @ 02:26 PM Reply

At 9/20/12 12:01 PM, MikeyS9607 wrote: and it works for the most part, but it cuts out the first letter on the printf("\"%s\", He says!", &string); function. EG "ASS" comes out as "SS". What gives?

Well you have more than one problem here.

First you're mixing C and C++. <iostream> is a C++ header, and you're not returning an integer at the end of main(). Only C++ allows you to exclude that; you must always return an integer in C.

Secondly the string is being printed incorrectly because, I am pretty sure at least, you're passing a pointer to the string rather than the string itself (that's that the & operator does).
To print a string you simply just do this:

#include <stdio.h>

int main()
{
    char* foo = "hello world";
    printf("%s", foo);
    return 0;
}

Or if you want to use an array instead of a char pointer:

#include <stdio.h>

int main()
{
  char foo[50];
  
  scanf("%s", foo);
  printf("%s", foo);

  return 0;
}

You can pretty much think of an array as a pointer. It behaves the same way.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to I'm learning C Sep. 20th, 2012 @ 02:29 PM Reply

Woo triple post (I'm fancy).

using namespace std;

This is also C++ code; there is no std namespace in C.

MikeyS9607
MikeyS9607
  • Member since: Feb. 18, 2007
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to I'm learning C Sep. 20th, 2012 @ 03:41 PM Reply

At 9/20/12 02:29 PM, Diki wrote: Woo triple post (I'm fancy).

using namespace std;
This is also C++ code; there is no std namespace in C.

Yeah, i know. Thanks for the help.


MLP:FiM Club | My Site | YouTube | Ponies are awesome. <3. | PM Me | Sig by Skaren

BBS Signature
Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to I'm learning C Sep. 21st, 2012 @ 11:10 PM Reply

At 9/20/12 02:29 PM, Diki wrote: This is also C++ code; there is no std namespace in C.

No namespaces in C either.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to I'm learning C Sep. 21st, 2012 @ 11:19 PM Reply

At 9/21/12 11:10 PM, Redshift wrote: No namespaces in C either.

Ah, that's right.
I'm not as well versed in C as I should be and that slipped my mind. :)

NickJohnson
NickJohnson
  • Member since: Aug. 30, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to I'm learning C Sep. 26th, 2012 @ 02:50 AM Reply

The book is " The C Programming Language " by "Brian W. Kernighan, Dennis M. Ritchie"......
Very good book......

Trunks
Trunks
  • Member since: Jul. 31, 2005
  • Offline.
Forum Stats
Member
Level 22
Musician
Response to I'm learning C Oct. 9th, 2012 @ 08:15 AM Reply

Applications Programming in ANSI C, by Johnsonbaugh & Kalin.

This book. This book is your best friend.


Check out my band by clicking my sig!

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to I'm learning C Oct. 9th, 2012 @ 10:12 AM Reply

At 9/20/12 02:10 PM, Diki wrote: Learn C the Hard Way

I'm starting to see a pattern, here. Same author as "Learn Python the Hard Way" and "Learn C++ the Hard Way"?


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
m0lecule
m0lecule
  • Member since: Aug. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to I'm learning C Oct. 9th, 2012 @ 03:48 PM Reply

At 10/9/12 10:12 AM, egg82 wrote:
At 9/20/12 02:10 PM, Diki wrote: Learn C the Hard Way
I'm starting to see a pattern, here. Same author as "Learn Python the Hard Way" and "Learn C++ the Hard Way"?

Afraid so...

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to I'm learning C Oct. 9th, 2012 @ 04:51 PM Reply

At 10/9/12 10:12 AM, egg82 wrote: I'm starting to see a pattern, here. Same author as "Learn Python the Hard Way" and "Learn C++ the Hard Way"?

Yep. Same author; he gives all his books the same title.

At 10/9/12 03:48 PM, m0lecule wrote: Afraid so...

"Afraid so"? You say that like it's a bad thing.