Thread: Basic syntax in C

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    44

    Basic syntax in C

    Hi,
    Behold my ignorance. I've got the following code that works:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	FILE *text = fopen("/home/heras/code/text", "r");
    	// stuff that works as expected
    But even with tons of brackets I can't get this to compile:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int main(void)
    {
    	if (FILE *text = fopen("/home/heras/code/text", "r") == NULL) {
    		perror("fopen: ");
    		exit(EXIT_FAILURE);
    	}
    Code:
    ## I get varying versions of the below, depending on where I put brackets: 
    readfile.c: In function ‘main’:
    readfile.c:7: error: expected ‘)’ before ‘text’
    readfile.c:7: error: expected expression before ‘==’ token
    readfile.c:16: error: ‘text’ undeclared (first use in this function)
    This FAQ shows a working solution, but I don't understand why mine doesn't. In another program I've got such a construction that does work:
    Code:
        if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket: ");
            exit(EXIT_FAILURE);
        }
    I must be making some basic syntax mistake(s) or something. What is wrong with my attempt at error handling?
    Thanks,
    heras
    Last edited by heras; 03-16-2008 at 07:14 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could write:
    Code:
    FILE *text;
    if ((text = fopen("/home/heras/code/text", "r")) == NULL) {
        perror("fopen: ");
        exit(EXIT_FAILURE);
    }
    You could also write:
    Code:
    FILE *text = fopen("/home/heras/code/text", "r");
    if (text == NULL) {
        perror("fopen: ");
        exit(EXIT_FAILURE);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    44
    Hi laserlight, thanks for your reply.
    I've found that second solution in one of the FAQs of this board and it works. But what I don't understand is why my socket program accepts this construction and my readfile program doesn't. Is there some difference in the types of the return values of sockfd() vs fopen() ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are two errors with the example of yours.
    Firstly, C89 will not allowed you to define variables in statements like that. All variables must be defined at the beginning of a function. Always. C99 and C++ allows this, but then the rule below would make it that you can only use it inside your if, so using it to check for error would be bad.
    Secondly, since it's defined within your if, the variable will also be destructed within the if due to block rules.
    So you can assign within and if, but you can't actually define a variable inside.

    Code:
    /* Invalid in C89; valid in C99 and C++ */
    if (int i = 0 == 0)
    	i = 1; /* Valid, because i exists within the if scope */
    i = 0; /* Invalid, because i no longer exists. */
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    44
    Quote Originally Posted by Elysia View Post
    All variables must be defined at the beginning of a function. Always. C99 and C++ allows this, but then the rule below would make it that you can only use it inside your if, so using it to check for error would be bad.
    Secondly, since it's defined within your if, the variable will also be destructed within the if due to block rules.
    So you can assign within and if, but you can't actually define a variable inside.
    Ah, I think I understand, thanks! I've noticed that compiling with gcc -std=c99 makes warnings go away when the previously undeclared i is used as follows:
    Code:
        for (i = 0; i < 10; i++) {
        ...
    It may work but probably bad form as well if I understand correctly.

    edit: and obviously the sockfd from the snippet it my first post was already declared as opposed to FILE *text.
    Last edited by heras; 03-16-2008 at 07:40 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're not actually defining any variable either.
    But if you did...
    Code:
    for (int i = 0; i < 10; i++)
    {
    	/* i is accessible throughout the for scope */
    }
    Then this is possible. The variable "i" can be accessed within the i scope, but will be destroyed when you leave the loop.
    Again, only works with C99 & C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    44
    Code:
       for (int i = 0; ...
    Oops, yes, that's what I meant to type. I'll stick to your suggestion though of always defining them first
    Thanks,
    heras

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM