User:Wickedjargon/Learning C Q and A Style

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Data Types[edit | edit source]

Is it true that this is an int (stands for integer)?

42

Yes.

Is it true that this is an int?

0

Yes.

Is it true that this is an int?

-27

Yes.

Is it true that this is an int?

3.14

No, that number includes a decimal point. 3.14 is a float.

Is it true that this is an int?

42.0

No, 42.0 is a float.

Are these ints?

-0, +0

Yes! C allows all ints to be signed or unsigned. Zero is not a special case. -0, +0 are are signed ints. 0 is an unsigned int.

Is it true that this is an int?

2,147,483,647

Yes.

Is it true that this is an int?

2,147,483,648

No! ints cannot surpass 4 bytes. 2147483647 is the largest int that can fit onto 4 bytes of memory.

How would this denary number be expressed in binary?

+2147483647

0111 1111 1111 1111 1111 1111 1111 1111

The left most bit indicates the sign in. that 0 on the left means it's a positive number. There are

  • 4 bits in a nibble
  • 8 bits in a byte

If you do not know binary, or need a review try A Crash Course in the Mathematics of Infinite Sets

What is your definition of an int?

int is a C data type that can hold only whole numbers, signed or unsigned, that can fit on to 4 bytes of memory. including 0, +0, -0,

Which of the following is a float (floating point number)?

42., 40, 0, +0, -0

42., 40, 0 are floats. the float data type in C can represent both whole numbers and non-whole numbers. Floats cannot represent neither +0 nor -0.

Is it true that this is a int?

No. infinity has no representation in the int data type. However, is represented in the float data type.

How is this represented in binary?

+∞ is represented as

0 11111 0000000000

-∞ is represented as

1 11111 0000000000

Functions[edit | edit source]

Is it true that this is a function?


int main(){}

Yes. It can be compiled as-is.

what does this mean/do?

main

It declares a function named 'main'

What does this mean/do?

()

  • Between ( and ) are listed the names of the arguments that main takes.
  • An argument is input that the a function receives.
  • in this case, main is the only function in the code, so main takes no arguments

What does this mean/do?

{}

  • Between { and } are listed the statements that are performed when main is called.
  • Between { and } is the definition of the function main.
  • in this case, main has no statements in its body

How many statements are there here?

int pizza; int pizza = 1;

A statement is preceded with ; therefore there are 2 statements.

What does this do/mean?

int pizza; int pizza = 1;

  • int pizza; declares a variable named pizza with type int
  • int pizza = 1; assigns the value 1 to pizza

How many statements are there here?

int pizza; int pizza = 1;

2

How many statements are there here?

int pizza = 1;

1

What does this mean/do?

int pizza = 1;

  • int pizza declares a variable named pizza with type int
  • = 1 assigns the value 1 to pizza

What does this mean/do?


#include <stdio.h>
int pepperoni;

int AddOneToInput(int pepperoni)
{
    return pepperoni + 1;
}

int main()
{
    printf ("%d",AddOneToInput(3));
}


...

int pepperoni;

the variable pepperoni of type int is initialized

int AddOneToInput(int pepperoni)

the function named AddOneToInput is declared an accepts one argument named pepperoni

AddOneToInput(3)

AddOneToInput(3) calls the function AddOneToInput with the argument 3

return pepperoni + 1;

AddOneToInput takes the argument 3 and uses it in place of pepperoni.

So this... return pepperoni + 1;

becomes this... return 3 + 1;

printf ("%d",AddOneToInput(3));

the function printf is called. prtinf's definition is within stdio.h.

printf ("%d",AddOneToInput(3));

becomes.. printf ("%d",4); which becomes.. printf ("4"); and 4 is printed to the console.

Functions outside main()[edit | edit source]

Using struct[edit | edit source]