Thread: How to print specific amount of characters per line

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    How to print specific amount of characters per line

    Hey, so I've just started programming in C and was wondering if you guys could enlighten me on how to print a specific amount of characters per line. In this program I want to display all the capital letters in ASCII, but I want it to be 10 letters per line; Here is what I have so far:
    Code:
    #include <stdio.h>  
    
    
    int main()  
    {  
        int i;  
    
    
        while ( i=65; i<=90; i++ )
        {
            for (i= 0; i < 10; i++)
            {
                printf("\n");
            }{
            printf("%c", i); 
        }  
    
    
        return 0;  
    }}
    Any feedback is appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suggest you add another variable called

    int numberOfCharactersPrintedOnThisLine = 0;

    Figure out
    - where to declare it
    - where to increment it
    - where to test it is equal to 10
    and so on.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    I can't get my head around it man... I'm trying to figure out how to connect my new integer to the i


    So this is what I did, and apparently it prints out the whole alphabet 10 times in 10 rows...

    Code:
    #include <stdio.h>  
    
    int main()  
    {  
        int i;
        int k=0; 
        for (k=0; k < 10; k++)
        {
            for ( i=65; i<=90 ; i++ )
                {  
            printf("%c", i); 
        }
        printf("\n");
    }
        return 0;  
    }
    Last edited by Yashiiro; 06-13-2016 at 10:54 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and apparently it prints out the whole alphabet 10 times in 10 rows
    What do you mean "apparently"?

    Apparently means you've been told that it does, and you believed the person who told you.

    Actually means you tested it yourself and found the observation to be true (or false).

    Try it with only one loop.
    Code:
    for ( i = 0 ; i < 26 ; i++ ) {
      printf("%d %d %c\n", i, i % 10, i + 65);
    }
    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.

  5. #5
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    Sorry, english is not my native language.
    I'll try out the loop you sent me, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-05-2014, 11:05 AM
  2. How to print number of characters per line
    By Slyvester Ping in forum C Programming
    Replies: 25
    Last Post: 03-30-2012, 03:11 AM
  3. help showing amount of whitespace and characters
    By mattz714 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2010, 03:13 PM
  4. how to print big amount of number?
    By zzatan in forum C Programming
    Replies: 9
    Last Post: 10-02-2009, 07:46 PM
  5. Reading files with an unkown amount of characters
    By Zahl in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2002, 02:04 PM

Tags for this Thread