Professional Documents
Culture Documents
Chapter2 - Basics of C and C++ - Part2
Chapter2 - Basics of C and C++ - Part2
Declared in <stdio.h>
Control string: a character array defines the format of
input/output string
Expressions: contain data to be presented
Variables: variables store input data (may be provided by
users)
& (ampersand): required symbol
Chapter 2: Basic of C & C++ © DIA 2022.2 14
printf()
int printf ( const char * format, ... );
A format specifier follows this prototype:
%[flags][width][.precision][length]specifier
Returning value:
On success, the total number of characters written is returned
If a writing error occurs, the error indicator (ferror) is set and a
negative number is returned
If a multibyte character encoding error occurs while writing
wide characters, errno is set to EILSEQ and a negative number
is returned.
width description
Minimum number of characters to be printed. If the value to be printed is shorter than this
(number) number, the result is padded with blank spaces. The value is not truncated even if the result is
larger.
The width is not specified in the format string, but as an additional integer value argument
*
Chapter 2: Basic ofpreceding
C & C++ the argument that has to be formatted. © DIA 2022.2 17
printf()
%[flags][width][.precision][length]specifier
.precision description
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value
to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated
even if the result is longer. A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this
.number is 6).
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the
ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.
The precision is not specified in the format string, but as an additional integer value argument preceding the
.*
argument that has to be formatted.
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n",
100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}
int numPushups;
specifiers
int scanfCount;
scanfCount = scanf(“%d”, &studentID);
/* if scanfCount is not equal to 1 at this point,
the user has made some kind of mistake.
Handle it. */
int main ()
{
char str [80];
int i;
return 0;
}
It’s a must!
Trang
Chapter 2: Basic of C & C++ © DIA 2022.2
45
Constant definition
#define BUFFERSIZE 256
#define MIN_VALUE -32
#define PI 3.14159
Attention:
There are neither = nor ; in the line
Name defined by #define can be removed by using #undef
(it may be redefined later)
Substitutions are made only for tokens, and do not take place
within quoted strings, e.g. there is no substitution for PI in
printf("PI")
UPPERCASE WORD is used as the constant name in
order to differentiate with variable and function names
Chapter 2: Basic of C & C++ © DIA 2022.2 46
#define, const and enum
It is possible to replace #define with constant
or enum
#define ARRAYSIZE 10
const int ArraySize = 10;
double array[ARRAYSIZE]; /* Valid. */
#define PI 3.14159
#define ARRAYSIZE 10
const double Pi = 3.14159; /* Preferred */
enum {ARRAYSIZE = 10}; /* Preferred */
#define CLAMP(val,low,high) \
((val)<(low) ? (low) : (val) > (high) ? (high) :
(val))
#define ROUND(val) \
((val)>0 ? (int)((val)+0.5) : -(int)(0.5-(val)))
WAIT_OBJECT_0;
#elif defined(__QNX__)||defined(__linux__)
if(flock(fd,LOCK_EX|LOCK_NB) == -1)
return 0;
else
return 1;
#endif
Chapter 2: Basic of C & C++ © DIA 2022.2 62
Non-standard code
Applied in the case that codes are used for different microprocessors
#if defined(ARDUINO) && ((ARDUINO) >= 100)
//arduino core v1.0 or later
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#if defined(__AVR__)
#include <avr/pgmspace.h>
//use for PROGMEM Arduino AVR
#elif defined(ESP8266)
#include <pgmspace.h>
//use for PROGMEM Arduino ESP8266
#elif defined(_VARIANT_ARDUINO_STM32_)
#include <avr/pgmspace.h>
//use for PROGMEM Arduino STM32
#endif
E.g.: