0% found this document useful (0 votes)
238 views

C Programming

This document provides an introduction to the C programming language. It discusses key concepts like data types, functions, loops, conditionals, pointers, structures, input/output, and the preprocessor. It also outlines an example program structure with header files, source files, and libraries. Finally, it describes two exercises for students to get started with C programming: 1) writing a "Hello World" program and 2) creating a number guessing game.

Uploaded by

Anbu Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views

C Programming

This document provides an introduction to the C programming language. It discusses key concepts like data types, functions, loops, conditionals, pointers, structures, input/output, and the preprocessor. It also outlines an example program structure with header files, source files, and libraries. Finally, it describes two exercises for students to get started with C programming: 1) writing a "Hello World" program and 2) creating a number guessing game.

Uploaded by

Anbu Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C: A lot like Java

Datatypes
int, float, .. (as in Java)
char* (instead of String)
Exercise Lecture 11. January no boolean
Functions
(0 is false, everything else true)

almost like static Java methods


Introduction to C Program start in “main(…)”
First exercise Loops & flow control (for, while, if, else, switch)
like Java, except “for” which cannot be used to declare
variables
by Fredrik Orderud for (int i=0; i < size; i++) {...} //ERROR Compile your program
int i; as C++ to avoid this
for (i=0; i < size; i++) {...} // Correct limitation ☺

C and C++ Header files (.h/.hpp)


C is not object oriented
No methods, only functions. Used to declare data types, structures,
E.g. “add(list, obj)” (function call – C/C++/Java) instead of
“list.add(obj)” (method call, using reference – C++/Java) or
“list->add(obj)” (method call, using pointer – C++)
constants and functions.
No inheritance or polymorphism
No public/protected/private member/method encapsulation
Contains interface, but not implementation.
C also has a very limited API compared to e.g. Java Make sure to avoid multiple includes by
Limited to string processing, file I/O and basic math.
But, it is easy to use external libraries, since almost all software libraries are surrounding header-files by #ifndef, #define, …,
written with a C interface. Other languages usually rely on “C-wrappers” to gain
access to external libraries. #endif
The C++ programming language is an “extension” that turns C into a fully
fledged object oriented language.
C++ is a very flexible and powerful programming language which features
(nearly) all the advantages of Java (except garbage collection), without
sacrificing any low-level control.
Most applications, and nearly all operating systems in use today are written in C
or C++.
complex.h

Source files (.c/.cpp)


Standard I/O
library
Contains function implementation
Program
Contains the actual program code starts in main(..)
Use #include to gain access to functionality complex.c main.c
described in header-files (almost like “import” in
java).
#include “header_file.h” for your own code
#include <library.h> to include libraries

Output:
Value: (7.000000, -1.000000i).

Structures Console Input/Output


Structure members

Used to create new Use library <stdio.h>


#include <stdio.h>
datatypes (consisting
of existing datatypes) printf(…)
Like a class without Prints text - like System.out.PrintLine(...)
Typename
printf(“Hello world.\n”); // ‘\n’ yields newline
methods int val = 7;
printf(“The number is %d”, val);
scanf(...)
WARNING: Members Reads text - like System.in.ReadLine(…)
are not initialized to char string[128];
scanf(“%s”, string); //reads a line of text
zero, as in Java. int val;
scanf(“%d”, &val); //reads an integer
Pointers - “*” and “&” Function Parameters
Contains memory address of a variable (or function) Parameters are
instead of variable value
Used in dynamic memory management
passed by value,
Very useful for handling arrays meaning a local copy
Enables us to avoid copying function arguments is created
Pass a pointer as
complex c;
complex *pc = &c;
parameter if you want
“&c” gives the memory address of variable c to:
(*pc).re = 3; Avoids copying
“*pc” gives value of the variable the pointer points to Modifications to the
pc->im = 4; parameter will have
An convenient shortcut for “(*pc).im” permanent effect

Text Strings and Arrays Preprocessor - “#” fun ☺


Datatype: The preprocessor is a “search & replace” routine
char* or char[] that is executed prior to compilation.
Example It prepares the code for the compiler
char text[] = “Hello”;
Replaces #define constants with actual values
text[0] == ‘H’;
Copies header-files into source-files, by replacing
Zero terminated!
determines end of string
#include entries
text[1] == ‘e’;
*(text+2) == ‘l’; Notice: no “=“ sign
(C has no built-in support for
text[3] == ‘l’; determining the length of Example:
text[4] == ‘o’; strings and arrays) #define PI 3.1415927
text[5] == ‘\0’; Somewhere in your code:
float val = PI;
text[i] is a syntactic shortcut for *(text+i)
References Exercise #1: Getting started

We recommend you to buy a “C”-book to use as Simple exercise to get you started with C-programming
Task 1:
language reference throughout the course Configure your development environment.
“The C Programming Language”, by Brian Kernighan Write a “Hello world” program.
and Dennis Ritchie is the reference for C programming. Task 2:
Most other books will, however, also suffice. Pick one Write a “Guess a number between 1 and 100” program.
you like. Separate program into several functions in separate source-files
(probably overkill, but useful for learning)
A “C”-reference will come in handy in later Learn how to use printf(…) to display output, and scanf(…) for
programming-intensive courses. reading keyboard input.
The C standard library API is linked to from the course Task 3:
Create a “3D-vector” datastructure, and implement functions for
website. vector addition, subtraction, as well as dot- and cross-product.
Write some code that demonstrates this functionality.

Development Options Command-line Compiling with gcc


Executable name
Integrated development environment (IDE) gcc -o filename
E.g. Visual Studio in Windows or Xcode in OS-X. Source code
Streamlines the development process, with features file1.c file2.c .. fileN.c
like graphical debuggers, and “play”-button for
compiling and program execution. -l<lib> {-static}
Read “visual studio getting started guide” on course Libraries & settings
website to get yourself started if using Windows.
Text editor and “Makefile”s Example:
E.g. Emacs and gcc in Linux
gcc -o program program.c formulas.c –lm
Edit source-code in text editors.
Compile program from command line using Makefile ./program
scripts. Link to math library
for sin/cos/sqrt
functions etc.
Makefiles for gcc

Makes frequent compiling easier

Simple “Makefile” content:

make
compiles program (executes “default” script)
./main
executes program

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy