473,471 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Program output

I can't understand the output of the following function:

main()
{
char str[]="part-time musicians are semiconductors";
int a=5;
printf(a>10?"%50s":"%s",str);
}

Nov 15 '05 #1
6 4411
if a is grater than 10 the print only 50 chars
of the string, else print it all

Nov 15 '05 #2
It seems to print the entire statement in any case

Nov 15 '05 #3
I mean, if I make a=15 and do %20s instead of %50s, it prints the
entire statement rather than just first 20 chars

Nov 15 '05 #4
Meenu wrote:
I mean, if I make a=15 and do %20s instead of %50s, it prints the
entire statement rather than just first 20 chars


Well, of course - %20s means print using at least 20 chars width. Consider:

#include <stdio.h>
int main (void)
{
char s[]="String.";
printf("s is '%20s'\n", s);
return 0;
}

This outputs:
s is ' String.'

Perhaps the OP was also confused about the ? operator. It's like an
abbreiviated if - then - else and can be useful in printf in forms like:
printf("There were %d error%s\n", errors, errors == 1 ? "" : "s" );
--
Rob Morris: arr emm four four five (at) cam dot ac dot uk
Nov 15 '05 #5
Rob Morris wrote:
[Snip!]
Perhaps the OP was also confused about the ? operator. It's like an
abbreiviated if - then - else and can be useful in printf in forms like:
printf("There were %d error%s\n", errors, errors == 1 ? "" : "s" );

.... though one might wish to use slightly better grammar (D'oh!)

--
Rob Morris: arr emm four four five (at) cam dot ac dot uk
Nov 15 '05 #6
Meenu wrote:
I mean, if I make a=15 and do %20s instead of %50s, it prints the
entire statement rather than just first 20 chars


It is considered polite on Usenet to quote parts of the
posts you are replying to, and then write your comments
after it, so that a reader can see what your post is about,
without having to refer to any other posts.

Since you didn't do this, I'll fill in: you were replying to
your own post which went:
I can't understand the output of the following function:
main()
{
char str[]="part-time musicians are semiconductors";
int a=5;
printf(a>10?"%50s":"%s",str);
}


The answer is that "%15s" means: print a MINIMUM of 15
characters (but possibly more).
To specify a maximum you could write:
"%.15s" -- print a MAXIMUM of 15 characters
"%10.20s" -- print a MINIMUM of 10 and a MAXIMUM of
20 chars (right-justified)
"%-10.20s" -- as above, but left-justified.

Also your program has some other errors:
- it must #include <stdio.h> because it calls printf
- it should return a value from main
- main should be declared as: int main(void)

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
13
by: Liang Chen | last post by:
Hope you all had a nice weekend. I have a question that I hope someone can help me out. I want to run a Python program that uses Tkinter for the user interface (GUI). The program allows me to type...
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.