ECE 15 Chapter 2 Code

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Output formatting

%(flags)(width)(.precision)specifier

String Specifier Formatting:

%(flags)(width)(.precision)specifier

Integer Specifiers: +,-,0

+ adds the sign of the number at the start - forces the integer to go to the left and makes all spaces go right 0 makes any blank space a 0.

4%3

1

User input is 1053 17.5 42 Smith fgets(str3, USER_TEXT_LIMIT, stdin); What is str3?

1053 17.5 42 Smith

#include <stdio.h> int main(void) { double myFloat=12.34; printf("%-010.3fTest", myFloat); return 0; }

12.340BBBBTest

short(bits)

16

10/4 (Integer division)

2

User input is 1053 17.5 42 Smith scanf("%f %d %d %d", &val1, &val2, &val3, &val4); How many variables will be succesfully scanned?

2 %f reads 1053 %d reads 17 second %d tries to read "." and fails

int(bits)

32

long(bits)

32

long long(bits)

64

char(bits)

8

char c; scanf(" %c", &c); printf("%c", c); Input is " A" What is Output

A

char c; scanf(" BBBBBBBBBBBBBBBBBBBBB%c", &c); printf("%c", c); Input is " A", B is whitespace What is Output

A

Input Parsing

Controls how an input from the scanf function is accepted %(width)specifier

Floating Point Number

Decimal

Integer Specifiers: Width

Dictates the total number of characters. Does nothing if width < # of characters in variable.

sscanf(objectInfo, "%s %d %d", object, &price, &quantity);

Finds the line where objectinfo is defined, tells the program that the first input is a string, the second an integer, and the third an integer

Unsigned

Forces an integer to be positive

String Flag: -

Forces the string to be on the left and uses blanks to meet the width requirement

-: flag

Forces the text to go to the left.

Width

Forces x amount of characters to present in total. Does nothing if width is less than actual number defined by precision.

//

Issues single comment line; everything after // is ignored.

o /* o */

Multi-line commentl; everything between the two is ignored.

Identifiers

Must start w/ letters or underscore, can only have those or numbers.

#include <stdio.h> int main(void) { double myFloat=12.34; printf("%+f", myFloat); return 0; }

Output: +12.340000

#include <stdio.h> int main(void) { double myFloat=12.34; printf("%08.2f", myFloat); return 0; }

Output: 00012.34

#include <stdio.h> int main(void) { double myFloat=12.34; printf("test:%.3f", myFloat); return 0; }

Output:test:12.340

#include <stdio.h> int main(void) { double myFloat=12.34; printf("test:%0.3f", myFloat); return 0; }

Output:test:12.340

+: flag

Prints a + sign at the start if the number is not negative

#define x y

Replaces all instances of x with y

char c; scanf("%c", &c); printf("%c", c); Input is " A" What is Output

Space

Precision

Specifies how many characters there are after the decimal place

Input parsing width

Specifies max # of characters to read. %2d will read up to 2 characters

String Precision

Specifies the maximum characters that can be printed, truncating right to left

String Width

Specifies the minimum number of characters present

fgets(streetAddress, ADDRESS_SIZE_LIMIT, stdin);

Tells the user to input their value (stdin), gives a limit on max characters (ADDRESS_SIZE_LIMIT), then stores user input into the variable streetAdress.

#include <stdio.h> int main(void) { char myString[30] = "Testing"; printf("Test: %.4s", myString); return 0; }

Test: Test

#include <stdio.h> int main(void) { char myString[5] = "Testing"; printf("Test: %s", myString); return 0; }

Test: Testi // the [5] means we only stored 5 characters of our variable, hence, we can only print the first 5.

#include <stdio.h> int main(void) { char myString[30] = "Testing"; printf("Test:%10s String", myString); return 0; }

Test: Testing String

#include <stdio.h> int main(void) { char myString[30] = "Testing"; printf("Test: %-10s Test" , myString); return 0; }

Test: Testing Test

#include <stdio.h> int main(void) { int myFloat=301; printf("Test:%+08d", myInt); return 0; }

Test:+0000301

#include <stdio.h> int main(void) { int myFloat=301; printf("Test:%+d", myFloat); return 0; }

Test:+301

#include <stdio.h> int main(void) { int myFloat=301; printf("Test:%08d", myInt); return 0; }

Test:00000301

#include <stdio.h> int main(void) { int myFloat=301; printf("Test:%7d", myFloat); return 0; }

Test:BBBB301

0: flag

Uses 0s instead of spaces to hit the width requirement

#include <stdio.h> int main(void) { double myFloat=12.34; printf("Value:%7.2f", myFloat); return 0; }

Value:BB12.34

#include <stdio.h> int main(void) { const int ADDRESS_SIZE_LIMIT = 50; char streetAddress[ADDRESS_SIZE_LIMIT]; printf("Enter street address: "); fgets(streetAddress, ADDRESS_SIZE_LIMIT, stdin); printf("You entered: %s", streetAddress); return 0; } Input is 1313 Mockingbird lane

You entered: 1313 Mockingbird Lane

\\

backlash

Initialize a string under the variable "myString" to the value "Hello" using both one word and each individual letter.

char greeting[ ] = "Hello"; char myString[ ]= {'H', 'e', 'l', 'l', 'o', '\0'};

%d

decimal integer values

%lf

double float

Double variable (float) declaration for milesTravel

double milesTravel;

\''

double quote

Absolute value of x

fabs(x)

%f

float-point

Assignment statement: Declare the variable litterSize then set it's value equal to 3.

int litterSize; litterSize = 3;

Initial integer assignment

int numDogs = 0

Variable initialization, create a variable called userAge.

int userAge;

%lld

long long signed integer

%ld

long signed integer

Variable

named item, such as x or numPeople, used to hold a value

\n

newline

%c

single ASCII character

\'

single quote

square root of x

sqrt(x)

%s

string

\t

tab

%u

unsigned integer

%llu

unsigned long long integer

%lu

unsigned long signature

%hu

unsigned short integer

scanf("%d, %f, %d", &val1, &val2, &val3); User input is 19, 20, 21 The variables were initially set to 0 What is the val1, val2, and val3 after input?

val1 reads 19 The "," reads the "," after the 19 and discards it val2 reads 20 "," reads "," and discards it val3 reads 21

scanf("%2d %f %d", &val1, &val2, &val3); User input is 1053 17.5 42 What is val1, val2, and val3?

val1 scans the first two characters of 1053, giving it a value of 10 val2 reads in the next two digits of 1053, giving it a value of 52 as a float val3 reads 17, since it's an integer and cannot read decimals

%hd

short signed integer

x to the power of y

pow(x,y)

Print the ith element of a string

printf("%c", myString[i]);

Print a double variable milesTravel

printf("%lf", milesTravel);

Print a string for variable myString

printf("%s", myString);

%%

prints the % symbol

How to scan for char variable?

scanf("%c", &userChar);

Scan a string for variable myString

scanf("%s", myString);


Kaugnay na mga set ng pag-aaral

Chapter 5 physics (homework, smart book, and quiz)

View Set

Ch. 6 Individual Deductions Concepts & HW

View Set

Chpt. 1: The Study of Human Sexuality

View Set

Lección 6: Recapitulación y Flash cultura

View Set

Essentials of Human Anatomy and Physiology: Chapter 5 The Skeletal System

View Set

EXW 450 Final Exam Question Bank

View Set

Unit 1: Introduction QUIZ 1: SET THEORY

View Set