Holla Tech - Learn

Data Types
 

C supports the following basic data types:
int: integer, a whole number.
float: floating point, a number with a fractional part.
double: double-precision floating point value.
char: single character.

The amount of storage required for each of these types varies by platform.
C has a built-in sizeof operator that gives the memory requirements for a particular data type.
For example

#include <stdio.h>

int main() {
  printf(“int: %ld \n”, sizeof(int));
  printf(“float: %ld \n”, sizeof(float));
  printf(“double: %ld \n”, sizeof(double));
  printf(“char: %ld \n”, sizeof(char));

  return 0;

 

The program output displays the corresponding size in bytes for each data type.
The printf statements in this program have two arguments. The first is the output string with a format specifier (%ld), while the next argument returns the sizeof value. In the final output, the %ld (for long decimal) is replaced by the value in the second argument. 

NOTE!
Note that C does not have a boolean type. A printf statement can have multiple format specifiers with corresponding arguments to replace the specifiers. Format specifiers are also referred to as conversion specifiers. We will learn more about format specifiers in the upcoming lessons.

Variables
 

A variable is a name for an area in memory.
The name of a variable (also called the identifier) must begin with either a letter or an underscore and can be composed of letters, digits, and the underscore character.
Variable naming conventions differ, however using lowercase letters with an underscore to separate words is common (snake_case).
Variables must also be declared as a data type before they are used.

The value for a declared variable is changed with an assignment statement.
For example, the following statements declare an integer variable my_var and then assigns it the value 42:

int my_var;
my_var = 42

 

You can also declare and initialize (assign an initial value) a variable in a single statement: 

int my_var = 42

 

Let’s define variables of different types, do a simple math operation, and output the results:

#include <stdio.h>

int main() {
  int a, b;
  float salary = 56.23;
  char letter = ‘Z’;
  a = 8;
  b = 34;
  int c = a+b;

  printf(“%d \n”, c);
  printf(“%f \n”, salary);
  printf(“%c \n”, letter);

  return 0;

 

As you can see, you can declare multiple variables on a single line by separating them with a comma. Also, notice the use of format specifiers for float (%f) and char (%c) output.

NOTE!
The C programming language is case-sensitive, so my_Variable and my_variable are two different identifiers.

Constants
 

A constant stores a value that cannot be changed from its initial assignment.
By using constants with meaningful names, code is easier to read and understand.
To distinguish constants from variables, a common practice is to use uppercase identifiers.

One way to define a constant is by using the const keyword in a variable declaration:

#include <stdio.h>

int main() {
  const double PI = 3.14;
  printf(“%f”, PI);
   
  return 0;

 

The value of PI cannot be changed during program execution.
For example, another assignment statement, such as PI = 3.141 will generate an error.

NOTE!
Constants must be initialized with a value when declared.

Another way to define a constant is with the #define preprocessor directive.
The #define directive uses macros for defining constant values.
For example:

#include <stdio.h>

#define PI 3.14

int main() {
  printf(“%f”, PI);
  return 0;

 

Before compilation, the preprocessor replaces every macro identifier in the code with its corresponding value from the directive. In this case, every occurrence of PI is replaced with 3.14.
The final code sent to the compiler will already have the constant values in place.

The difference between const and #define is that the former uses memory for storage and the latter does not.

NOTE!
Do NOT put a semicolon character at the end of #define statements. This is a common mistake. We will learn more about preprocessor directives in the next modules.

BACK NEXT 

CLICK ON THE BUTTON BELOW TO GO TO THE C MAIN COURSE PAGE. 

C MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT