Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
How can I add a + as a String using C Language?

I am trying to generate numbers from 1-5 using for loop and display their sum. I am aiming to have an output that looks like this:

1 + 2 + 3 + 4 + 5 = 15

But instead, I get the output:

+ 1 + 2 + 3 + 4 + 5 = 15

    #include <stdio.h>

    void main()
    {   
        int a, sum = 0;
        for (a = 1; a <= 5; a++) 
        { 
            printf("+\t%d\t",a);
            sum = sum + a;
        }
        printf("=\t%d", sum);       
    }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is encountered very frequently whenever outputting lists separated by some value. The problem is that you have 5 values to output, but only 4 separators. If you don't do anything special, then you will output 5 separators which is exactly what's happening in your example. Even if you move the separator to after your value, you'll still have one too many.

The way I prefer to do it is this:

for (a = 1; a <= 5; ++a)
{
    if (a > 1) printf("\t+\t");
    printf("%d", a);
    sum += a;
}

The reason I prefer this approach, versus outputting some value outside of the loop, is because often the thing you're outputting is more complicated, possibly involving additional calculation or function calls and I don't like duplicating that code.

So, I only output a separator if I know that I'm about to output the other thing. That means, output a separator for every loop iteration except the first.

I also like doing it prefix-style because usually the condition for the first item in a loop is simpler than the condition for the last item. It's also compatible with a different approach involving a flag:

int first = 1;
for (a = 1; a <= 5; ++a)
{
    if (!first) printf("\t+\t");
    first = 0;
    printf("%d", a);
    sum += a;
}

There are many other ways you'll see this kind of pattern occur. And there may be various forms of optimizing it that reduce readability. But this approach is simple and easy to follow.

over 4 years ago · Santiago Trujillo Report

0

You can take pointer of type char and assign it an empty string as a separator for the first iteration and after printing first number assign the separator string, to the pointer, that you want to print between two number in further iterations.

Implementation:

#include <stdio.h>

int main (void) {   
    int a, sum = 0;
    char * str = "";

    for (a = 1; a <= 5; a++) { 
        printf("%s%d\t", str, a);
        sum = sum + a;
        str = "+\t";
    }

    printf("=\t%d\n", sum);       
    return 0;
}

Output:

# ./a.out
1   +   2   +   3   +   4   +   5   =   15

Additional:

  • Using void as return type of main() function is not as per standards. The return type of main() function should be int.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!