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.


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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.