Computer Science Canada

I am doing printf functions. What line is my constant string for syntax?

Author:  php111 [ Sat Jan 10, 2009 6:53 pm ]
Post subject:  I am doing printf functions. What line is my constant string for syntax?

Where is my constant string for syntax? I am getting errors? I am following printf functions. My code has to be right. I know it is right. Why am I getting syntax errors?


Here are the errors:


Quote:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

H:\Documents and Settings\baseball>gcc programming.c -o programming.exe
programming.c:1: error: syntax error before string constant
programming.c:1: warning: conflicting types for built-in function 'printf'
programming.c:1: warning: data definition has no type or storage class
programming.c:3: error: syntax error before string constant
programming.c:3: warning: data definition has no type or storage class
programming.c:5: error: syntax error before string constant
programming.c:5: warning: data definition has no type or storage class
programming.c:5:64: warning: no newline at end of file

H:\Documents and Settings\baseball>




code:
printf("I am a simple ");

printf("computer.\n");

printf("My favorite number is %d because it is first.\n", num);

Author:  OneOffDriveByPoster [ Sat Jan 10, 2009 9:16 pm ]
Post subject:  Re: I am doing printf functions. What line is my constant string for syntax?

php111 @ Sat Jan 10, 2009 6:53 pm wrote:
code:
printf("I am a simple ");

printf("computer.\n");

printf("My favorite number is %d because it is first.\n", num);
Looks like you got confused because you were using some interactive "C" environment that I won't name. What you have there is not a valid C program. You cannot have statements at the top level of your translation unit. You should have a function called main(). You can have statements in main().

Author:  wtd [ Sun Jan 11, 2009 12:53 am ]
Post subject:  RE:I am doing printf functions. What line is my constant string for syntax?

The "main" function is your C program's "entry point." It's where execution of code starts in any C program.

Author:  php111 [ Sun Jan 11, 2009 9:40 am ]
Post subject:  Re: I am doing printf functions. What line is my constant string for syntax?

I was following the code from the book. I did put in int main() I still get errors. I give my code. I also give the code from the book.



code:
int main()

printf("I am a simple ");

printf("computer.\n");

printf("My favorite number is %d because it is first.\n", num);



Here is the code from the book...



code:
printf("I am a simple ");

printf("computer.\n");

printf("My favorite number is %d because it is first.\n", num);

Author:  Vertico [ Sun Jan 11, 2009 11:01 am ]
Post subject:  Re: I am doing printf functions. What line is my constant string for syntax?

You are still missing necessary parts for this to work.

Go back to your book and read through it carefully, not just the small code snippets. Also look back at your previous thread near the end and see what you are missing.

Author:  [Gandalf] [ Sun Jan 11, 2009 3:53 pm ]
Post subject:  RE:I am doing printf functions. What line is my constant string for syntax?

Generally a function in C will look like:
code:
<type> <function name>(<parameters>)
{
     <statement a>;
     <statement b>;
     <statement c>;
     <return statement if type non-void>;
}

Now keep in mind that main is a function, and think about why your code isn't working.


: