Thread: Coding noob - Hello World?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Coding noob - Hello World?

    Im so super noobie at programming -

    Code:
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
    	const char text = Hello World;
    	char text[15];
    	printf("%c", text);
    	return 0;
    }
    What im trying to do in the simple bit of code is define a vairable at the top, in this case, I want the variable text to be "Hello". I then want to print this variable below.

    What am i doing wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int main(void)
    {
       const char* text = "Hello world";
       printf("%s\n",text);
       return 0;
    }
    look at the red parts
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spadez View Post
    What am i doing wrong?
    A bunch of things. You need to include stdio.h for printf:
    Code:
    #include <stdio.h>
    You must quote string literals:
    Code:
    "Hello World"
    You try to assign a string to a single char:
    Code:
    const char text =
    then you redefine "text" as something else
    Code:
    char text[15];
    And your printf template should contain %s, not %c.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Code:
    #include <stdio.h>
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
    	const char* text ="Hello World";
    	printf("%s", text);
    	return 0;
    }
    Thank you for the help. Can you explain what the * is for next to char? This is my code above. It compiles and works, is it now properly coded?

    Edit: Since there are no arguments - can i change int main to (void) instead of (int argc, _TCHAR* argv[])

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It means it's a pointer
    Cprogramming.com Tutorial: Pointers

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by spadez View Post
    Can you explain what the * is for next to char? This is my code above.
    char in C could store only 1 character, to build a string you need an array of characters
    "Hello world" is string literal - it means array of characters stored somewhere by the compiler, and you have only read access to it. Your text variable is a pointer to this string.
    you can look at the tutorial section on this site - it has a lesson about strings as well
    C Strings - C++ Tutorial - Cprogramming.com

    Edit: Since there are no arguments - can i change int main to (void) instead of (int argc, _TCHAR* argv[])
    yes - read FAQ CProgramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Thank you for the links, ive printed them off and im going to read though them now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C coding help for finding the weather... NOOB
    By philmagrill in forum C Programming
    Replies: 8
    Last Post: 04-18-2009, 03:46 PM
  2. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM