Docsity

C Programming Language Fundamentals: Introduction to C Programming, Lecture notes of Computer Science

Partial preview of the text

Download C Programming Language Fundamentals: Introduction to C Programming and more Lecture notes Computer Science in PDF only on Docsity!

System

Fundamentals

Brent C. Munsell

1

C Programming Language

Agenda

  • Announcements
  • C-Language Basics
  • Standard Input/Output
  • Built-in Data Types
  • Variables and Variable Scope
  • Operators
  • Branching statements
  • Loop statements

C-Language

Basics

4

Simple Program #include <stdio.h> int main( int argc, char** argv ) { printf("Hello, World!\n"); return 0; }

C comments Identical to Java (Comp 210)! // Comment a single line /* Comment multiple lines */

Software program!

  • Commonly referred as “the compiler” … but much much

more :)

Important flags used by GCC:

    • g: produce debug information (used by GDB/valgrind)
    • Werror: treat all warnings as errors (this is our default)
    • Wall: enable all compiler warnings
    • o : name of the executable file Example: gcc – g - Werror - Wall first.c - o first 8 GNU Compiler Collection (GCC)

Standard

Input/Output

10 read and write

Standard Output (stdout)

#include <stdio.h>

int main( int argc, char** argv ) {

printf(”%s!\n”, “Hello World”);

return 0;

  • printf function and format specifiers
    • Displays characters in terminal (stdout)
  • Same as Java version (System.out.printf) and the documented format specifiers. Terminal Keyboard (stdin) Display (stdout) Standard Input/Output Library

Built-in Data

Types

13 32 - bit System

Typical Types (Size and Range) Type Storage size Value range char 1 byte - 128 to 127 or 0 to 255 (system dependent) unsigned char 1 byte 0 to 255 signed char 1 byte - 128 to 127 int 4 bytes - 2,147,483,648 to 2,147,483, unsigned int 4 bytes 0 to 4,294,967, short 2 bytes - 32,768 to 32, unsigned short 2 bytes 0 to 65, long 8 bytes - 9223372036854775808 to 9223372036854775807 unsigned long 8 bytes 0 to 18446744073709551615

Variables

16 Declaration and Names

Variable declaration data type variable name; // without initialization e.g., int a;

  • or- data type variable name = value; // initialization e.g., int a = 10; Identical to Java

Variable

Scope

19 global and local

Global Variable unsigned int x = 10; int main() { printf(”x = %d\n”, x ); func(); return 0; } void func() { printf(”x = %d\n”, x ); } 20 Global Variable