Thread: conditions array, string

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    5

    conditions array, string

    I want to number (compute 10) into char for example [1] and conditions if it smaller return 1 else return 1. If you know what i mean? I select lenght char [1] and when i compute string 100+100=200 i want with conditions if else 0 for larger then char [1] and 1 for smaller. Thank you



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char src[] = "10 10";
        char *data = src;
        int pocet;
        int v;
        int compute = 0;
    
        while (sscanf(data, " %d%n", &v, &pocet) == 1)
        {
            compute += v;
            data += pocet;
            printf("cislo: %5d; compute = %5d; pocet = %5d\n", v, compute, pocet);
        }
    
        printf("compute = %d\n", compute);
    {
        char str[5]= {compute,'\0' };
        printf("compute = %ld\n", str);
    
        if (str > 10)
        printf("0 \n");
    else
        printf("1 \n");
    }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well,
    Code:
    char str[5];
    sprintf(str,"%d",compute);
    Saying strlen(str) > 2 is just the long way of saying compute >= 100.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin with conditions
    By sean_cantab in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2016, 11:31 PM
  2. if conditions
    By RyanC in forum C Programming
    Replies: 4
    Last Post: 08-12-2015, 06:29 AM
  3. if statement with 2 conditions
    By robando in forum C Programming
    Replies: 2
    Last Post: 04-12-2015, 04:18 AM
  4. if and while conditions
    By firehydrant in forum C Programming
    Replies: 12
    Last Post: 03-05-2011, 03:07 AM
  5. Makefile conditions for OS
    By Holymanus in forum C Programming
    Replies: 2
    Last Post: 01-07-2011, 08:54 AM

Tags for this Thread