Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Default values of variables

When you declare a variable in C and do not initialize it, it'll have a garbage value.

(1) C code
#include <stdio.h>

int main()
{
    int x;

    printf("x: %d\n", x);  // -73847465, i.e. garbage value

    return 0;
}

Nim, on the other hand, implicitly initializes every declared variable if you do not assign a value to it. The memory location of a primitive variable will be filled with zeroes.

When you declare a string, it'll be an empty string (and not a nil).

(2) Nim code
import std/strformat   # &"Hello {name}!"

var
  a: int      # 0
  b: bool     # false
  c: char     # '\0'
  d: float    # 0.0
  s: string   # ""


echo "a: ", a
echo "b: ", b
echo &"c: '{c}'; ASCII code: {ord(c)}"
echo "d: ", d
echo &"s: '{s}'; length: {len(s)}"

Output:

a: 0
b: false
c: '�'; ASCII code: 0
d: 0.0
s: ''; length: 0

Tip

However, explicit is better than implicit. When you need a counter, I think it's a good practice to explicitly set its value to 0, to avoid confusion:

# var counter: int    # it would also work since its initial value would be 0
var counter = 0       # explicit, easier to read

for i in 1..5:  # executed 5 times
  counter += 1

echo counter  # 5
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 03, 12:36