C Programming Flashcards
Introduction to C
Introduction
- C is a general-purpose programming language.
- Developed by Dennis Ritchie at Bell Labs during the development of Unix in 1972-73.
- The Unix system and most programs that run on Unix are written in C.
- C is called a “System Programming Language” because it's useful for writing compilers and Operating Systems.
- C has been equally used in writing applications/software in other domains.
C as a Low-Level Language
- C is relatively a low level language.
- Designed such that its constructs can efficiently map with machine instructions (machine code).
- Therefore, C easily interacts with hardware, e.g., RAM, CPU.
- Devices formerly coded using Assembly Language now use C, from supercomputers to embedded systems.
C as a Cross-Platform Language
- C is a cross-platform language.
- A program written on one machine can be executed on another machine.
- You don’t have to rewrite the program for another machine, unlike Assembly or Machine Language.
First C Program
- A C Program consists of functions and variables.
- A program may contain several functions.
- In every C Program, there is a special function
main()
. - Your program begins executing at the beginning of
main
. - Within the
main
function, you may call other functions. - The first line of the program is
#include <stdio.h>
. - This line tells the compiler to include information about the standard input/output library.
- This line appears at the beginning of many C source files/programs.
Elements of a C Program
- A C development environment includes:
- System libraries and headers: a set of standard libraries and their header files (e.g.,
/usr/include
andglibc
). - Application Source: application source and header files.
- Compiler: converts source to object code for a specific platform.
- Linker: resolves external references and produces the executable module.
- System libraries and headers: a set of standard libraries and their header files (e.g.,
- User program structure:
- There must be one
main
function where execution begins when the program is run. This function is calledmain
.int main (void) { ... }
int main (int argc, char *argv[]) { ... }
- UNIX Systems have a 3rd way to define main(), though it is not POSIX.1 compliant
int main (int argc, char *argv[], char *envp[])
- Additional local and external functions and variables.
- There must be one
C Standard Header Files
Standard Headers you should know about:
stdio.h
– file and console (also a file) IO:perror
,printf
,open
,close
,read
,write
,scanf
, etc.stdlib.h
- common utility functions:malloc
,calloc
,strtol
,atoi
, etc.string.h
- string and byte manipulation:strlen
,strcpy
,strcat
,memcpy
,memset
, etc.ctype.h
– character types:isalnum
,isprint
,isupper
,tolower
, etc.errno.h
– defineserrno
used for reporting system errors.math.h
– math functions:ceil
,exp
,floor
,sqrt
, etc.signal.h
– signal handling facility:raise
,signal
, etc.stdint.h
– standard integer:intN_t
,uintN_t
, etc.time.h
– time-related facility:asctime
,clock
,time_t
, etc.
Writing a C Program
- Open Terminal on Seed Virtual Machine.
- Create a folder in your home directory (
/home/seed
).mkdir Cprograms
cd Cprograms
- Open a text editor using the following command:
sudo gedit helloworld.c
- Using the above command, you are opening a text editor and naming the file as
helloworld.c
(.c
is an extension for all C files). - Type the program you just learned in the text editor.
- Once you have finished writing it, go to the file tab and click save.
- Save this file in the
Cprograms
folder.
Running a C Program
- To run a C Program, you first need to compile the program.
- If you are not in the
Cprograms
directory, in the terminal, browse to that directory. - To compile, use the following command:
gcc –o helloworld helloworld.c
- After the file is compiled, if you list the contents of this folder, you will notice a new file just created with the
helloworld
name (this is an executable file). - In the terminal, now type the following to run the program:
./helloworld
Variables and Arithmetic Expressions
- Write a program in C that converts Fahrenheit into centigrade or Celsius equivalents:
- Formula: C = (5/9)(F-32)
Variables
- A variable is a named space in memory to store data.
- In C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements.
- A declaration tells the properties of variables.
- It consists of a type name and a list of variables.
- Example:
int fahr, celsius;
int lower, upper, step;
- Example:
- The type
int
means that the variables listed are integers. - Another data type is
float
- numbers that may have a fractional part. - The range of both
int
andfloat
depends on the machine you are using.- A 16-bit integer will hold values between -32768 and +32767.
- A 32-bit integer will hold values between -2,147,483,648 to 2,147,483,647.
- C provides several other basic data types besides
int
andfloat
, includingchar
,double
, etc. - After being declared, a variable is usually initialized to some initial value before being used.
- A variable has a type and a name.
int int1;
// declares an integer named int1double d;
// declares a doubleint1 = 5;
// int1 is given the value 5int int2 = 3;
// int2 is declared and initialized
Variable Declaration Syntax
- Example:
float x;
double d = 5;
int *p, i, a[100];
char s[21];
- Syntax:
type variable_name, ... [= value];
Changing Variable Values
- Example 1:
int x, y, z;
x = 2;
x = x + 1;
y = z = 4 + 5;
x += 1;
++x; //Pre increment
x++; //Post increment
y = x--;
- Example 2:
int i, j;
i = 1;
j = 1;
j = i++;
- `printf (“Value of j is %d\n Value of i is %d\n