How we can allign and print the any statement in middle of the page using C ? | Sololearn: Learn to code for FREE!

0

How we can allign and print the any statement in middle of the page using C ?

I am writing the code using C language but I am not able to use and operator to allign in middle of the page like I want to print "hello world" in middle of the compiler which operator I use .\n is used for new line like what is used for this.

10/22/2021 7:52:46 AM

Shivani

13 Answers

New Answer

+3

Shivani this code may helpful :) https://code.sololearn.com/c1tACaP9P59N/?ref=app

+5

It's rather difficult to print string at the center of the console screen because centering calculation involves length of string, number of character per console screen line, and most importantly, the font used in the console screen. Even if you find a way to center a string in your console screen; if you run the code somewhere else, things may look different due to use of different font.

+3

You seem to be confused as to where you want to print. In your question headline you state that you want to print in the middle of the page but in the question body you state that you want print " in middle of the compiler". To horizontally and vertically centre one thing inside another you need to know the dimensions of both things. Since the C programming language standard has no concept of a screen or printer this means whatever code you write will be operating system dependent and require non standard code. There are no standard library functions that can obtain the dimensions of the text or the target medium. The Microsoft Windows API has functions such as GetTextExtentPoint32, GetTabbedTextExtent, GetTextExtentExPoint and GetTextMetrics to obtain information about strings and fonts. The GetClientRect function returns information about the client area of a Window. Determining the height and width of a printed page is complex as it involves the use of the printer driver selected, paper size, printable area, and user selected margins. I can see from your profile that you are from India. That probably means that you are using Turbo C. Turbo C is a non standard, 30 year old, obsolete, ms-dos compiler that has it's own non standard, ms-dos functions for handling such things. There are various functions available in Turbo C to help you do this but it's been almost 30 years since I last used Turbo C so my memory is a bit foggy on the details. Have you tried reading the online help that accompanies Turbo C?

+2

Shivani, For better results, please do search the web for this, cause I don't think I can suggest you an actual solution ...

+2

As per my knowledge in c cpp their is no standard function or method available which allow you to format your text in center . One function gotoxy () which allow to do this but its not standard function . In cpp we have manipulators which allow to do that or you can use string literals but you asking for c you can use \n and \t and some other formaters to do this

+2

Shivani, I'm glad to hear that you are not using Turbo C. However neither Dev C nor Visual Studio Code are compilers. Dev C++ is an Integrated Development Environment (IDE) that works with multiple C/C++ compilers while Visual Studio Code is an editor which can also be used with multiple compilers. So I'm still non the wiser as to which compiler you are actually using. Disregarding that there are no escape codes for centering text in the standard C/C++ libraries. Modern operating systems do not have fixed sized consoles like they did back in the days of ms-dos. If you don't know the size of the console then how are you going to centre text in it? The size of the console can even change while the program is running.

+2

You need not to worry noone will se your formatting just implement your logics its better to print asterisk or any other symbols instead of \t or \n

+1

But I am doing a assignment where I have to use C language to do like that

+1

printf("\t\t\t\t%s\t\t\t\t", "hello world" ) ;

0

Martin Taylor Lol! Being Indian doesn't mean that he is using turbo c++. I know that you are pissed of by turbo c questions asked by indians. But almost everyone doesn't search anything by themselves because of educational system in schools. Not just in India. If they were teaching Turbo c in schools of England just like indian schools, then you will also encounter same problem with these students too.

0

@Shail Murtaza, Since there are no standard functions to do this in C the answer is going to be os, and possibly compiler, specific. I stated that Shivani was "probably" using Turbo C because... 1. They are from India where this obsolete compiler is still taught in schools. 2. No modern C course would be asking you to centre text since C has no concept of a screen in terms if height and width. The stdout stream is a data stream not an array of characters. So it's a safe bet that the question is both Turbo C and MS-DOS specific. How about we ask Shivani what compiler they are using?

0

First I am not using turbo C I am using dev C or visual studio code for program And If I am a indian it's doesn't mean that I am using old version for coding

0

I am asking for sequence like \n, \t that's why I gave an example in description I am not sure which sequence is used because i forgot the think in C , their is a gap of 2 year that's why I am again started learning in C