Perhaps you would like to write your software to handle more general cases, so that you don't have to adjust your source every time the grid size changes, or a variable is added to the file.
There are inquiry functions that let you find out everything you need to know about a file. These functions contain “inq” or “INQ” in their names.
Using the inquiry functions, it is possible to write code that will read and understand any netCDF file, whatever its contents. (For example, ncdump does just that.)
First use nc_inq(), which will tell you how many variables and global attributes there are in the file.
Start with global attribute 0, and proceed to natts - 1, the number of global attributes minus one. The nc_inq_att() function will tell you the name, type, and length of each global attribute.
Then start with dimid 0, and proceed to dimid ndims - 1, calling nc_inq_dim(). This will tell you the name and length of each dimension, and whether it is unlimited.
Then start with varid 0, and proceed to varid nvars - 1, calling nc_inq_var(). This will tell you the number of dimensions of this variable, and their associated IDs. It will also get the name and type of this variable, and whether there are any attributes attached. If there are attributes attached, use the nc_inq_att() function to get their names, types, and lengths.
(To read an attribute, use the appropriate nc_get_att_<TYPE> function, like nc_get_att_int() to get the data from an attribute that is an array of integers.)
The inquiry functions are: