Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1

    [C] quando è TRUE?

    se ho

    do{
    }while(scanf("%d",&a[i]));

    la while vedo che è TRUE se inserisco un numero naturale, se invece inserisco un * è FALSE...ma quando rende TRUE di preciso?

    Grazie.
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

  2. #2
    A giudicare dal prototipo della funzione, la risposta è mai !
    Header File

    stdio.h

    Category

    Input/output Routines

    Syntax

    #include <stdio.h>
    int scanf(const char *format[, address, ...]);
    int wscanf(const wchar_t *format[, address, ...]);

    Description

    Scans and formats input from the stdin stream.

    Note: For Win32s or Win32 GUI applications, stdin must be redirected.

    The scanf function:

    scans a series of input fields one character at a time
    formats each field according to a corresponding format specifier passed in the format string *format.
    vsscanf scans and formats input from a string, using an argument list

    There must be one format specifier and address for each input field.

    scanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely. For details about why this might happen, see When ...scanf Stops Scanning.

    Warning: scanf often leads to unexpected results if you diverge from an expected pattern. You must provide information that tells scanf how to synchronize at the end of a line.

    The combination of gets or fgets followed by sscanf is safe and easy, and therefore recommended over scanf.

    Return Value

    On success, scanf returns the number of input fields successfully scanned, converted, and stored. The return value does not include scanned fields that were not stored.

    On error:

    if no fields were stored, scanf returns 0.
    if scanf attempts to read at end-of-file or at end-of-string, it returns EOF.
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  3. #3
    returns the number of input fields successfully scanned
    Ritorna il numero di campi letti con successo.
    Siccome ha come direttiva %d gli stai dicendo di leggere un intero. Quindi se inserisci un intero ha letto con successo un campo. scanf restituisce 1 che quindi è true.
    Se metti un altro carattere che non sia un numero, il campo letto non è un intero e quindi scanf restituisce 0, cioè un false.


    ciao ciao

  4. #4
    perfetto:-) come posso uscire in modo "elegante"? Ovvero...l'utente può inserire quanti valori interi vuole...in questo modo smette se inserisce un nonnumero...potrei farlo uscire se da un invio a vuoto?
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

  5. #5
    codice:
    #include <stdio.h>
    #define MAX_A 10
    int main()
    {
    	int i=0, q, tmp, a[MAX_A];
    	printf("Inserisci max %d valori interi, (digita una lettera per terminare).\n", MAX_A);
    	while( scanf("%d", &tmp) && i<MAX_A)
    		a[i++]=tmp;
    	for( q=0; q<i; q++)
    		printf("%d,", a[q]);
    	getchar();
    	return 0;
    }
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  6. #6
    #define MAX_A 10

    perché si mette?
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

  7. #7
    Originariamente inviato da Vre
    #define MAX_A 10

    perché si mette?
    Definisce una costante globale che dichiara la dimensione massima del vettore.
    In questo modo modificando semplicemente quel valore, si modifica in un sol colpo
    sia la dimensione del vettore, che il controllo nel while(), in pratica durante la
    fase di compilazione tutte le occorrenze di MAX_A vengono sostituite con il
    valore assegnatogli. Evitando 'sgradevoli' errori, a volte difficili da rilevare.
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  8. #8
    ma se io non ho un massimo definito? L'utente potrebbe inserire anche 10^10 valori...
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

  9. #9
    Originariamente inviato da Vre
    ma se io non ho un massimo definito? L'utente potrebbe inserire anche 10^10 valori...
    Se non lo scrivi, io come posso saperlo questo ?
    Utilizza allora una variabile normale.
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  10. #10
    okok, scusa e grazie!!!!!!
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.