How can we use multiple "\n" in c programming - c

In Python,we're using print( "\n" * 15). I wonder that how can we do this in c programming without printf("\n\n\n....\n");. Is there any function for it?

C does not have a string repetition operator, but it does have a function you can use:
char newlines[16];
memset(newlines, '\n', 15); /* <-- this function */
newlines[15] = '\0';
fputs(newlines, stdout);

You can just use a loop:
for (int i = 0; i < 15; i++) {
printf("\n");
}

You can make your own function.
int PrintLines(int n)
{
int x;
for( x =0 ; x<n ; x++ )
{
printf("\n");
}
return 0;
}

Always you can make a loop between the number of new lines you want and make a print.
for(int x=0; x<15; x++){
printf("\n");
}

Call fputc() in a loop.
while (n>0) {
n--;
fputc('\n', stdout);
}

Use "precision". Practical for up to a small number (e.g. up to 20 below)
int precision = 15;
printf("%.*s", precision, "\n\n\n\n\n" "\n\n\n\n\n" "\n\n\n\n\n" "\n\n\n\n\n");

Related

Use of "while" instead of "for" in C

Hello I'm new to programming, and would want to return an array in a "while" condition (not use "for"), any tips to make my program working please? Thanks
int numberPlayer=0;
int *listPlayers= NULL;
int i;
printf("How many players");
scanf("%d",&numberPlayer);
listPlayers= malloc(sizeof(int) * numberPlayer);
if (listPlayers==NULL){
exit(1);
}
i=0;
while(i<numberPlayer){
printf("Joueur n° %d", i*3);
listPlayers[i]= i*3;
i++;
}
while(i<numberPlayer){
printf("%d", listPlayers[i]);
i++;
}
I think you should learn how to use function. return array pointer is available for function not for commands like while, for.
you can check this https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm
Your example is a perfect illustration of the superiority of the for loop over the while loop. It is a shame some schools insist on students using only while loops (42, Epita, Epitech...)
You forgot to initialize i = 0 before the second while loop. A classical for loop combines initialization, test and increment in the for clauses, making it easy to read and verify.
For proper output, you should use a separator between the numbers and output a newline.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int main() {
int numberPlayers = 0;
int *listPlayers = NULL;
int i;
printf("How many players? ");
if (scanf("%d", &numberPlayers) != 1) {
return 1;
}
listPlayers = malloc(sizeof(int) * numberPlayers);
if (listPlayers == NULL) {
return 1;
}
i = 0;
while (i < numberPlayers) {
printf("Joueur n° %d? ", i * 3);
if (scanf("%d", &listPlayers[i]) != 1)
return 1;
i++;
}
printf("Liste des joueurs: ");
i = 0;
while (i < numberPlayers) {
printf("[%d]", numberPlayers[i]);
i++;
}
printf("\n");
free(listPlayers);
return 0;
}

Why this for loop ends in the second index?

I wrote a program that it's duty is to read 20 numbers from user and put them in a list, after that it prints the value in array from bottom to starting point.
But program stops exactly after reading second value from input.
Source code :
#include <stdio.h>
#define N 20
int main(void)
{
int numbers[N];
int i;
for(i=0;i<N;i++)
{
scanf("%i", &numbers[i]);
}
for(i=N;i<0;i--)
{
printf("%i", numbers[i]);
}
return 0;
}
I use Dev-C++ 5.6.3 as my IDE and TDM-GCC 4.8.1 as my compiler. But I don't know exactly that is this an IDE related issue or not.
If you want the loop to count downwards, then this loop
for(i=N;i<0;i--)
starts at the wrong index, and fails the test condition. It should be
for(i = N - 1; i >= 0; i--)
If you want your second loop to count down, then
for(i=N;i<0;i--)
should be
for(i=N;i>0;i--)
or the loop will not execute, as i<0 is not true to start with.
and, as #WeatherVane pointed out in the comments:
scanf("%i", numbers[i]);
should be
scanf("%i", &numbers[i]);
as you need to pass a pointer to the integer you wish to fill in with the number that scanf returns.
There is some issue with the given below for loop.
for(i=N;i<0;i--)
{
printf("%i", numbers[i]);
}
return 0;
If you Want to print the array from bottom to starting point.
You can make some changes in the for loop.
Changes :
1- Change in loop while assigning the value to i :
i = N-1 -> as the size of array is 20 and array index starts with 0.
it will go 19 to 0 to print all 20 data values.
2- changes in the condition check in for loop :
i >= 0 as we printing the reverse array.
Correct for loop should be
for(i = N-1; i >= 0; i--)
{
printf("%i", numbers[i]);
}
for(i=N-1;i>=0;i--)
For n items in an array, last index will be n-1. you need to iterate from n-1 index to 0th index.
second loop must be
for(i=N;i>=0;i--)
so index will be from 19 to 0 ( 20 number )
Try This
#include <stdio.h>
#define N 20
int main(void)
{
int numbers[N];
int i;
for(i = 0; i < N; i++)
{
scanf("%i", &numbers[i]);
}
for(i = N ; i >= 0; i--)
{
printf("%i ", numbers[i]);
}
return 0;
}
Try this:
#include <stdio.h>
#define N 20
int main(void)
{
int numbers[N];
int i;
for(i = 0; i < N; i++)
{
scanf("%i", &numbers[i]);
}
for(i = N - 1; i >= 0; i--)
{
printf("%i ", numbers[i]);
}
return 0;
}
Remember that scanf always uses pointers.
Your second loop's condition, "i<0", is false to begin with. It should be "i > -1". You also need to make sure that the first time printf is called with index 19 and not 20. That is why I use pre decrement operator -- i.
for(i = N; i > -1; )
{
printf("%i\n", numbers[--i])
};

Why isn't this C code's \t working?

I am trying to print a pattern of numbers. Try running this code with a input of 20, you will see that the tab spaces are all in the wrong place, they don't follow the order. I know that the tab spaces jump to the next header, but is there a way to avoid it?
#include <stdio.h>
int main()
{
int i, n, count = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("%dnumber\t", i);
count++;
if(count == 4)
{
count = 0;
printf("\n");
}
}
return 0;
}
Note: Is there a way to do this with tab spaces only(i.e., "\t" only) instead of using an ordinary white space.
Here is the output that I am getting.
But what I want is this
There are several different problems here.
But it sounds like your main question is "gee: the tabs don't line up like I expect."
SUGGESTED ALTERNATIVE:
Consider using "field length specifiers" in printf():
http://www.cplusplus.com/reference/cstdio/printf/
EXAMPLE:
printf ("%-20s", mystring); // Will always be exactly 20 characters
printf ("%06d", myint); // 6-digits, zero filled
Why not:
#include <stdio.h>
int main( void )
{
int i = 0;
int n = 0;
scanf( "%d", &n );
for( i = 1; i <= n; i++ )
{
printf( "%02dnumber\t", i );
if( i % 4 == 0 )
printf("\n");
}
return 0;
}
Output:
16
01number 02number 03number 04number
05number 06number 07number 08number
09number 10number 11number 12number
13number 14number 15number 16number
Hope it helps!

How can I print multiple character with one printf?

I want to print multiple character using printf. My approach up to now is this-
#include <stdio.h>
int main()
{
printf("%*c\n", 10, '#');
return 0;
}
But this only prints 9 spaces before the #.
I want to print it like this-
##########
I am unable to figure out how to do this. Please help me?
You can not use printf like that to print repetitive characters in Ansi C. I suggest you to use a loop like this -
#include <stdio.h>
int main()
{
int i;
for(i = 0; i < 10; i++) putchar('#');
return 0;
}
Or if you have absolutely no desire to use loops, you can do something like this-
#include <stdio.h>
int main()
{
char out[100];
memset(out, '#', 10);
out[10] = 0;
printf("%s", out);
return 0;
}
By the way, using printf like this also works-
#include <stdio.h>
int main()
{
printf("%.*s", 10, "############################################");
return 0;
}
I think the best approach, if you have an upper limit to the number of characters to be output is:
printf("%.*s", number_of_asterisks_to_be_printed,
"**********************************************************************");
I think this will also be the most efficient, portable way to do that.
this will print ten # characters, followed by a newline
char tenPounds[] = "##########";
printf( "%s\n", tenPounds);
I am working on a similar problem in "The C Programming Language" book (exercises 1-13 and 1-14). My own program, to start simply, is to count the occurrences of the digits 0 to 9 in a given input, and print a horizontal histogram made of '=' bars according to each count.
To do this, I created the following program;
main() {
int c, ix, k;
int nDigit[10];
//Instantiate zero values for nDigits array
for(ix = 0; ix < 10; ix++) {
nDigit[ix] = 0;
}
//Pull in input, counting digit occurrences and
//incrementing the corresponding value in the nDigit array
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9') {
++nDigit[c-'0'];
}
}
//For each digit value in the array, print that many
//'=' symbols, then a new line when completed
for (ix = 0; ix < 10; ix++) {
k = 0;
while (k <= nDigit[ix]) {
putchar('=');
k++;
}
printf("\n");
}
}
Note that this is a work in progress. A proper histogram should include axes labels, and most importantly this program does not account for digits of zero count. If the input includes five 1's but no 0's, there is no visual way to show that we have no zeros. Still, the mechanism for printing multiple symbols works.

Looping through an Array in C with no output

Here is my code:
int main()
{
int tiles[9];
int counter=0;
int i=1;
while (counter<8)
{
tiles[counter]=i;
counter=counter+1;
i=i+1;
}
int running_total=0;
int current_number;
printf(tiles);
return 0;
}
But I get no output, what is my problem? I'm new to C so I appreciate any comments/criticism.
Edit: I do get an output, but it's a smily face...
If you want to print a number, you need a format string.
If you want to print an array, you need to loop through it.
int i;
for ( i = 0; i < sizeof(tiles)/ sizeof(tiles[0]); ++i)
printf("%d ", tiles[i]); // << added a space for Dietrich Epp :)

Resources