Computer Science

C Program — Basics

2023-08-13 4 min read

Getting Started

  • Programming language

    • C
    • C++
    • Java
    • JavaScript
    • Python
  • Source-code Editor

    • VS Code
    • Dev-C++
    • Code::Blocks
    • Notepad++
  • Compiler

Introduction

  • Although C is an old and relatively low-level language, it is still very popular today.
  • It is the foundation of many languages — once you know C, picking up others is much easier.
  • C is closely tied to UNIX, since it was created to write the UNIX operating system.
  • C is fast compared with most other languages.

Base Concepts

Rule

  • Left to Right, Up to Down
  • ; at the end of each line of instructions
  • return 0 ; ends the program
  • Use TAB
  • {} wraps the content of a structure

Comment

  • // … single line
  • /* … */ multi-line

Syntax

Getting started

#include <stdio.h>

int main(){
    ...
    return 0;
}
  • Including a library

    Standard Library Header File.

#include <stdio.h> — roughly, it handles input/output such as scanf / printf.

It gives the computer a “dictionary” (the header file) to look up what the program’s statements mean.

  • Main function

    The main body of execution — the entry point of any program.

    Note: this becomes clearer once you learn about functions.

Variables

Simply put, a variable is a value that can change.

  • Type
  • Label
  • Data

Declare

DataType  variableName  =  data;

Data Type

  • Primitive Data Types : int, float, char, bool, void

  • Derived Data Types : function, array, pointer, reference

  • Abstract or User-Defined Data Types : Class, Structure, Union, Enumeration, Typedef-defined datatype

Primitive Data Type

These data types are built-in or predefined and can be used directly to declare variables.

Data TypebyteRemark
Int (integer)4 byterange up to ≈ 10^9
Float (single-precision)4 byte
Double (double-precision)8 byterange from -128 to 127 or 0 to 255
Char (character)1 byteTrue(1) or False(0)
Bool (boolean)1 byteused for a function that does not return a value
Void (none/empty)

1 byte == 8 bits

Identifiers

  • Can contain letters, digits and underscores
  • Must begin with a letter or an underscore (_)
  • case sensitive (myVar and myvar are different variables)
  • Cannot contain whitespace or special characters ( ! # % … )
  • Reserved words cannot be used (C keywords such as int)
  • Names should be descriptive and sensible.
  • Avoid abbreviations — though abbreviations familiar to people in the field (even if not on this project) are fine.
  • Variable names (including function parameters) are all lowercase, with words joined by underscores.

Naming conventions

Constants

Constants.

A value that cannot be changed.

const  DataType  variableName  =  data;

I/O

Printf

Output.

Print data to the screen.

Printing variables.

Print a variable’s value to the screen.

Printing variables in a specific format.

Print a variable in a specific format to the screen.

Escape

  • \ escape character
  • \0 null character (NULL)
  • \t TAB
  • \n newline
  • \a bell (BEL)
  • \" double quote
  • \' single quote

Scanf

Input.

Read input; you need a variable to store the input data.

Getchar/Putchar

Character input / output.

If more than one character is entered, getchar takes the first one and leaves the rest in the buffer until the next getchar or scanf reads them.

Operator

  • Arithmetic Operator
  • Assignment Operator
  • Relational Operator / Comparison Operator
  • Logical Operator
  • Unary Operator

Arithmetic Operators

  • + addition
  • - subtraction
  • * multiplication
  • / division (quotient)
  • % modulo (remainder)

Assignment Operators

  • = assignment
  • += compound assignment

Relational Operators

  • > greater than
  • >= greater than or equal to
  • < less than
  • <= less than or equal to
  • == equal to
  • != not equal to

= VS. ==

  • = assignment: a = b stores b’s value into variable a.
  • == equality: a == b — are a and b equal? True/False.

Logical Operators

  • && AND
  • || OR
  • ! NOT

Unary Operators

++ / --

  • postfix-expression ++
  • ++ unary-expression

Challenge

(B)

(A)

(A)

(B)

Encoding

Encoding is the process of converting information from one format to another.

ASCII

American Standard Code for Information Interchange

Character encoding — 128 total

  • Control characters 33
  • Printable characters 95

ASCII (Wikipedia)

Uppercase → lowercase

  • Uppercase A — decimal: 65
  • Lowercase a — decimal: 97

# $97 - 65 = 32$ # Upper- and lowercase differ by 32 in decimal.

← Posts
meow~