Tag Archives: c

Some arg checking and file IO in C

This small program reads a filename from the command line, and reads the file, while printing the line number in front of it.

// Line Number.
#include <stdio .h>
 
void printnumber(int number);
 
int main(int argc, char *argv[]) {
  int c;
  int lines = 1;
  FILE *fh;
 
  if (argc < 2) {
    printf("ERROR: Need a filename as argument.\n");
    return 1;
  }
 
  fh = fopen(argv[1], "r");
 
  printnumber(lines);
  while ((c = getc(fh)) != EOF) {
    if (c == '\n') {
      lines++;
      printnumber(lines);
    }
    else {
      printf("%c", c);
    }
  }
  return 0;
}
 
void printnumber(int number) {
  printf("\n%3d|", number);
}

Arrays 5[a] == a[5]

First time I saw that I thought it was weird.
Thinking more about it, it actually makes a lot of sense.

Consider the a array, with the 6 elements
The memory address of this array is a.

The first element of the array evaluates to *(a).
So, the 6th element can be written as *(a + 5).
And as *(5 + a), thus 5[a].

C enums

 
#include <stdio .h>
 
enum months {
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
 
const char *monthName[] = { "", "January", "February", "March",   
"April", "May", "June", "July", "August", "September", "October",
"November", "December" };
 
int main() {
enum months month;  
 
for (month = JAN; month < = DEC; month++) {
puts(monthName[month]);
}
return 0;
}

C pointers

* Once a variable is declared, we can get its address by preceding its name with the unary & operator, as in &k.
* We can “dereference” a pointer, i.e. refer to the value of that which it points to, by using the unary ‘*’ operator as in *ptr.
* An “lvalue” of a variable is the value of its address, i.e. where it is stored in memory.
The “rvalue” of a variable is the value stored in that variable (at that address).

#include <stdio .h>
 
int main()
{
int k=3;
int *ptr;
 
ptr = &k;
printf("The value ptr is pointing to:%d at address:%p\n", *ptr, ptr);
}
</stdio>

C prinft formatting

Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument:

specifier Output Example
c Character a
d or i Signed decimal integer 392
e Scientific notation (mantise/exponent) using e character 3.9265e+2
E Scientific notation (mantise/exponent) using E character 3.9265E+2
f Decimal floating point 392.65
g Use the shorter of %e or %f 392.65
G Use the shorter of %E or %f 392.65
o Signed octal 610
s String of characters sample
u Unsigned decimal integer 7235
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (capital letters) 7FA
p Pointer address B800:0000
n Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
% A % followed by another % character will write % to stdout.

Strings in C

C doesn’t know about strings. It knows about characters, and thus, a string is an array of characters like so:

char the_string[] = "this is my fancy string";

Another way to allocate a number of chars is this:

char the_string[50];

To show that its really an array:

#include <stdio .h>
 
int main() {
  char some_array[] = {'a', 'b', 'c'};
  printf("%s\n", some_array);
  return 0;
}
</stdio>

Which yields:

abc

Here we create an array of characters named the_string. A string in C is terminated by the “\0″ (NULL) character.

Example of passing a string to a method called print:

#include <stdio .h>
 
void print(char string[]) {
  printf("%s\n", string);
}
 
int main() {
  print("test 1 2 3");
  return 0;
}
</stdio>

C specifier meanings

%c  – Print a character
%d  – Print a Integer
%i  – Print a Integer
%e  – Print float value in exponential form.
%f  – Print float value
%g  – Print using %e or %f whichever is smaller
%o  – Print actual value
%s  – Print a string
%x  – Print a hexadecimal integer (Unsigned) using lower case a – F
%X  – Print a hexadecimal integer (Unsigned) using upper case A – F
%a  – Print a unsigned integer.
%p  – Print a pointer value
%hx – hex short
%lo – octal long
%ld – long