-1

Something like this, I'd like to see the full syntax.

Pseudo Code:

var = user_input

if var > 5:
    output = 'var > 5'
else:
    output = 'var < 5'
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • 13
    Your example is already pretty close to C. You are asking a lot of very basic questions about the language - have you tried searching the web for a tutorial or something? – Carl Norum Sep 16 '10 at 04:15
  • 3
    Note that the 'else' clause misdescribes the value when `var` is equal to 5. – Jonathan Leffler Sep 16 '10 at 04:32

6 Answers6

7

How about something along the lines of:

#include <stdio.h>
#include <string.h>

int main (void) {
    int var;
    char buff[100];

    printf ("Enter number> ");
    fflush (stdout);
    if (fgets (buff, sizeof(buff), stdin) == NULL) {
        printf ("\nfgets() failed\n");
        return 1;
    }
    if (sscanf (buff, "%d", &var) != 1) {
        printf ("\nsscanf() failed\n");
        return 1;
    }

    if (var > 5)
        printf ("%d is greater than 5\n", var);
    else
        printf ("%d is less than 6\n", var);

    return 0;
}

with a couple of test runs:

pax> testprog
Enter number> 99
99 is greater than 5

pax> testprog
Enter number> -5
-5 is less than 6
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

This seems to correspond to what you'd like:

int var;
scanf("%d", &var);

if (var > 5)
    printf("var > 5\n");
else
    printf("var <= 5\n");

With caveats about using scanf() - I generally don't like it for resilient code, but it gives a minimal answer swiftly.

You'd have to wrap it in a main() and #include <stdio.h> to make it executable:

#include <stdio.h>
int main()
{
    int var;
    scanf("%d", &var);
    if (var > 5)
        printf("var > 5\n");
    else
        printf("var <= 5\n");
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2
  1. Variables and datatypes
  2. Scanf
  3. Conditional statements

Hope this will help you get started .

Praveen S
  • 10,355
  • 2
  • 43
  • 69
1

In addition to the other two answers, there's always the ternary operator ?: which can be used like this:

printf("var %s 5\n", var > 5 ? ">" : "<=");
jer
  • 20,094
  • 5
  • 45
  • 69
0
if(condition)
    doThis();
else
    doThat();

It's pretty much what you've got there. Your example:

if(var > 5)
    output = "var > 5";
else
    output = "var < 5";

The only difference is that you need semicolons after the statements and parentheses around the conditional expression, and the colons are not required.

You can also use curly braces to denote a block of commands to execute given a certain condition. When there's only one line being executed, however, the braces are not necessary. But this is equivalent to:

if(var > 5)
{
    output = "var > 5";
}
else
{
    output = "var < 5";
}

You can have braces just after the if or just after the else, or both, or neither. Remember, though, that with multiple statements the braces are required.

It's also worth noting that the line breaks are optional. This could be written

if(var > 5) output = "var > 5";
else output = "var < 5";

Or even

if(var > 5) output = "var > 5"; else output = "var < 5";

But this code is far less readable. The first and second forms are better practice.

Ian Henry
  • 22,255
  • 4
  • 50
  • 61
0
char var = getchar();

if (atoi(var) > 5)
{
  printf("var > 5 \n");
}
else
{
  printf("var < 5 \n");
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426