From owner-freebsd-questions@freebsd.org Thu May 14 20:09:07 2020 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B52D72DD7EC for ; Thu, 14 May 2020 20:09:07 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.17.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass Class 2 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49NN0f4c0Wz3M6b for ; Thu, 14 May 2020 20:09:06 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de ([188.102.97.58]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPA (Nemesis) id 1MpUlO-1ijTg42aHU-00py9q for ; Thu, 14 May 2020 22:09:04 +0200 Date: Thu, 14 May 2020 22:09:04 +0200 From: Polytropon To: FreeBSD Questions Subject: Multi-line text output via printf() et al. Message-Id: <20200514220904.7a4c1e28.freebsd@edvax.de> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:pVw0yBdKoUqXUNM402cD/KtWUIpzgRU/oX3TzGmK/jWoZP/pCeu pzNnivtd6P5AFSkyRDC4jPV57C683tXGzlIV9BstMjKfFPdzJ0IG+Vt/829vhOlNaje+dmB SZg+J6tyLxpu0GoHxied6758SODiKhk4eU0KbXyb+FYJKVhaj/Z1osuIVc1yCyqA7MIiwXu +2iwoH4IWDLOt33P8VM8A== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:8rO/jRz+he8=:9al3CmM0CFjDkgbQFNwGOq pyDvmqjzLBdNS5D9LzzgK4LQAGNZwhfRQT1l2tQ/sghJDCXlmTu44igGze0jvqc8M+yIoXr8K gu1YTsYbzdljRS7rEzDhtCXX5wtUGkECzhoExPURdD39qTiaSHjxme1g5kTtpUGSjZCABfeQi ZgJUtvoEl3sdo6R2sVEYaI88OIM56ufRHOYK7ouawKfifTh7fGcg70mxxDbSeDpFokdqRaLVy BARc0Ca3/udC8c4vekhrdE+daMsv46Vy7dyzr5HlSlcM1dmRjXNNFhFx/h9RFfQxalvW939Hp AunWK2w1zEwWe43DAySLfx+AS6jZOziYa22cRpa1C7NON5GG2u951B54s7tOJMxu688uf4qEI SzQHMY8HLLypzw2K8TjlrKQx3LuoZ9/81RChgJs57QbNaMEBbuNGaFW+2e3C/P8buL+zdTm9X 8eJ8i9E8kHEfoQZsrk++t12owSJEgrmMSVc5ciuCT8c18vB/kv+HKQM8w2/qtQC/dvZ1fOx0q ml/sMlu+oLpFBKIXZPhvo5TRkGKGicJ1nmXkpr8kUV8CgXLyjdZ1+g/J1bSIEhoFbOm9QdiyE MI35007Ym3HEs9GuRpkzLtqCRRX/O8HT9uB4wyEgRd4+GxSTupiXczW1jgzcE9YgqIsVgNrtF r9AkV5Jh4/4TtK1RQuaNFWdKM5LPoBsyb7FBCtQij5YtvsPAQHYNOzTOiGM4Wwn1x583wbyFL A2Wx+xDqoZoNwTH2sNPkqZeG9U21glEZTgXZXm6pTTq5mn0iGKcIJQEPEcjlhkTuJpzvjkPPf X96fM8Mq8e7WObhbLVWcPNSMMusu69RZMjYYYwmBpbBND+bUEDuOauJD0sZpBhZO7IbMWYU X-Rspamd-Queue-Id: 49NN0f4c0Wz3M6b X-Spamd-Bar: +++ Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of freebsd@edvax.de has no SPF policy when checking 212.227.17.24) smtp.mailfrom=freebsd@edvax.de X-Spamd-Result: default: False [3.85 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; HAS_REPLYTO(0.00)[freebsd@edvax.de]; RWL_MAILSPIKE_GOOD(0.00)[24.17.227.212.rep.mailspike.net : 127.0.0.18]; MV_CASE(0.50)[]; HAS_ORG_HEADER(0.00)[]; TO_DN_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; R_DKIM_NA(0.00)[]; ASN(0.00)[asn:8560, ipnet:212.227.0.0/16, country:DE]; MIME_TRACE(0.00)[0:+]; RECEIVED_SPAMHAUS_PBL(0.00)[58.97.102.188.khpj7ygk5idzvmvt5x4ziurxhy.zen.dq.spamhaus.net : 127.0.0.11]; ARC_NA(0.00)[]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; DMARC_NA(0.00)[edvax.de]; AUTH_NA(1.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_SPAM_MEDIUM(0.40)[0.403,0]; IP_SCORE(0.05)[ip: (-0.51), ipnet: 212.227.0.0/16(-1.20), asn: 8560(2.00), country: DE(-0.02)]; NEURAL_SPAM_LONG(1.00)[0.996,0]; MID_CONTAINS_FROM(1.00)[]; RCVD_IN_DNSWL_NONE(0.00)[24.17.227.212.list.dnswl.org : 127.0.5.0]; R_SPF_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 May 2020 20:09:07 -0000 In the FreeBSD sources, there are several ways shown about how to emit several lines of text to the console or to a file. Allow me to phrase the question in C, even though it will be a valid question in almost every other programming language (compiled or interpreted): What is the _proper_ way of printing multiple lines? My research basically did bring up the following methods: a) multiple function calls: printf("This is the first line of text.\n"); printf("And this is the second line.\n"); printf("Finally a third line.\n"); b) one string in multiple parts in one function call: printf("This is the first line of text.\n" "And this is the second line.\n" "Finally a third line.\n"); c) multiple lines in one string: printf("This is the first line of text.\n And this is the second line.\n Finally a third line.\n"); There is a specific restriction that if a string contains conversion specifications for variables, those have to be in the 1st argument, so printf() does not print several strings as individual arguments except there's a first one containing appropriate %s entries. This applies for any string containing %: it has to be in the first argument passed to printf(). d) multiple strings with format string: printf("%s%s%s", "This is the first line of text.\n", "And this is the second line.\n", "Finally a third line.\n"); In this case, the \n could be in the format string instead of the text lines. So, what's the correct (or at least recommended way) of doing this? Think about use cases like fprintf(stderr, ...) for things like usage messages, or printing to a file for multi-line entries. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...