Programming

Print Name in C: Alternatives to printf (putchar, puts)

Discover alternative ways to print your name in C without printf: putchar loops, puts/fputs, write calls, ASCII codes, macros. Fix common issues, pros/cons, and beginner tips for print c and output in c basics.

1 answer 1 view

Machine Problem 1 — Print your name in C

Create a program to print your name on the screen. You may add some info like course.

Desired output:

Name: Juan Dela Cruz
Course: BSIT

My code:

c
#include <stdio.h>

int main(){
 printf("Name: scythress");
 printf("Course: BSIT");
 return 0;
}

Is there any other approach to solve this in C (I know it’s trivial)? I’m interested in unique or alternative ways of coding this — for example using ASCII numeric codes, character arrays, puts/fputs, putchar in a loop, fwrite/write, macros, or other creative methods. Could you provide concise example implementations for several alternatives and briefly explain the pros/cons and which methods are appropriate for beginners?

Yes, you can print your name and course in C using alternatives to printf that give more control or teach low-level concepts, like a putchar loop for character-by-character print c output or puts for simple strings. Your current code mashes the lines together without newlines—add “\n” after each printf to fix it, but let’s explore unique ways to output “Name: scythress\nCourse: BSIT” with character arrays, ASCII codes, fwrite/write, macros, and more. These print c methods range from beginner-friendly to clever hacks, perfect for understanding вывод в c basics.


Contents


Fixing Your Original Code

Your code works but glues the lines: “Name: scythressCourse: BSIT”. Quick fix? Slap “\n” at the end.

c
#include <stdio.h>

int main() {
 printf("Name: scythress\n");
 printf("Course: BSIT\n");
 return 0;
}

Done. But why stop there? printf is handy, but alternatives reveal how print c really happens under the hood—like streaming bytes to stdout.


putchar Loop for Character-by-Character Printing

Ever wonder what printf does deep down? It spits out chars one by one. Recreate it with putchar in a loop over a char array. Super beginner-friendly since it mirrors string basics.

c
#include <stdio.h>

int main() {
 char name[] = "Name: scythress\n";
 char course[] = "Course: BSIT\n";
 
 for (int i = 0; name[i]; i++) putchar(name[i]);
 for (int i = 0; course[i]; i++) putchar(course[i]);
 
 return 0;
}

This traverses until ‘\0’. Stack Overflow discussions love this for custom print functions. Pros: Teaches null-terminated strings, no formatting overhead. Cons: Verbose for long text. Great for newbies grasping loops.


Using puts or fputs for Strings

puts is printf’s simpler cousin—dumps a string to stdout with an auto-newline. No format specifiers, just raw output. fputs skips the newline if you want.

c
#include <stdio.h>

int main() {
 puts("Name: scythress");
 puts("Course: BSIT");
 return 0;
}

Boom, newlines included. For no newline on the last line, mix fputs:

c
fputs("Name: scythress\n", stdout);
puts("Course: BSIT");

GeeksforGeeks compares puts as faster for static strings—no parsing needed. Pros: Clean, portable, secure (no format bugs). Cons: No variables without extra work. Ideal starter alternative to printf c.


Low-Level write System Call

Ditch stdio entirely. On Unix-like systems, write blasts bytes straight to stdout (file descriptor 1). POSIX magic, no buffers.

c
#include <unistd.h>

int main() {
 write(1, "Name: scythress\n", 15);
 write(1, "Course: BSIT\n", 12);
 return 0;
}

Count those bytes precisely! Quora threads swear by it for speed. Pros: Blazing fast, minimal overhead. Cons: Platform-specific (needs unistd.h), manual lengths, no null-term safety. Skip for Windows beginners—stick to stdio.

For buffered fwrite (more portable):

c
#include <stdio.h>

int main() {
 fwrite("Name: scythress\n", 1, 15, stdout);
 fwrite("Course: BSIT\n", 1, 12, stdout);
 return 0;
}

ASCII Numeric Codes

Hacky fun: Print via ASCII values. Convert chars to ints, loop putchar. Teaches encoding.

c
#include <stdio.h>

int main() {
 int name_ascii[] = {78,97,109,101,58,32,115,99,121,116,104,114,101,115,115,10};
 int course_ascii[] = {67,111,117,114,115,101,58,32,66,83,73,84,10};
 
 for (int i = 0; i < 16; i++) putchar(name_ascii[i]);
 for (int i = 0; i < 13; i++) putchar(course_ascii[i]);
 
 return 0;
}

(10 is newline.) Pros: Obscure, embeds strings as data. Cons: Brittle—change text, rewrite array. Not beginner rec, but cool for puzzles. PuskarCoding demos similar loops.


Macros and Custom Functions

Macros hide printf like a Trojan horse. Or build reusable functions.

Macro way:

c
#define PRINT_NAME puts("Name: scythress")
#define PRINT_COURSE puts("Course: BSIT")

#include <stdio.h>

int main() {
 PRINT_NAME;
 PRINT_COURSE;
 return 0;
}

Custom func (char array style):

c
#include <stdio.h>

void print_str(const char* s) {
 while (*s) putchar(*s++);
}

int main() {
 print_str("Name: scythress\n");
 print_str("Course: BSIT\n");
 return 0;
}

Quora suggests this for “no printf” technically. Pros: Modular, extensible. Cons: Macros can bite (no type safety). Beginners: Use functions over macros.


Creative Tricks: No Semicolons or Loops

GeeksforGeeks-style hacks. Print without semicolons by exploiting printf’s return (char count >0):

c
#include <stdio.h>

int main(){ if(printf("Name: scythress\nCourse: BSIT\n")){} }

Or no loop conditionals:

c
#include <stdio.h>

int main(){ char* s="Name: scythress\nCourse: BSIT\n"; while(putchar(*s++)); }

Puts returns non-zero, loops until ‘\0’. GeeksforGeeks article has more. Pros: Mind-bending interviews. Cons: Unreadable, non-portable. Avoid unless golfing code.


Pros, Cons, and Beginner Recommendations

Method Pros Cons Beginner-Friendly?
putchar Loop Teaches strings/loops Verbose Yes
puts/fputs Simple, fast No formatting Yes
write Speedy, low-level Unix-only, manual bytes No
ASCII Codes Embedded, stealthy Error-prone Maybe
Macros/Functions Reusable Macro pitfalls Functions: Yes
Tricks Clever Unmaintainable No

Start with puts or putchar—builds fundamentals without frustration. As you level up, try write for performance. All compile with gcc, run anywhere stdio works.


Sources

  1. Hello world in C without printf?
  2. Any other way to print on the screen instead of printf?
  3. Print our name in C without printf/puts
  4. Print Hello World without printf
  5. Printing a string in C using a function
  6. Print without semicolon
  7. puts vs printf
  8. puts vs printf for strings

Conclusion

Exploring print c alternatives like putchar loops or puts sharpens your C skills way beyond basic printf—your “Name: scythress\nCourse: BSIT” now has endless flavors. Beginners, prioritize puts for simplicity; pros, dive into write for efficiency. Experiment, compile often, and you’ll master вывод в c nuances. What’s your favorite hack?

Authors
Verified by moderation
Moderation
Print Name in C: Alternatives to printf (putchar, puts)