ADVERTISEMENT
ADVERTISEMENT

Home » C solved programs

C program to print string (your name) using different methods.

In this program we will learn how to print string or your name using different methods in c programming language?

In this program we will print string by using printf(), puts() and character by character.

C program to print string using printf()

printf() is a library function which is declare in stdio.h it is used to print string along with the values of the variables on output screen.

Here we will print the string using printf()

/*C program to print string using printf()*/

#include <stdio.h>

int main(){
	
	char name[]="Alvin Alexander";
	
	printf("name is %s.\n",name);
	
	return 0;
}

Output

    name is: Alvin Alexander.

C program to print string using puts()

puts() is a library function which is declare in stdio.h it is used to print string values to the output screen.

ADVERTISEMENT

Here we will print the string using puts()

/*C program to print string using puts()*/

#include <stdio.h>

int main(){
	
	char name[]="Alvin Alexander";

	printf("name is: ");
	puts(name);
	
	return 0;
}

Output

    name is: Alvin Alexander

C program to print string character by character

Here we will not using either printf() or puts() to print the string variable, we are using character by character printing to print the string.

We will run a loop from 0 to NULL and print the character by character.

/*C program to print string character by character*/

#include <stdio.h>

int main(){
	
	char name[]="Alvin Alexander";
	int i;

	printf("name is: ");
	for(i=0;name[i]!='\0';i++)
		printf("%c",name[i]);
	
	printf("\n");
	
	return 0;
}

Output

    name is: Alvin Alexander
ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.