Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [ C ]: Stringhe e "printf"


    Salve a tutti,
    c'è per caso qualcuno tra voi che mi saprebbe spiegare questo comportamento strano del C?
    Ora vi illustro...

    Io carico un vettore di caratteri con un ciclo for e la getchar() inserendo alla fine anche il terminatore '\0';
    ...fin quì tutto bene...
    Quando inserisco una printf con lo specificatore di formato %s riferito a quella stringa, la prima volta mi viene stampato solo il contenuto dopo un'eventuale new line '\n' contenuto nella stringa!

    Tra l'altro questo problema non lo fa se prima della stringa stampo qualcos'altro!

    Vi mostro il codice che sarà sicuramente più chiaro della mia spiegazione:
    codice:
    #include <stdio.h>
    
    main()
    {
    
        char c, stringa[20] ;
        int i ;
        
        for ( i = 0 ; i < ( 20 - 1 ) && ( c = getchar() ) != EOF ; i++ )
            stringa[i] = c ;
        
        stringa[i] = '\0' ;
        
        printf("%s", stringa ) ;
        
        printf("\nSeconda stampa:\n%s", stringa ) ;
    
    }
    P.S: Provate a caricare la stringa con un'invio in mezzo e terminate con CTRL Z...
    esempio:
    Groove'\n'
    Man'ctrl Z'
    GrooveMan

  2. #2
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    a me non da alcun problema...
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  3. #3
    Ciao pdpmpd,
    mi sta venendo il dubbio che dipenda dal compilatore...
    tu che compilatore usi?
    GrooveMan

  4. #4
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    CCompiler della GNU Compiler Collection v 3.2.2
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  5. #5
    Scusa se avevo temporaneamente "mollato" la discussione.

    Il problema dipendeva proprio dal compilatore ( DevC++ 4.9 ).

    Con il Borland questo problema non si pone.

    Grazie e ciao!
    GrooveMan

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.