Basic C programming 1: Variables¶

Yuto Omae @ Nihon University, Japan¶

  • We use online compiler and debugger for c/c++.
  • https://www.onlinegdb.com/online_c_compiler

Code 1: A program that does nothing¶

The following code is a program that does nothing.


#include <stdio.h>
int main(){
    // write your codes
    return 0;
}

Key points¶

  • We can use various standard functions by stating #include <stdio.h>.
  • In int main(){...}, we write some codes.
  • // denotes a comment, not a program.
  • The return 0; in the main function means successful end of the program.
  • When this code is run, nothing happens.

Code 2: Function to display text on the display¶

  • The following code is for showing text on your display.

#include <stdio.h>
int main(){
    printf("sample text 1 \n");
    printf("sample text 2 \n");
    return 0;
}

Key points¶

  • By using printf function, text is shown in our display.
  • The double quotation " is required for text in printf function, e.g., "sample text".
  • The escape sequence \n is for line break.
  • The bottom of the each line, semicolon ; is required except for curly brackets {...} and <...>.

Works¶

  • Check that removing semicolon ; results in a error.
  • Check that removing \n results in not occurring a line break.

Code 3: Warning sound¶

  • The following code is for warning sound.

#include <stdio.h>
int main(){
    printf("warning sound \a");
    return 0;
}

Key points¶

  • By adding \a into printf function, a warning sound is heard.

Code 4: Integer variable¶

  • Integer is clean numbers not including fractions and decimals. For examples, 10, -1, 0, 1, 5, 10 ... are tntegers.
  • The following is the code including integer variables.

#include <stdio.h>
int main(){
    int a = 1;
    int b = -2;
    printf("a is %d \n", a);
    printf("b is %d \n", b);
    printf("a and b is %d %d", a, b);
    return 0;
}

#include <stdio.h>
int main(){
    int a = 1, b = -2;
    printf("a is %d \n", a);
    printf("b is %d \n", b);
    printf("a and b is %d %d", a, b);
    return 0;
}

Key points¶

  • By writing int a = 1;, the integer variable a is prepared and substituting 1 into a.
  • As shown in the second code, we can also write int a = 1, b = -2; for preparing two integer variables.
  • To show the value of integer vriables by using printf function, write %d to displaying location and set the integer variable on the end of printf function, e.g., printf("%d", a);.
  • To show two integer variables by one printf function, write printf("%d %d", a, b);. The first %d correspond to a and the second %d correspond to b.
  • In the case of 64 bit operating system (that is, computer as of 2024) and using int, the minimum integer value is -2147483648 and the maximum integer value is 2147483647. Substituting the out of this range into int variable results in unstable.

Code 5: Rules of variable name¶

  • In the previous code, we use the single character variable's name.
  • But, multi-character variable's name can also be used.

#include <stdio.h>
int main(){
    int abc = 1;
    int a_bc = 2;
    int a_0 = 3;
    printf("%d %d %d", abc, a_bc, a_0);
    return 0;
}

Key points¶

  • All alphabets a-z and A-Z can be allowed as variable name.
  • In all symbols, only the underscore symbol _ can be allowed.
  • That is, we cannot use other symbols, e.g., +, -, *, and # as variable's name.
  • The numbers can be allowed only after first character.
  • That is, as variable name, xyz8 is allowed, but 8xyz is not allowed.

Works¶

  • Define variables of new name not occurring errors and run the program.
  • By adding int 8xyz = 5; into your code, check for occurring a error. We cannot set numbers to the first character of variable name.
  • By adding int a#a = 3; into your code, check for occurring a error. We cannot use symbols excpet the underscore.

Code 6: Basic arithmetic operations on integer variable¶

The following is code for basic arithmetic operations.


#include <stdio.h>
int main(){
    int a = 5;
    int b = -3;
    int c0, c1, c2, c3;

    c0 = a + b;
    c1 = a - b;
    c2 = a * b;
    c3 = a / b;

    printf("a + b = %d \n", c0);
    printf("a - b = %d \n", c1);
    printf("a * b = %d \n", c2);
    printf("a / b = %d \n", c3);
    return 0;
}

Key points¶

  • The symbols +, -, *, / represent addition, subtraction, multiplication, and division, respectively.
  • The division symbol / only considers the integer part, even if it is a real number.
  • This reason is that all variables were defined as integer.
  • In general, the integer variable is not suitable for division.
  • The calculation result is a/b = 5/(-3) = -1.666..., but c3 is -1.

Code 7: Order of priority of calculation (int)¶

Like mathematics, the program calculates the inside of brackets as priority.


#include <stdio.h>
int main(){
    int a = 5;
    int b = -3;
    int c = 2;
    int d;

    //comment: 16 - 4 = 12
    d = (a - b) * (a + b) - c * (a + b);

    printf("(a - b) * (a + b) - c * (a + b) = %d \n", d);
    return 0;
 }

Key points¶

  • Expressions in brackets are calculated with highest priority.
  • Next, multiplication and division are computed.
  • Finally, additions and subtractions are computed.

Works¶

  • Please write a code that performs a slightly complex calculation.

Code 8: Variable for real numbers (double)¶

The following is code for using real numbers like 1.2, 0.4 and -2.9.


#include <stdio.h>
int main(){
    double a = 5.2;
    double b = -3.7;
    double c0, c1, c2, c3;

    c0 = a + b;
    c1 = a - b;
    c2 = a * b;
    c3 = a / b;

    printf("a + b = %lf \n", c0);
    printf("a - b = %lf \n", c1);
    printf("a * b = %lf \n", c2);
    printf("a / b = %lf \n", c3);
    return 0;
}

Key points¶

  • For using real numbers, using double instead of int.
  • For showing double variance, using %lf in printf function.
  • By using double variable, we can obtain exact division /. See printf("a / b = %lf \n", c3);

Code 9: Number of digits under decimal point¶

  • The number of digits of real numbers sometime become many like pi = 3.14159265359....
  • Reducing the number of digits of under decimal point improves ease of viewing.
  • The following is code for adjusting the number of digits under decimal point.

#include <stdio.h>
int main(){
    double pi = 3.14159265359;

    printf("pi = %lf \n", pi);
    printf("pi = %.4lf \n", pi);
    printf("pi = %.2lf \n", pi);
    printf("pi = %.0lf \n", pi);

    return 0;
}

Key points¶

  • %lf: 6 digits (default) = 3.141593
  • %.4lf: 4 digits = 3.1415
  • %.2lf: 2 digits = 3.14
  • %.0lf: 0 digits = 3

Code 10: Order of Priority of Calculation (double)¶

Like mathematics, the program calculates the inside of brackets as priority.


#include <stdio.h>
int main(){
    double a = 5.0;
    double b = -3.0;
    double c = 32.0;
    double d;

    //comment: 16/32 + 5 = 5.5
    d = (a - b) * (a + b) / c + a;

    printf("(a - b) * (a + b) / c + a = %lf", d);
    return 0;
}

Key points¶

  • Expressions in brackets are calculated with highest priority.
  • Next, multiplication and division are computed.
  • Finally, additions and subtractions are computed.
  • Since variable type is double, we can exactly calculate division /.

Works¶

  • Please write a code that performs a slightly complex calculation including division /.

Code 11: Variable for characters¶

  • C programming can use string type value.

#include <stdio.h>
int main(){
    char text[100] = "I have a pen.";
    printf("Text: %s", text);
    return 0;
}

Key points¶

  • By coding text[100], the variable text can be prepared, and it can be allowed maximum 100 characters.
  • To showing it in printf function, using %s.

Code 12: Input from keyboard (text)¶

  • The following is code for input values from keyboard and substituting it into variable.

#include <stdio.h>
int main(){
    printf("Input string: ");
    char text[100];
    scanf("%s", text);
    printf("Your input string: %s \n", text);
    return 0;
}

Key points¶

  • scanf function is for input value from keyboard.
  • By coding scanf("%s", text);, we can input value to variable text.
  • Because the type of variable text is characters, we use %s in scanf function.

Code 13: Input from keyboard (int)¶

The following is code for input from keyboard and substituting it into int-type variable.


#include <stdio.h>
int main(){
    printf("Input: ");
    int a_val;
    scanf("%d", &a_val);
    printf("Your input: %d \n", a_val);
    return 0;
}

Key points¶

  • By coding scanf("%d", &a_val); we can input value from keyboard to int-type variablea_val.
  • In the case of int-type variable, & is required.
  • Because the type of variable a_val is integer, we use %d in scanf function.

Code 14: Input from keyboard (double)¶

The following is code for input from keyboard and substituting it into double-type variable.


#include <stdio.h>
int main(){
    printf("Input: ");
    double a_val;
    scanf("%lf", &a_val);
    printf("Your input: %lf \n", a_val);
    return 0;
}

Key points¶

  • By coding scanf("%lf", &a_val); we can input value from keyboard to double-type variablea_val.
  • In the case of double-type variable same as int-type, & is required.
  • Because the type of variable a_val is double, we use %lf in scanf function.

Summary¶

Variable type Initialization Show on display Input from keyboard
Integer int a print(%d, a) scanf("%d", &a)
Double (real number) double a print(%lf, a) scanf("%lf", &a)
Characters char a[100] print(%s, a) scanf("%s", a)

Assignment¶

Create following programs and submit them.

  • (1) Enter the two integer-type values from keyboard. Then calculate adds them together, and displays the result.
  • (2) Enter the three double-type values (that is, real numbers) from keyboard. Then calculate these average values, and displays the result.
  • (3) Enter the width and height of the triangle from the keyboard. Then, calculate the area of the triangle, and displays the result.
  • (4) Consider a simple system to be installed in a hospital. Write code to input the patient's name, age, height [meter], weight [kg], and blood type from the keyboard and display it. After that, add a function to that code that calculates and displays the body mass index.
  • (5) Using the knowledge you have learned here, write a slightly more complex program. Also, explain the function of that code.

Submission: Please submit following materials.

  • (A) a screenshot of the program code
  • (B) a screenshot of the running results
  • (C) Detailed explanation by text.
  • Note: Please use the submission function of oma portal system, not use word and pdf file.