a little c language problem...

This is a discussion on a little c language problem... within the C Programming forums, part of the General Programming Boards category; the following program has an output of: 2 4 16 how can i make it appear like this: 2 4 ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    a little c language problem...

    the following program has an output of:

    2 4 16

    how can i make it appear like this:

    2 4 16 256

    this is the program:
    Code:
    # include <stdio.h>
    # define p printf
    # define s scanf
    main()
    {
    int x;
    clrscr();
    for (x=2;x<=30; x=x*x)
    {
    p ("%d\t", x);
    }
    getch();
    }

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    First of all main always returns int. program does not print 256 because of the loop condition x<=30. 256 is less than 30 so it does not enter the loop. Change the condition.

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    # include <stdio.h>
    
    // Functions have somewhat descriptive names for a reason,
    // please don't make this a habit. :)
    # define p printf
    # define s scanf
    
    int main( void )
    {
        int x;
        clrscr();
        for (x = 2; x <= 30; x = x * x)
        {
            p("%d\t", x);
        }
        getch();
    
        return 0;
    }
    To answer your question, the for loop executes only until x is less or equal to 30. You need to change the loop condition.

    Spaces are dime a dozen, please use them for increased readability.

    What compiler are you using?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Unless you are going to participate in IOCC, you SHOULD NOT be using such macro definition.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 02:11 PM
  2. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 02:20 AM
  3. Languages dying
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-29-2003, 11:08 AM
  4. Language Script..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-30-2003, 06:48 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 04:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21