Like Tree2Likes
  • 2 Post By Salem

a syntax error in expression near "if"

This is a discussion on a syntax error in expression near "if" within the C Programming forums, part of the General Programming Boards category; hey i m using code::blocks for c programming and when i take help of debugger in below code it show ...

  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,681
    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,681
    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,384
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    AndiPersti and Click_here like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04:55 AM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. Replies: 24
    Last Post: 09-06-2006, 06:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21