.

Newbie at C help

<<

cyber.spirit

User avatar

Sr. Member
Sr. Member

Posts: 356

Joined: Sun Feb 26, 2012 8:07 am

Location: in your heart!

Post Wed Jan 09, 2013 12:27 pm

Newbie at C help

Hi there
I'm learning C language and i am so new in this world. so i have two problems the first one is i need to print a char two times in a line without using printf twice and i used this code:

  Code:
#include <stdio.h>

int main(void)
{  
    char chr = 'a';
    printf("%2c\n", chr);
    return 0;
}


as you know the %2 won't print 'a' two times it will show it like this in output "  a" (with 2 spaces at first) and it is not bad i will need it too
but i need to declare a variable for it instead of %2 i need something like this:

  Code:
#include <stdio.h>

int main(void)
{  
    int i = 2;
    char chr = 'a';
    printf("%ic\n", chr);
    return 0;
}



but it is not correct so can you please resolve these two problems god is bless you
regards cyber.spirit
Last edited by cyber.spirit on Wed Jan 09, 2013 12:29 pm, edited 1 time in total.
ICS Academy Network Security Certified
<<

caissyd

User avatar

Hero Member
Hero Member

Posts: 894

Joined: Thu Dec 31, 2009 11:20 am

Location: Ottawa, Canada

Post Wed Jan 09, 2013 1:08 pm

Re: Newbie at C help

Hey,

You can do this in many, many different ways. Here are two working examples:

  Code:
#include <stdio.h>

int main(void)
{  
    char chr = 'a';

    for(int i=0; i<2; i++)
    {
   printf("%c", chr);
    }
   
    return 0;
}

Loops twice and there prints the value of 'chr' twice. Notice I removed the the new line character "\n" (which you can add later)

  Code:
#include <stdio.h>

int main(void)
{  
    char chr = 'a';
    printf("%c%c", chr, chr);
   
    return 0;
}

Another variation, without the loop.

I used to teach C and C++ at a College. So I would be happy to give you a hand!
OSCP, GPEN, GWAPT, GSEC, CEH, CISSP
(aka H1t.M0nk3y)
<<

cyber.spirit

User avatar

Sr. Member
Sr. Member

Posts: 356

Joined: Sun Feb 26, 2012 8:07 am

Location: in your heart!

Post Wed Jan 09, 2013 1:57 pm

Re: Newbie at C help

H1t M0nk3y wrote:Hey,

You can do this in many, many different ways. Here are two working examples:

  Code:
#include <stdio.h>

int main(void)
{  
    char chr = 'a';

    for(int i=0; i<2; i++)
    {
   printf("%c", chr);
    }
   
    return 0;
}

Loops twice and there prints the value of 'chr' twice. Notice I removed the the new line character "\n" (which you can add later)

  Code:
#include <stdio.h>

int main(void)
{  
    char chr = 'a';
    printf("%c%c", chr, chr);
   
    return 0;
}

Another variation, without the loop.

I used to teach C and C++ at a College. So I would be happy to give you a hand!




Hi there whats up? Man for my program maybe i need to print it more than 1000 times two was an exmaple so loop is better but i need to use another loop too so it may cause some confilict too any way what about the second one? Thx so much
ICS Academy Network Security Certified
<<

caissyd

User avatar

Hero Member
Hero Member

Posts: 894

Joined: Thu Dec 31, 2009 11:20 am

Location: Ottawa, Canada

Post Thu Jan 10, 2013 8:42 am

Re: Newbie at C help

There's a joke in French that, if I translate it word by word, it would say: "Could you be less clear? I understand too much..."  :D

So, could you please tell me what you are trying to achieve? I will be able to help you better.

Meanwhile, you can always have multiple loops, one within another. For example:
  Code:
#include <stdio.h>

int main(void)
{  
    char chrA = 'a';
    char chrB = 'b';
    char chrC = 'c';
   
    for(int a=0; a<4; a++)
    {
        printf("%c", chrA);
   
        for(int b=0; b<3; b++)
        {
            printf("%c", chrB);
         
            for(int c=0; c<2; c++)
            {
                printf("%c", chrC);
            }
        }
    }

    return 0;
}


Output:
  Code:
abccbccbccabccbccbccabccbccbccabccbccbcc

Try to see if you understand the output.

As you can see, you can have multiple loops within each others.
But let me know exactly what you are trying to do, so I can help you better.
OSCP, GPEN, GWAPT, GSEC, CEH, CISSP
(aka H1t.M0nk3y)

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

.
Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software