Mahakar, thank you for the minimal reproducer. The program looks correct.
I tested it on macOS with gfortran 13 and netcdf-C 4.9.2. It ran
correctly, and gave the expected output:
> ./a.out
No error
No error
No error
> ncdump -h test.nc
netcdf test {
dimensions:
lon_dim = 360 ;
variables:
float lon(lon_dim) ;
}
I notice that on github issues, there are at least three other previous
cases of "Name contains illegal characters" that were never fully
explained. Cases were all fortran, or fortran-related.
If you still want help with this, then please open a new issue on
https://github.com/Unidata/netcdf-fortran/issues . Please include your
compiler and version identification, version numbers of netcdf-C and
netcdf-fortran, and the full compile command that you are using.
On Sun, May 10, 2026 at 9:31 AM M.Mahakur <mmahakur@xxxxxxxxxxxxxx> wrote:
> Dear all,
>
> After installation of netcdf4 in Ubuntu I am trying to use the netcdf
> library through fortran. I am facing problem in running the code. Here is
> sample code:
>
> program test
> use netcdf
> implicit none
> character(len=10) :: vname
> integer :: ncid, dimid, varid, sta
> vname='lon'
> sta = nf90_create("test.nc", nf90_clobber, ncid)
> print *, trim(nf90_strerror(sta))
> sta = nf90_def_dim(ncid, "lon_dim", 360, dimid)
> print *, trim(nf90_strerror(sta))
> sta = nf90_def_var(ncid, trim(vname) , nf90_float, (/dimid/), varid)
> print *, trim(nf90_strerror(sta))
> sta = nf90_close(ncid)
> end program
> ~
>
> This prompts me:
> No error
> No error
> NetCDF: Name contains illegal characters
>
> The nf90_def_var does not works! I also tried to keep 'lon' in place of
> trim(vname). The problem is same. But, creates test.nc!
>
> $ ncdump test.nc
> netcdf test {
> dimensions:
> lon_dim = 360 ;
> }
>
> Someone please help to overcome this problem.
>
> With thanks and Regards,
> Mahakur
>