Image
 
linkedin_logo.png rss_logo.jpg
twitter_logo.png youtube_logo.jpg
Latest Additions
 
EH-Net Login
Welcome Guest.






Lost Password?
No account yet? Register
Who's Online
We have 45 guests and 2 members online
 
Advertisement

You are here: Home arrow Ethical Hacking Discussions and Related Certificationsarrow Programmingarrow Newbie at C help
EH-Net
May 21, 2013, 08:59:57 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Go back to The Ethical Hacker Network Online Magazine Home Page
 
   Home   Help Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Newbie at C help  (Read 2307 times)
0 Members and 1 Guest are viewing this topic.
Cyber.spirit
Sr. Member
****
Offline Offline

Posts: 351


The World is sick, Save your mind...


View Profile
« on: January 09, 2013, 11:27:26 AM »

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 Edit: January 09, 2013, 11:29:56 AM by Cyber.spirit » Logged

ICS Academy Network Security Certified
H1t M0nk3y
Hero Member
*****
Offline Offline

Posts: 864



View Profile
« Reply #1 on: January 09, 2013, 12:08:25 PM »

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!

Logged

OSCP, GPEN, GWAPT, GSEC, CEH, CISSP
Cyber.spirit
Sr. Member
****
Offline Offline

Posts: 351


The World is sick, Save your mind...


View Profile
« Reply #2 on: January 09, 2013, 12:57:51 PM »

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
Logged

ICS Academy Network Security Certified
H1t M0nk3y
Hero Member
*****
Offline Offline

Posts: 864



View Profile
« Reply #3 on: January 10, 2013, 07:42:54 AM »

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..."  Cheesy

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.
Logged

OSCP, GPEN, GWAPT, GSEC, CEH, CISSP
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Joomla Bridge by JoomlaHacks.com
Valid XHTML 1.0! Valid CSS!
Page created in 0.09 seconds with 22 queries.
 
Exclusive Deal

sansfire13_245x90_cw90.jpg
SANSFIRE 2013
June 15 - 22

5% Off w/ Code: EHN_5

SANS Deals 4 EH-Netters
5% OFF Any SANS Course in Any Format!
Coupon Code: EHN_5 Including SANS Rocky Mountain 2013 & SANS Boston 2013
Polls
Compared to this year, 2013 will be:
 
Recent Forum Topics
EH-Net News Feeds
Latest Additions
 
         
Advertisement

© 2013 The Ethical Hacker Network
Joomla! is Free Software released under the GNU/GPL License.