Thread: a syntax error in expression near "if"

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    14

    Angry a syntax error in expression near "if"

    hey i m using code::blocks for c programming and when i take help of debugger in below code it show ..
    a syntax error in expression near "if"..
    i m just checking debugger ...i m begginer .....
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
     char s1[100];
     int i;
     puts("enter s1");
     gets(s1);
     for(i=0;s1[i];i++)
     {
      if(((s1[i]<65)||(s1[i]>90))&&((s1[i]<97)||(s1[i]>122)))
      {
       strcpy(s1+i,s1+i+1);
       i--;
      }
     }
     puts(s1);
        return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The code itself is ok. Are you sure you have all the right compilers and settings? Can you run a "hello world"?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    After running it, read why gets is bad and should never be used!
    fgets on the other hand is just fine
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I just compiled it in code::blocks with no problem

    Have you got any other projects open at the same time that could be getting compiled by accident instead?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    Quote Originally Posted by Click_here View Post
    I just compiled it in code::blocks with no problem

    Have you got any other projects open at the same time that could be getting compiled by accident instead?
    the code is running fine ...no problem with code but why its show error when i take help of debugger.....

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    is anyone here can help me to understand debugger...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well perhaps you should post a more accurate error message (from the debugger).

    Apart from using gets(), the other issue the code has is your use of strcpy.
    > strcpy(s1+i,s1+i+1);
    strcpy is UNDEFINED when copying overlapping strings - like copying one part of a string to another part of itself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    1
    What is the objective of your code naresh? No problem in string functions. But check your logic, because your loop is stopping you from what you want to do. 'Hint: Check line 11 & 13 again for logic. And esp Line 15 & what follows afterwards doesn't stay in the loop, you jump out of the if condition. Also, notice the data type for s1? Compare it with what you're doing by incrementing that data type with the data type 'i'? what happens when you do that within strcpy? that's for strings. did you mean s1[I] & s1[I+1]?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-09-2012, 12:48 PM
  2. Replies: 3
    Last Post: 12-09-2009, 05:55 AM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. Replies: 24
    Last Post: 09-06-2006, 06:17 PM